From 7fb0c27f40434f41923525c2e3a3aee2a3b3e127 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 | 4 +-- .../jsonunit/core/internal/Node.java | 28 +++++++++++++------ 2 files changed, 21 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 0a67f62f..4fb01907 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 @@ -108,7 +108,7 @@ ComparisonMatrix compare() { List matches = getEqualValues(i); if (matches.size() == 1) { recordMatch(i, matches.get(0)); - } else if (matches.size() > 0) { + } 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) { @@ -137,7 +137,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 9d1fdcf2..6574f5d2 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,13 +15,19 @@ */ package net.javacrumbs.jsonunit.core.internal; -import static net.javacrumbs.jsonunit.core.internal.JsonUtils.prettyPrint; +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; /** * For internal use only!!! Abstract node representation. @@ -32,7 +38,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 +220,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