Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/main/java/dev/openfeature/sdk/AbstractStructure.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;

@SuppressWarnings({"PMD.BeanMembersShouldSerialize", "checkstyle:MissingJavadocType"})
Expand Down Expand Up @@ -42,13 +43,11 @@ public Map<String, Value> asUnmodifiableMap() {
*/
@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);
Map<String, Object> objectMap = HashMapUtils.forEntries(attributes.size());
for (Entry<String, Value> entry : attributes.entrySet()) {
objectMap.put(entry.getKey(), convertValue(entry.getValue()));
}
return objectMap;
}

@Override
Expand Down
34 changes: 20 additions & 14 deletions src/main/java/dev/openfeature/sdk/Structure.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import static dev.openfeature.sdk.Value.objectToValue;

import dev.openfeature.sdk.exceptions.ValueNotConvertableError;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;

/**
* {@link Structure} represents a potentially nested object type which is used to represent
Expand Down Expand Up @@ -94,16 +95,21 @@ default Object convertValue(Value value) {
}

if (value.isList()) {
return value.asList().stream().map(this::convertValue).collect(Collectors.toList());
List<Value> listValue = value.asList();
List<Object> converted = new ArrayList<>(listValue.size());
for (Value element : listValue) {
converted.add(convertValue(element));
}
return converted;
}

if (value.isStructure()) {
Structure s = value.asStructure();
return s.asUnmodifiableMap().entrySet().stream()
.collect(
HashMap::new,
(accumulated, entry) -> accumulated.put(entry.getKey(), convertValue(entry.getValue())),
HashMap::putAll);
Map<String, Value> structureAttributes = value.asStructure().asUnmodifiableMap();
Map<String, Object> converted = HashMapUtils.forEntries(structureAttributes.size());
for (Entry<String, Value> entry : structureAttributes.entrySet()) {
converted.put(entry.getKey(), convertValue(entry.getValue()));
}
return converted;
}

throw new ValueNotConvertableError();
Expand All @@ -116,10 +122,10 @@ default Object convertValue(Value value) {
* @return a Structure object in the SDK format
*/
static Structure mapToStructure(Map<String, Object> map) {
return new MutableStructure(map.entrySet().stream()
.collect(
HashMap::new,
(accumulated, entry) -> accumulated.put(entry.getKey(), objectToValue(entry.getValue())),
HashMap::putAll));
Map<String, Value> attributes = HashMapUtils.forEntries(map.size());
for (Entry<String, Object> entry : map.entrySet()) {
attributes.put(entry.getKey(), objectToValue(entry.getValue()));
}
return new MutableStructure(attributes);
}
}
16 changes: 12 additions & 4 deletions src/main/java/dev/openfeature/sdk/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import dev.openfeature.sdk.exceptions.TypeMismatchError;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.EqualsAndHashCode;
import lombok.SneakyThrows;
import lombok.ToString;
Expand Down Expand Up @@ -288,7 +288,11 @@ public Instant asInstant() {
@Override
protected Value clone() {
if (this.isList()) {
List<Value> copy = this.asList().stream().map(Value::new).collect(Collectors.toList());
List<Value> listValue = this.asList();
List<Value> copy = new ArrayList<>(listValue.size());
for (Value element : listValue) {
copy.add(new Value(element));
}
return new Value(copy);
}
if (this.isStructure()) {
Expand Down Expand Up @@ -325,8 +329,12 @@ public static Value objectToValue(Object object) {
} else if (object instanceof Structure) {
return new Value((Structure) object);
} else if (object instanceof List) {
return new Value(
((List<Object>) object).stream().map(o -> objectToValue(o)).collect(Collectors.toList()));
List<Object> listValue = (List<Object>) object;
List<Value> values = new ArrayList<>(listValue.size());
for (Object element : listValue) {
values.add(objectToValue(element));
}
return new Value(values);
} else if (object instanceof Instant) {
return new Value((Instant) object);
} else if (object instanceof Map) {
Expand Down