Skip to content

Commit

Permalink
Lazy initialization of JsonMap
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed Jul 1, 2024
1 parent a4ce2aa commit 70b6e9e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ ComparisonMatrix compare() {
List<Integer> 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);
Expand All @@ -114,7 +115,7 @@ private void doSimpleMatching() {
for (int i = 0; i < equalElements.size(); i++) {
if (!alreadyMatched.get(i)) {
List<Integer> equalTo = equalElements.get(i);
if (equalTo.size() > 0) {
if (!equalTo.isEmpty()) {
List<Integer> equivalentElements = getEquivalentElements(equalTo);

// We have the same set matching as is equivalent, we can remove them all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down Expand Up @@ -214,16 +222,20 @@ interface ValueExtractor {
Object getValue(Node node);
}

class JsonMap extends LinkedHashMap<String, Object> implements NodeWrapper {
class JsonMap extends AbstractMap<String, Object> implements NodeWrapper {
private final Node wrappedNode;

JsonMap(Node node) {
wrappedNode = node;
Iterator<KeyValue> fields = node.fields();
while (fields.hasNext()) {
KeyValue keyValue = fields.next();
put(keyValue.getKey(), keyValue.getValue().getValue());
}
}

@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());
}

@Override
Expand Down

0 comments on commit 70b6e9e

Please sign in to comment.