1
1
package dev .openfeature .sdk ;
2
2
3
- import lombok .EqualsAndHashCode ;
4
- import lombok .ToString ;
5
-
6
3
import java .util .HashMap ;
7
4
import java .util .HashSet ;
8
5
import java .util .Map ;
6
+ import java .util .Optional ;
9
7
import java .util .Set ;
10
- import java .util .stream .Collectors ;
8
+
9
+ import lombok .EqualsAndHashCode ;
10
+ import lombok .ToString ;
11
11
12
12
/**
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
14
15
* 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.
17
19
*/
18
20
@ ToString
19
21
@ 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 {
24
24
25
25
/**
26
26
* create an immutable structure with the empty attributes.
27
27
*/
28
28
public ImmutableStructure () {
29
- this ( new HashMap <>() );
29
+ super ( );
30
30
}
31
31
32
32
/**
@@ -35,10 +35,14 @@ public ImmutableStructure() {
35
35
* @param attributes attributes.
36
36
*/
37
37
public ImmutableStructure (Map <String , Value > attributes ) {
38
- Map < String , Value > copy = attributes .entrySet ()
38
+ super ( new HashMap <>( attributes .entrySet ()
39
39
.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 )));
42
46
}
43
47
44
48
@ Override
@@ -63,25 +67,11 @@ public Map<String, Value> asMap() {
63
67
return attributes
64
68
.entrySet ()
65
69
.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 );
86
76
}
87
77
}
0 commit comments