Skip to content

Commit 9e02a1c

Browse files
Finish the implementation of the Customizer inner class
1 parent 2ce7836 commit 9e02a1c

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

testutil/src/main/java/org/spine3/test/NullToleranceTest.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.lang.reflect.InvocationTargetException;
3535
import java.lang.reflect.Method;
3636
import java.util.Arrays;
37+
import java.util.Collections;
3738
import java.util.List;
3839
import java.util.Map;
3940
import java.util.Queue;
@@ -502,8 +503,16 @@ public Builder excludeMethod(String methodName) {
502503
* the predefined list of the default values per type will be used:
503504
* <ul>
504505
* <li>an empty string is used for the {@code String};
506+
* <li>the result of the {@link Collections#emptyList()} call for the types
507+
* derived from {@link List};</li>
508+
* <li>the result of the {@link Collections#emptySet()} call for the types
509+
* derived from {@link Set};</li>
510+
* <li>the result of the {@link Collections#emptyMap()} call for the types
511+
* derived from {@link Map};</li>
512+
* <li>the result of the {@link com.google.common.collect.Queues#newPriorityQueue()} call
513+
* for the types derived from {@link Queue};</li>
505514
* <li>the result of the {@link com.google.common.base.Defaults#defaultValue(Class)} call
506-
* is for the primitives and related wrapper types.</li>
515+
* is for the primitives and related wrapper types;</li>
507516
* <li>the result of {@code getDefaultInstance} call for the types
508517
* derived from {@link Message}.</li>
509518
* </ul>
@@ -565,12 +574,11 @@ public NullToleranceTest build() {
565574
}
566575

567576
private void addDefaultTypeValues() {
568-
569577
final Customizer<String> stringCustomizer = new Customizer<>(STRING_DEFAULT_VALUE, defaultValues);
570-
final Customizer<Queue> queueCustomizer = new Customizer(newPriorityQueue(), defaultValues);
571-
final Customizer<Set> setCustomizer = new Customizer(emptySet(), defaultValues);
572-
final Customizer<List> listCustomizer = new Customizer(emptyList(), defaultValues);
573-
final Customizer<Map> mapCustomizer = new Customizer(emptyMap(), defaultValues);
578+
final Customizer<Queue> queueCustomizer = new Customizer<>(newPriorityQueue(), defaultValues);
579+
final Customizer<Set> setCustomizer = new Customizer<>(emptySet(), defaultValues);
580+
final Customizer<List> listCustomizer = new Customizer<>(emptyList(), defaultValues);
581+
final Customizer<Map> mapCustomizer = new Customizer<>(emptyMap(), defaultValues);
574582
final String defaultStringValue = stringCustomizer.getCustomizedValue(String.class);
575583
defaultValues.put(String.class, defaultStringValue);
576584
final Queue<?> defaultQueue = queueCustomizer.getCustomizedValue(Queue.class);
@@ -588,16 +596,18 @@ private static class Customizer<T> {
588596
private final T defaultValue;
589597
private final Map<Class<?>, ?> defaultValues;
590598

591-
public Customizer(T defaultValue, Map<Class<?>, ?> defaultValues) {
599+
<B extends T> Customizer(B defaultValue, Map<Class<?>, ?> defaultValues) {
592600
this.defaultValue = defaultValue;
593601
this.defaultValues = defaultValues;
594602
}
595603

596-
public T getCustomizedValue(Class<T> typeOfInterest) {
604+
T getCustomizedValue(Class<T> typeOfInterest) {
597605
for (Map.Entry<Class<?>, ?> entry : defaultValues.entrySet()) {
598606
final boolean customValuePresent = typeOfInterest.isAssignableFrom(entry.getKey());
599607
if (customValuePresent) {
600-
return (T) entry.getValue();
608+
@SuppressWarnings("unchecked") // It's OK, since we check for the type compliance above.
609+
final T result = (T) entry.getValue();
610+
return result;
601611
}
602612
}
603613
return defaultValue;

0 commit comments

Comments
 (0)