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 info.magnolia.config.source.ConfigurationSourceType;
37
38 import java.util.Collection;
39 import java.util.regex.Pattern;
40
41
42
43
44
45
46
47 public abstract class DefinitionQuery<T> {
48 private String module;
49 private String name;
50 private DefinitionType type;
51 private ConfigurationSourceType configurationSourceType;
52 private Pattern locationPattern;
53
54 protected DefinitionQuery() {
55 }
56
57
58
59
60
61
62 protected DefinitionQuery(DefinitionQuery query) {
63 this.module = query.module;
64 this.name = query.name;
65 this.type = query.type;
66 this.configurationSourceType = query.configurationSourceType;
67 this.locationPattern = query.locationPattern;
68 }
69
70
71
72
73 public static <T> DefinitionQuery<T> build(Registry<T> registry) {
74 return new DefinitionQueryImpl<T>(registry);
75 }
76
77
78
79
80
81 public static <T> DefinitionQuery<T> build(Registry<T> registry, DefinitionQuery<T> baseQuery) {
82 return new DefinitionQueryImpl<T>(registry, baseQuery);
83 }
84
85 public DefinitionQuery<T> from(String moduleName) {
86 this.module = moduleName;
87 return this;
88 }
89
90 public DefinitionQuery<T> named(String defName) {
91 this.name = defName;
92 return this;
93 }
94
95 public DefinitionQuery<T> ofType(DefinitionType type) {
96 this.type = type;
97 return this;
98 }
99
100 public DefinitionQuery<T> ofConfigurationSourceType(ConfigurationSourceType configurationSourceType) {
101 this.configurationSourceType = configurationSourceType;
102 return this;
103 }
104
105 public DefinitionQuery<T> at(String locationRegexPattern) {
106 this.locationPattern = Pattern.compile(locationRegexPattern);
107 return this;
108 }
109
110 public DefinitionQuery<T> at(Pattern locationRegexPattern) {
111 this.locationPattern = locationRegexPattern;
112 return this;
113 }
114
115 public String getModule() {
116 return module;
117 }
118
119 public String getName() {
120 return name;
121 }
122
123 public DefinitionType getType() {
124 return type;
125 }
126
127 public ConfigurationSourceType getConfigurationSourceType() {
128 return configurationSourceType;
129 }
130
131 public Pattern getLocationPattern() {
132 return locationPattern;
133 }
134
135
136
137
138
139
140 public DefinitionProvider<T> findSingle() {
141 final Collection<DefinitionProvider<T>> multiple = findMultiple();
142 if (multiple.size() < 1) {
143 throw new Registry.NoSuchDefinitionException("No match found for " + this);
144 } else if (multiple.size() > 1) {
145 throw new IllegalStateException("More than one match found for " + this);
146 } else {
147 return multiple.iterator().next();
148 }
149 }
150
151
152
153
154 public abstract Collection<DefinitionProvider<T>> findMultiple();
155
156 @Override
157 public String toString() {
158 final StringBuilder s = new StringBuilder();
159 s.append("DefinitionQuery(");
160 if (this.getModule() != null) {
161 s.append("[module: ").append(this.getModule()).append("]");
162 }
163 if (this.getName() != null) {
164 s.append("[name: ").append(this.getName()).append("]");
165 }
166 if (this.getType() != null) {
167 s.append("[type: ").append(this.getType()).append("]");
168 }
169 if (this.getConfigurationSourceType() != null) {
170 s.append("[configurationSourceType: ").append(this.getConfigurationSourceType()).append("]");
171 }
172 if (this.getLocationPattern() != null) {
173 s.append("[locationPattern: ").append(this.getLocationPattern()).append("]");
174 }
175 s.append(")");
176 return s.toString();
177 }
178 }