From 70b6e9e8b414c9ae2159f7b87e245bc3ff0e0a88 Mon Sep 17 00:00:00 2001 From: Lukas Krecan Date: Wed, 26 Jun 2024 19:31:07 +0200 Subject: [PATCH] Lazy initialization of JsonMap --- .../core/internal/ComparisonMatrix.java | 7 +++-- .../jsonunit/core/internal/Node.java | 28 +++++++++++++------ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/ComparisonMatrix.java b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/ComparisonMatrix.java index 6f5c0c7f8..1c0a9cc01 100644 --- a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/ComparisonMatrix.java +++ b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/ComparisonMatrix.java @@ -86,8 +86,9 @@ ComparisonMatrix compare() { List matches = getEqualValues(i); if (matches.size() == 1) { recordMatch(i, matches.get(0)); - } else if (matches.size() > 0) { - // we have more matches, since comparison does not have to be transitive ([1, 2] == [2] == [2, 3]), we have to check all the possibilities + } else if (!matches.isEmpty()) { + // we have more matches, since comparison does not have to be transitive ([1, 2] == [2] == [2, 3]), + // we have to check all the possibilities for (int match : matches) { ComparisonMatrix copy = copy(i + 1); copy.recordMatch(i, match); @@ -114,7 +115,7 @@ private void doSimpleMatching() { for (int i = 0; i < equalElements.size(); i++) { if (!alreadyMatched.get(i)) { List equalTo = equalElements.get(i); - if (equalTo.size() > 0) { + if (!equalTo.isEmpty()) { List equivalentElements = getEquivalentElements(equalTo); // We have the same set matching as is equivalent, we can remove them all diff --git a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/Node.java b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/Node.java index a400b9b55..95a641794 100644 --- a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/Node.java +++ b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/Node.java @@ -15,11 +15,19 @@ */ package net.javacrumbs.jsonunit.core.internal; + +import org.jetbrains.annotations.NotNull; import java.math.BigDecimal; +import java.util.AbstractMap; import java.util.Collections; import java.util.Iterator; -import java.util.LinkedHashMap; import java.util.LinkedList; +import java.util.Set; +import java.util.Spliterators; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +import static net.javacrumbs.jsonunit.core.internal.JsonUtils.prettyPrint; import static net.javacrumbs.jsonunit.core.internal.JsonUtils.prettyPrint; @@ -33,7 +41,7 @@ enum NodeType implements ValueExtractor { OBJECT("object") { @Override public Object getValue(Node node) { - // custom conversion to map. We want be consistent and native mapping may have different rules for + // custom conversion to map. We want to be consistent and native mapping may have different rules for // serializing numbers, dates etc. return new JsonMap(node); } @@ -214,16 +222,20 @@ interface ValueExtractor { Object getValue(Node node); } - class JsonMap extends LinkedHashMap implements NodeWrapper { + class JsonMap extends AbstractMap implements NodeWrapper { private final Node wrappedNode; JsonMap(Node node) { wrappedNode = node; - Iterator fields = node.fields(); - while (fields.hasNext()) { - KeyValue keyValue = fields.next(); - put(keyValue.getKey(), keyValue.getValue().getValue()); - } + } + + @NotNull + @Override + public Set> entrySet() { + Iterator fields = wrappedNode.fields(); + return StreamSupport.stream(Spliterators.spliteratorUnknownSize(fields, 0), false) + .map(keyValue -> new SimpleEntry<>(keyValue.getKey(), keyValue.getValue().getValue())) + .collect(Collectors.toSet()); } @Override