1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.config.registry;
35
36 import java.util.Collection;
37 import java.util.regex.Pattern;
38
39
40
41
42
43
44
45 public abstract class DefinitionQuery<T> {
46 private String module;
47 private String name;
48 private DefinitionType type;
49 private Pattern locationPattern;
50
51 public DefinitionQuery<T> from(String moduleName) {
52 this.module = moduleName;
53 return this;
54 }
55
56 public DefinitionQuery<T> named(String defName) {
57 this.name = defName;
58 return this;
59 }
60
61 public DefinitionQuery<T> ofType(DefinitionType type) {
62 this.type = type;
63 return this;
64 }
65
66 public DefinitionQuery<T> at(String locationRegexPattern) {
67 this.locationPattern = Pattern.compile(locationRegexPattern);
68 return this;
69 }
70
71 public DefinitionQuery<T> at(Pattern locationRegexPattern) {
72 this.locationPattern = locationRegexPattern;
73 return this;
74 }
75
76 public String getModule() {
77 return module;
78 }
79
80 public String getName() {
81 return name;
82 }
83
84 public DefinitionType getType() {
85 return type;
86 }
87
88 public Pattern getLocationPattern() {
89 return locationPattern;
90 }
91
92
93
94
95
96
97 public DefinitionProvider<T> findSingle() {
98 final Collection<DefinitionProvider<T>> multiple = findMultiple();
99 if (multiple.size() < 1) {
100 throw new Registry.NoSuchDefinitionException("No match found for " + this);
101 } else if (multiple.size() > 1) {
102 throw new IllegalStateException("More than one match found for " + this);
103 } else {
104 return multiple.iterator().next();
105 }
106 }
107
108
109
110
111 public abstract Collection<DefinitionProvider<T>> findMultiple();
112
113 @Override
114 public String toString() {
115 final StringBuilder s = new StringBuilder();
116 s.append("DefinitionQuery(");
117 if (this.getModule() != null) {
118 s.append("[module: ").append(this.getModule()).append("]");
119 }
120 if (this.getName() != null) {
121 s.append("[name: ").append(this.getName()).append("]");
122 }
123 if (this.getType() != null) {
124 s.append("[type: ").append(this.getType()).append("]");
125 }
126 if (this.getLocationPattern() != null) {
127 s.append("[locationPattern: ").append(this.getLocationPattern()).append("]");
128 }
129 s.append(")");
130 return s.toString();
131 }
132
133 }