forked from open-feature/java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractStructure.java
51 lines (43 loc) · 1.47 KB
/
AbstractStructure.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package dev.openfeature.sdk;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import lombok.EqualsAndHashCode;
@SuppressWarnings({"PMD.BeanMembersShouldSerialize", "checkstyle:MissingJavadocType"})
@EqualsAndHashCode
abstract class AbstractStructure implements Structure {
protected final Map<String, Value> attributes;
@Override
public boolean isEmpty() {
return attributes == null || attributes.isEmpty();
}
AbstractStructure() {
this.attributes = new HashMap<>();
}
AbstractStructure(Map<String, Value> attributes) {
this.attributes = attributes;
}
/**
* Returns an unmodifiable representation of the internal attribute map.
*
* @return immutable map
*/
public Map<String, Value> asUnmodifiableMap() {
return Collections.unmodifiableMap(attributes);
}
/**
* Get all values as their underlying primitives types.
*
* @return all attributes on the structure into a Map
*/
@Override
public Map<String, Object> asObjectMap() {
return attributes.entrySet().stream()
// custom collector, workaround for Collectors.toMap in JDK8
// https://bugs.openjdk.org/browse/JDK-8148463
.collect(
HashMap::new,
(accumulated, entry) -> accumulated.put(entry.getKey(), convertValue(entry.getValue())),
HashMap::putAll);
}
}