Skip to content

Commit 58e2877

Browse files
thiyagu06toddbaert
authored andcommitted
removed copy method and implemented clone.
Signed-off-by: thiyagu06 <[email protected]>
1 parent d0aa19a commit 58e2877

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

src/main/java/dev/openfeature/sdk/ImmutableStructure.java

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public ImmutableStructure() {
3939
public ImmutableStructure(Map<String, Value> attributes) {
4040
Map<String, Value> copy = attributes.entrySet()
4141
.stream()
42-
.collect(Collectors.toMap(Map.Entry::getKey, e -> copy(e.getValue())));
42+
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().clone()));
4343
this.attributes = new HashMap<>(copy);
4444
}
4545

@@ -52,7 +52,7 @@ public Set<String> keySet() {
5252
@Override
5353
public Value getValue(String key) {
5454
Value value = this.attributes.get(key);
55-
return copy(value);
55+
return value.clone();
5656
}
5757

5858
/**
@@ -86,30 +86,4 @@ public Map<String, Object> asObjectMap() {
8686
e -> convertValue(getValue(e.getKey()))
8787
));
8888
}
89-
90-
/**
91-
* Perform deep copy of value object.
92-
*
93-
* @param value value object
94-
* @return new copy of the given value object
95-
*/
96-
@SneakyThrows
97-
private Value copy(Value value) {
98-
if (value.isList()) {
99-
List<Value> copy = value.asList().stream().map(Value::new).collect(Collectors.toList());
100-
return new Value(copy);
101-
}
102-
if (value.isStructure()) {
103-
Map<String, Value> copy = value.asStructure().asMap().entrySet().stream().collect(Collectors.toMap(
104-
Map.Entry::getKey,
105-
e -> copy(e.getValue())
106-
));
107-
return new Value(new ImmutableStructure(copy));
108-
}
109-
if (value.isInstant()) {
110-
Instant copy = Instant.ofEpochMilli(value.asInstant().toEpochMilli());
111-
return new Value(copy);
112-
}
113-
return new Value(value.asObject());
114-
}
11589
}

src/main/java/dev/openfeature/sdk/Value.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import java.time.Instant;
44
import java.util.List;
5+
import java.util.Map;
6+
import java.util.stream.Collectors;
57

68
import lombok.EqualsAndHashCode;
9+
import lombok.SneakyThrows;
710
import lombok.ToString;
811

912
/**
@@ -14,7 +17,7 @@
1417
@ToString
1518
@EqualsAndHashCode
1619
@SuppressWarnings({"PMD.BeanMembersShouldSerialize", "checkstyle:MissingJavadocType"})
17-
public class Value {
20+
public class Value implements Cloneable{
1821

1922
private final Object innerObject;
2023

@@ -238,4 +241,31 @@ public Instant asInstant() {
238241
}
239242
return null;
240243
}
244+
245+
/**
246+
* Perform deep clone of value object.
247+
*
248+
* @return Value
249+
*/
250+
251+
@SneakyThrows
252+
@Override
253+
protected Value clone() {
254+
if (this.isList()) {
255+
List<Value> copy = this.asList().stream().map(Value::new).collect(Collectors.toList());
256+
return new Value(copy);
257+
}
258+
if (this.isStructure()) {
259+
Map<String, Value> copy = this.asStructure().asMap().entrySet().stream().collect(Collectors.toMap(
260+
Map.Entry::getKey,
261+
e -> e.getValue().clone()
262+
));
263+
return new Value(new ImmutableStructure(copy));
264+
}
265+
if (this.isInstant()) {
266+
Instant copy = Instant.ofEpochMilli(this.asInstant().toEpochMilli());
267+
return new Value(copy);
268+
}
269+
return new Value(this.asObject());
270+
}
241271
}

0 commit comments

Comments
 (0)