11package dev .openfeature .sdk ;
22
3- import lombok .EqualsAndHashCode ;
4- import lombok .ToString ;
5-
63import java .util .HashMap ;
74import java .util .HashSet ;
85import java .util .Map ;
6+ import java .util .Optional ;
97import java .util .Set ;
10- import java .util .stream .Collectors ;
8+
9+ import lombok .EqualsAndHashCode ;
10+ import lombok .ToString ;
1111
1212/**
13- * {@link ImmutableStructure} represents a potentially nested object type which is used to represent
13+ * {@link ImmutableStructure} represents a potentially nested object type which
14+ * is used to represent
1415 * structured data.
15- * The ImmutableStructure is a Structure implementation which is threadsafe, and whose attributes can
16- * not be modified after instantiation.
16+ * The ImmutableStructure is a Structure implementation which is threadsafe, and
17+ * whose attributes can
18+ * not be modified after instantiation. All references are clones.
1719 */
1820@ ToString
1921@ EqualsAndHashCode
20- @ SuppressWarnings ({"PMD.BeanMembersShouldSerialize" , "checkstyle:MissingJavadocType" })
21- public final class ImmutableStructure implements Structure {
22-
23- private final Map <String , Value > attributes ;
22+ @ SuppressWarnings ({ "PMD.BeanMembersShouldSerialize" , "checkstyle:MissingJavadocType" })
23+ public final class ImmutableStructure extends AbstractStructure {
2424
2525 /**
2626 * create an immutable structure with the empty attributes.
2727 */
2828 public ImmutableStructure () {
29- this ( new HashMap <>() );
29+ super ( );
3030 }
3131
3232 /**
@@ -35,10 +35,14 @@ public ImmutableStructure() {
3535 * @param attributes attributes.
3636 */
3737 public ImmutableStructure (Map <String , Value > attributes ) {
38- Map < String , Value > copy = attributes .entrySet ()
38+ super ( new HashMap <>( attributes .entrySet ()
3939 .stream ()
40- .collect (Collectors .toMap (Map .Entry ::getKey , e -> e .getValue ().clone ()));
41- this .attributes = new HashMap <>(copy );
40+ .collect (HashMap ::new ,
41+ (accumulated , entry ) -> accumulated .put (entry .getKey (),
42+ Optional .ofNullable (entry .getValue ())
43+ .map (e -> e .clone ())
44+ .orElse (null )),
45+ HashMap ::putAll )));
4246 }
4347
4448 @ Override
@@ -63,25 +67,11 @@ public Map<String, Value> asMap() {
6367 return attributes
6468 .entrySet ()
6569 .stream ()
66- .collect (Collectors .toMap (
67- Map .Entry ::getKey ,
68- e -> getValue (e .getKey ())
69- ));
70- }
71-
72- /**
73- * Get all values, with primitives types.
74- *
75- * @return all attributes on the structure into a Map
76- */
77- @ Override
78- public Map <String , Object > asObjectMap () {
79- return attributes
80- .entrySet ()
81- .stream ()
82- .collect (Collectors .toMap (
83- Map .Entry ::getKey ,
84- e -> convertValue (getValue (e .getKey ()))
85- ));
70+ .collect (HashMap ::new ,
71+ (accumulated , entry ) -> accumulated .put (entry .getKey (),
72+ Optional .ofNullable (entry .getValue ())
73+ .map (e -> e .clone ())
74+ .orElse (null )),
75+ HashMap ::putAll );
8676 }
8777}
0 commit comments