-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes issue #573 by added specific compare of numeric types
- Loading branch information
John J. Aylward
committed
Nov 19, 2020
1 parent
e4b76d6
commit 11e6b1a
Showing
2 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2073,6 +2073,8 @@ public boolean similar(Object other) { | |
if (!((JSONArray)valueThis).similar(valueOther)) { | ||
return false; | ||
} | ||
} else if (valueThis instanceof Number && valueOther instanceof Number) { | ||
return isNumberSimilar((Number)valueThis, (Number)valueOther); | ||
} else if (!valueThis.equals(valueOther)) { | ||
return false; | ||
} | ||
|
@@ -2083,6 +2085,55 @@ public boolean similar(Object other) { | |
} | ||
} | ||
|
||
/** | ||
* Compares two numbers to see if they are similar. | ||
* | ||
* If either of the numbers are Double or Float instances, then they are checked to have | ||
* a finite value. If either value is not finite (NaN or ±infinity), then this | ||
* function will always return false. If both numbers are finite, they are first checked | ||
* to be the same type and implement {@link Comparable}. If they do, then the actual | ||
* {@link Comparable#compareTo(Object)} is called. If they are not the same type, or don't | ||
* implement Comparable, then they are converted to {@link BigDecimal}s. Finally the | ||
* BigDecimal values are compared using {@link BigDecimal#compareTo(BigDecimal)}. | ||
* | ||
* @param l the Left value to compare. Can not be <code>null</code>. | ||
* @param r the right value to compare. Can not be <code>null</code>. | ||
* @return true if the numbers are similar, false otherwise. | ||
*/ | ||
static boolean isNumberSimilar(Number l, Number r) { | ||
if (!numberIsFinite(l) || !numberIsFinite(r)) { | ||
// non-finite numbers are never similar | ||
return false; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
stleary
Owner
|
||
} | ||
|
||
// if the classes are the same and implement Comparable | ||
// then use the built in compare first. | ||
if(l.getClass().equals(r.getClass()) && l instanceof Comparable) { | ||
@SuppressWarnings({ "rawtypes", "unchecked" }) | ||
int compareTo = ((Comparable)l).compareTo(r); | ||
return compareTo==0; | ||
} | ||
|
||
// BigDecimal should be able to handle all of our number types that we support through | ||
// documentation. Convert to BigDecimal first, then use the Compare method to | ||
// decide equality. | ||
final BigDecimal lBigDecimal = objectToBigDecimal(l, null); | ||
final BigDecimal rBigDecimal = objectToBigDecimal(r, null); | ||
if (lBigDecimal == null || rBigDecimal == null) { | ||
return false; | ||
} | ||
return lBigDecimal.compareTo(rBigDecimal) == 0; | ||
} | ||
|
||
private static boolean numberIsFinite(Number n) { | ||
if (n instanceof Double && (((Double) n).isInfinite() || ((Double) n).isNaN())) { | ||
return false; | ||
} else if (n instanceof Float && (((Float) n).isInfinite() || ((Float) n).isNaN())) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Tests if the value should be tried as a decimal. It makes no test if there are actual digits. | ||
* | ||
|
@@ -2354,7 +2405,7 @@ public static String valueToString(Object value) throws JSONException { | |
*/ | ||
public static Object wrap(Object object) { | ||
try { | ||
if (object == null) { | ||
if (NULL.equals(object)) { | ||
return NULL; | ||
} | ||
if (object instanceof JSONObject || object instanceof JSONArray | ||
|
It may be only one return in method.