Skip to content

Commit bf21c51

Browse files
authored
Merge pull request #968 from focus-shift/manager-parameter-nonull
Manager parameter nonull
2 parents ba42571 + bec7c58 commit bf21c51

File tree

10 files changed

+144
-68
lines changed

10 files changed

+144
-68
lines changed

jollyday-core/src/main/java/de/focus_shift/jollyday/core/HolidayManager.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,7 @@ public void init(@NonNull ManagerParameter parameters) {
132132
* @return the manager implementation class name
133133
*/
134134
private static @NonNull String readManagerImplClassName(@NonNull final ManagerParameter parameter) {
135-
final String className = parameter.getManagerImplClassName();
136-
if (className == null) {
137-
throw new IllegalStateException("Missing configuration '" + ManagerParameter.MANAGER_IMPL_CLASS_PREFIX + "'. Cannot create manager.");
138-
}
139-
return className;
135+
return parameter.getManagerImplClassName();
140136
}
141137

142138
/**
@@ -146,11 +142,7 @@ public void init(@NonNull ManagerParameter parameters) {
146142
* @return the manager implementation class name
147143
*/
148144
private static @NonNull String readConfigurationServiceImplClassName(@NonNull final ManagerParameter parameter) {
149-
final String className = parameter.getConfigurationServiceImplClassName();
150-
if (className == null) {
151-
throw new IllegalStateException("Missing configuration '" + ManagerParameter.CONFIGURATION_SERVICE_IMPL + "'. Cannot create configuration service.");
152-
}
153-
return className;
145+
return parameter.getConfigurationServiceImplClassName();
154146
}
155147

156148
/**

jollyday-core/src/main/java/de/focus_shift/jollyday/core/ManagerParameter.java

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,106 @@
44
import org.jspecify.annotations.Nullable;
55

66
import java.net.URL;
7+
import java.util.Optional;
78
import java.util.Properties;
89

10+
/**
11+
* Defines a contract for managing and accessing configuration parameters
12+
* used by a manager component.
13+
*
14+
* <p>This interface provides methods to read, write, and merge properties,
15+
* as well as to retrieve specific configuration values such as implementation
16+
* class names and resource locations.</p>
17+
*
18+
* <p>Implementations are expected to handle property storage and ensure
19+
* consistency when merging or retrieving values.</p>
20+
*/
921
public interface ManagerParameter {
1022

23+
/**
24+
* Prefix used to identify manager implementation class properties.
25+
*/
1126
String MANAGER_IMPL_CLASS_PREFIX = "manager.impl";
1227

28+
/**
29+
* The configuration prefix for parser implementations.
30+
*/
31+
String PARSER_IMPL_PREFIX = "parser.impl.";
32+
33+
/**
34+
* Key used to identify the configuration service implementation class.
35+
*/
1336
String CONFIGURATION_SERVICE_IMPL = "configuration.service.impl";
1437

15-
void mergeProperties(final Properties properties);
38+
/**
39+
* Merges the given properties into the current set of properties.
40+
* Existing keys may be overwritten depending on the implementation.
41+
*
42+
* @param properties the properties to merge; may be {@code null}
43+
*/
44+
void mergeProperties(@Nullable final Properties properties);
45+
46+
/**
47+
* Retrieves the value of a property by its key.
48+
*
49+
* @param key the property key; must not be {@code null}
50+
* @return an {@link Optional} containing the property value if present,
51+
* otherwise an empty {@link Optional}; never {@code null}
52+
*/
53+
@NonNull Optional<String> getProperty(@NonNull final String key);
1654

17-
@Nullable String getProperty(final String key);
55+
/**
56+
* Sets or updates the value of a property.
57+
*
58+
* @param key the property key; must not be {@code null}
59+
* @param value the property value; must not be {@code null}
60+
*/
61+
void setProperty(@NonNull final String key, @NonNull final String value);
1862

19-
void setProperty(final String key, final String value);
63+
/**
64+
* Returns the fully qualified class name of the manager implementation.
65+
*
66+
* @return the manager implementation class name; never {@code null}
67+
*/
68+
@NonNull String getManagerImplClassName();
2069

21-
@Nullable String getManagerImplClassName();
70+
/**
71+
* Returns the fully qualified class name of the parser implementation.
72+
*
73+
* @return the parser implementation class name; never {@code null}
74+
*/
75+
@NonNull String getParserImplClassName(@NonNull final String className);
2276

23-
@Nullable String getConfigurationServiceImplClassName();
77+
/**
78+
* Returns the fully qualified class name of the configuration service implementation.
79+
*
80+
* @return the configuration service implementation class name; never {@code null}
81+
*/
82+
@NonNull String getConfigurationServiceImplClassName();
2483

84+
/**
85+
* Creates a cache key representing the current state of the parameters.
86+
*
87+
* <p>The cache key should uniquely identify the configuration so that
88+
* it can be used for caching purposes.</p>
89+
*
90+
* @return a non-null cache key string
91+
*/
2592
@NonNull String createCacheKey();
2693

94+
/**
95+
* Returns a human-readable display name for this parameter set.
96+
*
97+
* @return a non-null display name
98+
*/
2799
@NonNull String getDisplayName();
28100

29-
@Nullable URL createResourceUrl();
101+
/**
102+
* Creates a {@link URL} pointing to a resource associated with this configuration.
103+
*
104+
* @return a non-null URL to the resource
105+
* @throws RuntimeException if the URL cannot be created
106+
*/
107+
@NonNull URL createResourceUrl();
30108

31109
}

jollyday-core/src/main/java/de/focus_shift/jollyday/core/impl/DefaultHolidayManager.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ public class DefaultHolidayManager extends HolidayManager {
4242

4343
private static final Logger LOG = LoggerFactory.getLogger(DefaultHolidayManager.class);
4444

45-
/**
46-
* The configuration prefix for parser implementations.
47-
*/
48-
private static final String PARSER_IMPL_PREFIX = "parser.impl.";
49-
5045
/**
5146
* Caches all {@link HolidayParser} instances actually used by the HolidayManager
5247
*/
@@ -246,17 +241,12 @@ private void parseHolidays(@NonNull final Year year, @NonNull final Set<Holiday>
246241

247242
@Override
248243
public @NonNull HolidayParser createValue() {
249-
final String parserClassName = getManagerParameter().getProperty(PARSER_IMPL_PREFIX + className);
250-
if (parserClassName != null) {
251-
252-
try {
253-
return (HolidayParser) loadClass(parserClassName).getConstructor().newInstance();
254-
} catch (ReflectiveOperationException | SecurityException e) {
255-
throw new IllegalStateException("Cannot create parsers.", e);
256-
}
244+
final String parserClassName = getManagerParameter().getParserImplClassName(className);
245+
try {
246+
return (HolidayParser) loadClass(parserClassName).getConstructor().newInstance();
247+
} catch (ReflectiveOperationException | SecurityException e) {
248+
throw new IllegalStateException("Cannot create parsers.", e);
257249
}
258-
259-
throw new IllegalStateException("Cannot create parsers. No parser implementation defined for class " + className + " in properties with key " + PARSER_IMPL_PREFIX + className);
260250
}
261251
};
262252

jollyday-core/src/main/java/de/focus_shift/jollyday/core/parameter/BaseManagerParameter.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.jspecify.annotations.NonNull;
55
import org.jspecify.annotations.Nullable;
66

7+
import java.util.Optional;
78
import java.util.Properties;
89

910
public abstract class BaseManagerParameter implements ManagerParameter {
@@ -26,9 +27,8 @@ public void mergeProperties(@Nullable final Properties properties) {
2627
}
2728
}
2829

29-
@Override
30-
public @Nullable String getProperty(@NonNull String key) {
31-
return properties.getProperty(key);
30+
public @NonNull Optional<String> getProperty(@NonNull String key) {
31+
return Optional.ofNullable(properties.getProperty(key));
3232
}
3333

3434
@Override
@@ -37,12 +37,29 @@ public void setProperty(@NonNull String key, @NonNull String value) {
3737
}
3838

3939
@Override
40-
public @Nullable String getManagerImplClassName() {
41-
return getProperty(MANAGER_IMPL_CLASS_PREFIX);
40+
public @NonNull String getManagerImplClassName() {
41+
final Optional<String> managerImplClass = getProperty(MANAGER_IMPL_CLASS_PREFIX);
42+
if (managerImplClass.isEmpty()) {
43+
throw new IllegalStateException("Missing configuration '" + MANAGER_IMPL_CLASS_PREFIX + "'. Cannot create manager.");
44+
}
45+
return managerImplClass.get();
4246
}
4347

4448
@Override
45-
public @Nullable String getConfigurationServiceImplClassName() {
46-
return getProperty(CONFIGURATION_SERVICE_IMPL);
49+
public @NonNull String getParserImplClassName(@NonNull final String className) {
50+
final Optional<String> parserImplClass = getProperty(PARSER_IMPL_PREFIX + className);
51+
if (parserImplClass.isEmpty()) {
52+
throw new IllegalStateException("Cannot create parsers. No parser implementation defined for class " + className + " in properties with key " + PARSER_IMPL_PREFIX + className);
53+
}
54+
return parserImplClass.get();
55+
}
56+
57+
@Override
58+
public @NonNull String getConfigurationServiceImplClassName() {
59+
final Optional<String> configurationServiceImplClass = getProperty(CONFIGURATION_SERVICE_IMPL);
60+
if (configurationServiceImplClass.isEmpty()) {
61+
throw new IllegalStateException("Missing configuration '" + CONFIGURATION_SERVICE_IMPL + "'. Cannot create configuration service.");
62+
}
63+
return configurationServiceImplClass.get();
4764
}
4865
}

jollyday-core/src/main/java/de/focus_shift/jollyday/core/parameter/CalendarPartManagerParameter.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package de.focus_shift.jollyday.core.parameter;
22

3-
import de.focus_shift.jollyday.core.util.ResourceUtil;
43
import org.jspecify.annotations.NonNull;
54
import org.jspecify.annotations.Nullable;
65

76
import java.net.URL;
87
import java.util.Properties;
98

9+
import static de.focus_shift.jollyday.core.util.ResourceUtil.getResource;
10+
1011
public class CalendarPartManagerParameter extends BaseManagerParameter {
1112

1213
private static final String FILE_PREFIX = "holidays/Holidays";
@@ -30,18 +31,16 @@ public CalendarPartManagerParameter(@NonNull final String calendarPart, @Nullabl
3031
}
3132

3233
@Override
33-
public @Nullable URL createResourceUrl() {
34+
public @NonNull URL createResourceUrl() {
3435
final String configurationFileName = getConfigurationFileName(calendarPart);
35-
return ResourceUtil.getResource(configurationFileName).orElse(null);
36+
return getResource(configurationFileName)
37+
.orElseThrow(() -> new IllegalStateException("Cannot find resource '" + configurationFileName + "'."));
3638
}
3739

3840
@Override
39-
public @Nullable String getManagerImplClassName() {
40-
String className = getProperty(MANAGER_IMPL_CLASS_PREFIX + "." + calendarPart);
41-
if (className == null) {
42-
className = super.getManagerImplClassName();
43-
}
44-
return className;
41+
public @NonNull String getManagerImplClassName() {
42+
return getProperty(MANAGER_IMPL_CLASS_PREFIX + "." + calendarPart)
43+
.orElse(super.getManagerImplClassName());
4544
}
4645

4746
/**

jollyday-core/src/test/java/de/focus_shift/jollyday/core/ManagerParametersTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void ensureToCreateCalendarPartManagerParameterWithCalendarPartAndProperties() {
3333
final ManagerParameter managerParameter = ManagerParameters.create("de", properties);
3434
assertThat(managerParameter).isInstanceOf(CalendarPartManagerParameter.class);
3535
assertThat(managerParameter.getDisplayName()).isEqualTo("de");
36-
assertThat(managerParameter.getProperty("property")).isEqualTo("property-value");
36+
assertThat(managerParameter.getProperty("property")).hasValue("property-value");
3737
}
3838

3939
@Test
@@ -62,7 +62,7 @@ void ensureToCreateCalendarPartManagerParameterWithLocaleAndProperties() {
6262
final ManagerParameter managerParameter = ManagerParameters.create(Locale.GERMANY, properties);
6363
assertThat(managerParameter).isInstanceOf(CalendarPartManagerParameter.class);
6464
assertThat(managerParameter.getDisplayName()).isEqualTo("de");
65-
assertThat(managerParameter.getProperty("property")).isEqualTo("property-value");
65+
assertThat(managerParameter.getProperty("property")).hasValue("property-value");
6666
}
6767

6868
@Test
@@ -81,7 +81,7 @@ void ensureToCreateCalendarPartManagerParameterWithHolidayCalendarAndProperties(
8181
final ManagerParameter managerParameter = ManagerParameters.create(HolidayCalendar.GERMANY, properties);
8282
assertThat(managerParameter).isInstanceOf(CalendarPartManagerParameter.class);
8383
assertThat(managerParameter.getDisplayName()).isEqualTo("de");
84-
assertThat(managerParameter.getProperty("property")).isEqualTo("property-value");
84+
assertThat(managerParameter.getProperty("property")).hasValue("property-value");
8585
}
8686

8787
@Test
@@ -107,6 +107,6 @@ void ensureToCreateUrlManagerParameterWithHolidayCalendarAndProperties() {
107107
final ManagerParameter managerParameter = ManagerParameters.create(url, properties);
108108
assertThat(managerParameter).isInstanceOf(UrlManagerParameter.class);
109109
assertThat(managerParameter.getDisplayName()).isEqualTo("Holidays_test.xml");
110-
assertThat(managerParameter.getProperty("property")).isEqualTo("property-value");
110+
assertThat(managerParameter.getProperty("property")).hasValue("property-value");
111111
}
112112
}

jollyday-core/src/test/java/de/focus_shift/jollyday/core/configuration/ConfigurationProviderManagerTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ void ensuresUrlConfigurationProviderOverridesBaseClasspathConfigurationProvider(
2929
final ManagerParameter managerParameter = ManagerParameters.create((String) null);
3030
sut.mergeConfigurationProperties(managerParameter);
3131

32-
assertThat(managerParameter.getProperty("manager.impl")).isEqualTo("UrlConfigurationProviderOverloaded");
33-
assertThat(managerParameter.getProperty("manager.impl.url")).isEqualTo("UrlConfigurationProviderOverloaded.url");
32+
assertThat(managerParameter.getProperty("manager.impl")).hasValue("UrlConfigurationProviderOverloaded");
33+
assertThat(managerParameter.getProperty("manager.impl.url")).hasValue("UrlConfigurationProviderOverloaded.url");
3434
System.clearProperty(CONFIG_URLS_PROPERTY);
3535
}
3636

@@ -42,9 +42,9 @@ void ensuresProviderClassesConfigurationProviderOverridesUrlConfigurationProvide
4242
final ManagerParameter managerParameter = ManagerParameters.create((String) null);
4343
sut.mergeConfigurationProperties(managerParameter);
4444

45-
assertThat(managerParameter.getProperty("manager.impl")).isEqualTo("ProviderClassesConfigurationProviderOverloaded");
46-
assertThat(managerParameter.getProperty("manager.impl.url")).isEqualTo("UrlConfigurationProviderOverloaded.url");
47-
assertThat(managerParameter.getProperty("manager.impl.custom")).isEqualTo("ProviderClassesConfigurationProviderOverloaded.custom");
45+
assertThat(managerParameter.getProperty("manager.impl")).hasValue("ProviderClassesConfigurationProviderOverloaded");
46+
assertThat(managerParameter.getProperty("manager.impl.url")).hasValue("UrlConfigurationProviderOverloaded.url");
47+
assertThat(managerParameter.getProperty("manager.impl.custom")).hasValue("ProviderClassesConfigurationProviderOverloaded.custom");
4848

4949
System.clearProperty(CONFIG_URLS_PROPERTY);
5050
System.clearProperty(CONFIG_PROVIDERS_PROPERTY);
@@ -62,8 +62,8 @@ void ensuresManagerParametersOverridesProviderClassConfigurationProvider() {
6262

6363
sut.mergeConfigurationProperties(managerParameter);
6464

65-
assertThat(managerParameter.getProperty("manager.impl")).isEqualTo("ManagerParameterOverloaded");
66-
assertThat(managerParameter.getProperty("manager.impl.manager")).isEqualTo("ManagerParameterOverloaded.manager");
65+
assertThat(managerParameter.getProperty("manager.impl")).hasValue("ManagerParameterOverloaded");
66+
assertThat(managerParameter.getProperty("manager.impl.manager")).hasValue("ManagerParameterOverloaded.manager");
6767

6868
System.clearProperty(CONFIG_URLS_PROPERTY);
6969
System.clearProperty(CONFIG_PROVIDERS_PROPERTY);

jollyday-core/src/test/java/de/focus_shift/jollyday/core/parameter/BaseManagerParameterTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ void ensureToAddAllPropertiesOnCreating() {
1818
properties.setProperty("manager.impl", "managerImplClassName");
1919

2020
final TestBaseManagerParameter sut = new TestBaseManagerParameter(properties);
21-
assertThat(sut.getProperty("somebody")).isEqualTo("just told me");
22-
assertThat(sut.getProperty("manager.impl")).isEqualTo("managerImplClassName");
21+
assertThat(sut.getProperty("somebody")).hasValue("just told me");
22+
assertThat(sut.getProperty("manager.impl")).hasValue("managerImplClassName");
2323
}
2424

2525
@Test
@@ -28,7 +28,7 @@ void ensureToSetProperty() {
2828
final TestBaseManagerParameter sut = new TestBaseManagerParameter(new Properties());
2929
sut.setProperty("somebody", "just told me");
3030

31-
assertThat(sut.getProperty("somebody")).isEqualTo("just told me");
31+
assertThat(sut.getProperty("somebody")).hasValue("just told me");
3232
}
3333

3434
@Test
@@ -38,17 +38,17 @@ void ensureToMergePropertiesAndCannotOverridesTheDefaults() {
3838
properties.setProperty("manager.impl", "managerImplClassName");
3939

4040
final TestBaseManagerParameter sut = new TestBaseManagerParameter(properties);
41-
assertThat(sut.getProperty("somebody")).isEqualTo("just told me");
42-
assertThat(sut.getProperty("manager.impl")).isEqualTo("managerImplClassName");
41+
assertThat(sut.getProperty("somebody")).hasValue("just told me");
42+
assertThat(sut.getProperty("manager.impl")).hasValue("managerImplClassName");
4343

4444
final Properties mergeProperties = new Properties();
4545
mergeProperties.setProperty("somebody", "just told me that this is a new one");
4646
mergeProperties.setProperty("somebody new", "just told me that this is a new one");
4747

4848
sut.mergeProperties(mergeProperties);
49-
assertThat(sut.getProperty("somebody")).isEqualTo("just told me");
50-
assertThat(sut.getProperty("somebody new")).isEqualTo("just told me that this is a new one");
51-
assertThat(sut.getProperty("manager.impl")).isEqualTo("managerImplClassName");
49+
assertThat(sut.getProperty("somebody")).hasValue("just told me");
50+
assertThat(sut.getProperty("somebody new")).hasValue("just told me that this is a new one");
51+
assertThat(sut.getProperty("manager.impl")).hasValue("managerImplClassName");
5252
}
5353

5454
@Test

jollyday-tests/src/test/java/de/focus_shift/jollyday/tests/CalendarChecker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ private void assertHolidayPresent(Set<Holiday> holidays, Holiday holiday, Holida
265265
}
266266

267267
private String buildAssertionMessage(HolidayManager holidayManager, String[] subdivisions) {
268-
final String displayName = holidayManager != null && holidayManager.getManagerParameter() != null ? holidayManager.getManagerParameter().getDisplayName() : "UnknownManager";
268+
final String displayName = holidayManager != null ? holidayManager.getManagerParameter().getDisplayName() : "UnknownManager";
269269
final String subdivisionsStr = subdivisions != null ? Arrays.toString(subdivisions) : "[]";
270270
return "Failure in a holiday for " + displayName + " " + subdivisionsStr;
271271
}

jollyday-tests/src/test/java/de/focus_shift/jollyday/tests/DefaultHolidayManagerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ void ensureToGetHolidayManagerWithProperties() {
341341
final Properties properties = new Properties();
342342
properties.setProperty("prop", "test");
343343
final HolidayManager sut = HolidayManager.getInstance(properties);
344-
assertThat(sut.getManagerParameter().getProperty("prop")).isEqualTo("test");
344+
assertThat(sut.getManagerParameter().getProperty("prop")).hasValue("test");
345345
}
346346

347347
@Test

0 commit comments

Comments
 (0)