Skip to content

Commit 9a62815

Browse files
author
Tamas Balog
committed
Extract some logic to methods in DefaultComparator to improve readability
1 parent c324cff commit 9a62815

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package org.skyscreamer.jsonassert.comparator;
22

3+
import static org.skyscreamer.jsonassert.comparator.JSONCompareUtil.allJSONObjects;
4+
import static org.skyscreamer.jsonassert.comparator.JSONCompareUtil.allSimpleValues;
5+
36
import org.json.JSONArray;
47
import org.json.JSONException;
58
import org.json.JSONObject;
69
import org.skyscreamer.jsonassert.JSONCompareMode;
710
import org.skyscreamer.jsonassert.JSONCompareResult;
811

9-
import static org.skyscreamer.jsonassert.comparator.JSONCompareUtil.allJSONObjects;
10-
import static org.skyscreamer.jsonassert.comparator.JSONCompareUtil.allSimpleValues;
11-
1212
/**
1313
* This class is the default json comparator implementation. <p/>
14-
* Comparison is performed according to {@link JSONCompareMode} that is passed as constructor's argument.
14+
* Comparison is performed according to {@link JSONCompareMode} that is passed as constructor's argument.
1515
*/
1616
public class DefaultComparator extends AbstractComparator {
1717

@@ -36,8 +36,8 @@ public void compareJSON(String prefix, JSONObject expected, JSONObject actual, J
3636
@Override
3737
public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result)
3838
throws JSONException {
39-
if (expectedValue instanceof Number && actualValue instanceof Number) {
40-
if (((Number)expectedValue).doubleValue() != ((Number)actualValue).doubleValue()) {
39+
if (areNumbers(expectedValue, actualValue)) {
40+
if (areSameDoubles(expectedValue, actualValue)) {
4141
result.fail(prefix, expectedValue, actualValue);
4242
}
4343
} else if (expectedValue.getClass().isAssignableFrom(actualValue.getClass())) {
@@ -74,4 +74,12 @@ public void compareJSONArray(String prefix, JSONArray expected, JSONArray actual
7474
recursivelyCompareJSONArray(prefix, expected, actual, result);
7575
}
7676
}
77+
78+
private boolean areNumbers(Object expectedValue, Object actualValue) {
79+
return expectedValue instanceof Number && actualValue instanceof Number;
80+
}
81+
82+
private boolean areSameDoubles(Object expectedValue, Object actualValue) {
83+
return ((Number) expectedValue).doubleValue() != ((Number) actualValue).doubleValue();
84+
}
7785
}

0 commit comments

Comments
 (0)