Skip to content

Commit 48a4916

Browse files
committed
MCR-3650 refactor MCRConfigurableInstanceHelper into multiple classes
1 parent f6fe7e9 commit 48a4916

29 files changed

+2205
-1750
lines changed

mycore-base/src/main/java/org/mycore/common/config/MCRConfigurableInstanceHelper.java

Lines changed: 15 additions & 1731 deletions
Large diffs are not rendered by default.

mycore-base/src/main/java/org/mycore/common/config/MCRConfiguration2.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import java.util.stream.Stream;
3333

3434
import org.mycore.common.MCRClassTools;
35+
import org.mycore.common.config.MCRInstanceConfiguration.Option;
36+
import org.mycore.common.config.MCRInstanceConfiguration.Options;
3537
import org.mycore.common.function.MCRTriConsumer;
3638

3739
/**
@@ -131,11 +133,11 @@ public static Map<String, String> getSubPropertiesMap(String propertyPrefix) {
131133
* @throws MCRConfigurationException if the class can not be loaded or instantiated
132134
*/
133135
public static <S> Optional<S> getInstanceOf(Class<S> superClass, String name) throws MCRConfigurationException {
134-
return getInstanceOf(superClass, name, MCRConfigurableInstanceHelper.NO_OPTIONS);
136+
return getInstanceOf(superClass, name, Options.NONE);
135137
}
136138

137139
private static <S> Optional<S> getInstanceOf(Class<S> superClass, String name,
138-
Set<MCRConfigurableInstanceHelper.Option> options) {
140+
Set<Option> options) {
139141
if (MCRConfigurableInstanceHelper.isSingleton(superClass)) {
140142
return getSingleInstanceOf(superClass, name, options);
141143
} else {
@@ -153,7 +155,7 @@ private static <S> Optional<S> getInstanceOf(Class<S> superClass, String name,
153155
* or the configuration property is not set
154156
*/
155157
public static <S> S getInstanceOfOrThrow(Class<S> superClass, String name) throws MCRConfigurationException {
156-
return getInstanceOf(superClass, name, MCRConfigurableInstanceHelper.ADD_IMPLICIT_CLASS_PROPERTIES)
158+
return getInstanceOf(superClass, name, Options.IMPLICIT)
157159
.orElseThrow(() -> createConfigurationException(name));
158160
}
159161

@@ -167,11 +169,11 @@ public static <S> S getInstanceOfOrThrow(Class<S> superClass, String name) throw
167169
* @throws MCRConfigurationException if the class can not be loaded or instantiated
168170
*/
169171
public static <S> Optional<S> getSingleInstanceOf(Class<S> superClass, String name) {
170-
return getSingleInstanceOf(superClass, name, MCRConfigurableInstanceHelper.NO_OPTIONS);
172+
return getSingleInstanceOf(superClass, name, Options.NONE);
171173
}
172174

173175
private static <S> Optional<S> getSingleInstanceOf(Class<S> superClass, String name,
174-
Set<MCRConfigurableInstanceHelper.Option> options) {
176+
Set<Option> options) {
175177
return getString(name)
176178
.map(className -> new ConfigSingletonKey(name, className))
177179
.map(key -> (S) instanceHolder.computeIfAbsent(key,
@@ -189,7 +191,7 @@ private static <S> Optional<S> getSingleInstanceOf(Class<S> superClass, String n
189191
* or the configuration property is not set
190192
*/
191193
public static <S> S getSingleInstanceOfOrThrow(Class<S> superClass, String name) {
192-
return getSingleInstanceOf(superClass, name, MCRConfigurableInstanceHelper.ADD_IMPLICIT_CLASS_PROPERTIES)
194+
return getSingleInstanceOf(superClass, name, Options.IMPLICIT)
193195
.orElseThrow(() -> createConfigurationException(name));
194196
}
195197

mycore-base/src/main/java/org/mycore/common/config/MCRInstanceConfiguration.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
package org.mycore.common.config;
2020

2121
import java.util.Collections;
22+
import java.util.EnumSet;
2223
import java.util.HashMap;
2324
import java.util.List;
2425
import java.util.Map;
26+
import java.util.Set;
2527

2628
/**
2729
* Represents an extract of properties (typically {@link MCRConfiguration2#getPropertiesMap()}) used to
@@ -389,6 +391,9 @@ public Map<String, MCRInstanceConfiguration> nestedConfigurationMap() {
389391
* @return the nested configuration map
390392
*/
391393
public Map<String, MCRInstanceConfiguration> nestedConfigurationMap(String commonPrefix) {
394+
if (commonPrefix.isEmpty()) {
395+
return nestedConfigurationMap();
396+
}
392397
String commonSuffixWithDelimiter = commonPrefix + ".";
393398
Map<String, MCRInstanceConfiguration> nestedConfigurationMap = new HashMap<>();
394399
for (Map.Entry<String, String> entry : properties().entrySet()) {
@@ -444,4 +449,28 @@ public String toString() {
444449
"#fullProperties=" + fullProperties.size() + "}";
445450
}
446451

452+
public enum Option {
453+
454+
/**
455+
* If a class name is required to be in the configuration properties (for example, because of usage of
456+
* {@link MCRConfiguration2#getInstanceOfOrThrow(Class, String)} or because of an annotation with
457+
* <code>required=true</code>) and the expected super class is a final class (meaning, if the property
458+
* containing the class name is required to exist and can only have the class name of that final class),
459+
* assume that that property exists (if it doesn't).
460+
*/
461+
IMPLICIT
462+
463+
}
464+
465+
public static final class Options {
466+
467+
public static final Set<Option> NONE = EnumSet.noneOf(Option.class);
468+
469+
public static final Set<Option> IMPLICIT = EnumSet.of(Option.IMPLICIT);
470+
471+
private Options() {
472+
}
473+
474+
}
475+
447476
}

0 commit comments

Comments
 (0)