Skip to content

Commit

Permalink
Use unmodifiableSet for JsonMap
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed Jul 1, 2024
1 parent f6d5e6d commit 203d575
Showing 1 changed file with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package net.javacrumbs.jsonunit.core.internal;


import org.jetbrains.annotations.NotNull;

import java.math.BigDecimal;
import java.util.AbstractMap;
import java.util.Collections;
Expand All @@ -27,8 +27,7 @@
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static net.javacrumbs.jsonunit.core.internal.JsonUtils.prettyPrint;

import static java.util.Collections.unmodifiableSet;
import static net.javacrumbs.jsonunit.core.internal.JsonUtils.prettyPrint;


Expand Down Expand Up @@ -225,17 +224,23 @@ interface ValueExtractor {
class JsonMap extends AbstractMap<String, Object> implements NodeWrapper {
private final Node wrappedNode;

private Set<Entry<String, Object>> entrySet;

JsonMap(Node node) {
wrappedNode = node;
}

@NotNull
@Override
public Set<Entry<String, Object>> entrySet() {
Iterator<KeyValue> fields = wrappedNode.fields();
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(fields, 0), false)
.map(keyValue -> new SimpleEntry<>(keyValue.getKey(), keyValue.getValue().getValue()))
.collect(Collectors.toSet());
if (entrySet == null) {
Iterator<KeyValue> fields = wrappedNode.fields();
entrySet = unmodifiableSet(StreamSupport.stream(Spliterators.spliteratorUnknownSize(fields, 0), false)
.map(keyValue -> new SimpleEntry<>(
keyValue.getKey(), keyValue.getValue().getValue()))
.collect(Collectors.toSet()));
}
return entrySet;
}

@Override
Expand Down

0 comments on commit 203d575

Please sign in to comment.