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