forked from MCZbase/DataShot_DesktopApp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove duplicate results from Nahima before choosing best one
- Loading branch information
Showing
3 changed files
with
89 additions
and
6 deletions.
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
61 changes: 61 additions & 0 deletions
61
src/main/java/edu/harvard/mcz/imagecapture/utility/JSONUtility.java
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package edu.harvard.mcz.imagecapture.utility; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
import java.util.Iterator; | ||
|
||
public class JSONUtility { | ||
|
||
public static boolean areEqualIgnoringUnderscore(JSONArray a1, JSONArray a2) { | ||
if (a1.length() != a2.length()) { | ||
return false; | ||
} | ||
for (int i = 0; i < a1.length(); i++) { | ||
Object obj1 = a1.get(i); | ||
Object obj2 = a2.get(i); | ||
if (!objectsAreEqual(obj1, obj2)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public static boolean areEqualIgnoringUnderscore(JSONObject obj1, JSONObject obj2) { | ||
return areEqualIgnoringUnderscore(obj1, obj2, false); | ||
} | ||
|
||
private static boolean areEqualIgnoringUnderscore(@NotNull JSONObject obj1, JSONObject obj2, boolean inverted) { | ||
Iterator<String> keys = obj1.keys(); | ||
while (keys.hasNext()) { | ||
String key = keys.next(); | ||
if (key.startsWith("_")) { | ||
continue; | ||
} | ||
if (!obj2.has(key)) { | ||
return false; | ||
} | ||
Object val1 = obj1.get(key); | ||
Object val2 = obj2.get(key); | ||
if (!objectsAreEqual(val1, val2)) { | ||
return false; | ||
} | ||
} | ||
|
||
if (inverted) { | ||
return true; | ||
} else { | ||
return areEqualIgnoringUnderscore(obj2, obj1, true); | ||
} | ||
} | ||
|
||
private static boolean objectsAreEqual(Object val1, Object val2) { | ||
if (val1 instanceof JSONObject && val2 instanceof JSONObject) { | ||
return areEqualIgnoringUnderscore((JSONObject) val1, (JSONObject) val2); | ||
} else if (val1 instanceof JSONArray && val2 instanceof JSONArray) { | ||
return areEqualIgnoringUnderscore((JSONArray) val1, (JSONArray) val2); | ||
} //else { | ||
return val1.equals(val2); | ||
} | ||
} |