11package ch .jalu .configme .properties ;
22
3- import ch .jalu .configme .properties .convertresult . ConvertErrorRecorder ;
3+ import ch .jalu .configme .properties .types . MapPropertyType ;
44import ch .jalu .configme .properties .types .PropertyType ;
5- import ch .jalu .configme .resource .PropertyReader ;
65import org .jetbrains .annotations .NotNull ;
7- import org .jetbrains .annotations .Nullable ;
86
97import java .util .Collections ;
10- import java .util .LinkedHashMap ;
118import java .util .Map ;
12- import java .util .Objects ;
139
1410/**
15- * Property for an immutable map whose keys is of type String and whose values can be configured .
16- * The map retains the order of the elements .
11+ * Property for a map with String keys and a configurable value type. The map retains the order of the elements .
12+ * Maps produced by this property are guaranteed to never have a null key or null value .
1713 *
1814 * @param <V> the value type of the map
1915 */
20- public class MapProperty <V > extends BaseProperty <Map <String , V >> {
21-
22- private final PropertyType <V > valueType ;
16+ public class MapProperty <V > extends TypeBasedProperty <Map <String , V >> {
2317
2418 /**
2519 * Constructor. Builds a {@link MapProperty} with an empty map as default value.
@@ -28,9 +22,7 @@ public class MapProperty<V> extends BaseProperty<Map<String, V>> {
2822 * @param valueType the property type of the values
2923 */
3024 public MapProperty (@ NotNull String path , @ NotNull PropertyType <V > valueType ) {
31- super (path , Collections .emptyMap ());
32- Objects .requireNonNull (valueType , "valueType" );
33- this .valueType = valueType ;
25+ super (path , new MapPropertyType <>(valueType ), Collections .emptyMap ());
3426 }
3527
3628 /**
@@ -41,48 +33,46 @@ public MapProperty(@NotNull String path, @NotNull PropertyType<V> valueType) {
4133 * @param defaultValue the default value of the property
4234 */
4335 public MapProperty (@ NotNull String path , @ NotNull PropertyType <V > valueType , @ NotNull Map <String , V > defaultValue ) {
44- super (path , Collections .unmodifiableMap (defaultValue ));
45- Objects .requireNonNull (valueType , "valueType" );
46- this .valueType = valueType ;
36+ super (path , new MapPropertyType <>(valueType ), defaultValue );
4737 }
4838
49- @ Override
50- protected @ Nullable Map <String , V > getFromReader (@ NotNull PropertyReader reader ,
51- @ NotNull ConvertErrorRecorder errorRecorder ) {
52- Object rawObject = reader .getObject (getPath ());
53-
54- if (!(rawObject instanceof Map <?, ?>)) {
55- return null ;
56- }
57-
58- Map <?, ?> rawMap = (Map <?, ?>) rawObject ;
59- Map <String , V > map = new LinkedHashMap <>();
60-
61- for (Map .Entry <?, ?> entry : rawMap .entrySet ()) {
62- String path = entry .getKey ().toString ();
63- V value = valueType .convert (entry .getValue (), errorRecorder );
64-
65- if (value != null ) {
66- map .put (path , value );
67- }
68- }
69-
70- return postProcessMap (map );
39+ /**
40+ * Constructor. Use {@link #withMapType}.
41+ *
42+ * @param mapType the map type
43+ * @param path the path of the property
44+ * @param defaultValue the default value of the property
45+ */
46+ // Constructor arguments are usually (path, type, defaultValue), but this is not possible here because there
47+ // are other constructors with the same argument order.
48+ protected MapProperty (@ NotNull PropertyType <Map <String , V >> mapType , @ NotNull String path ,
49+ @ NotNull Map <String , V > defaultValue ) {
50+ super (path , mapType , defaultValue );
7151 }
7252
73- @ Override
74- public @ NotNull Object toExportValue (@ NotNull Map <String , V > value ) {
75- Map <String , Object > exportMap = new LinkedHashMap <>();
76-
77- for (Map .Entry <String , V > entry : value .entrySet ()) {
78- exportMap .put (entry .getKey (), valueType .toExportValue (entry .getValue ()));
79- }
80-
81- return exportMap ;
53+ /**
54+ * Creates a new map property with the given path and type. An empty map is set as default value.
55+ *
56+ * @param path the path of the property
57+ * @param mapType the map type
58+ * @param <V> the type of the values in the map
59+ * @return a new map property
60+ */
61+ public static <V > MapProperty <V > withMapType (@ NotNull String path , @ NotNull PropertyType <Map <String , V >> mapType ) {
62+ return new MapProperty <>(mapType , path , Collections .emptyMap ());
8263 }
8364
84- /* Allows to modify the map once its fully built based on the values in the property reader. */
85- protected @ NotNull Map <String , V > postProcessMap (@ NotNull Map <String , V > constructedMap ) {
86- return Collections .unmodifiableMap (constructedMap );
65+ /**
66+ * Creates a new map property with the given path, type and default value.
67+ *
68+ * @param path the path of the property
69+ * @param mapType the map type
70+ * @param defaultValue the default value of the property
71+ * @param <V> the type of the values in the map
72+ * @return a new map property
73+ */
74+ public static <V > MapProperty <V > withMapType (@ NotNull String path , @ NotNull PropertyType <Map <String , V >> mapType ,
75+ @ NotNull Map <String , V > defaultValue ) {
76+ return new MapProperty <>(mapType , path , defaultValue );
8777 }
8878}
0 commit comments