Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. package com.iddqd.doto.optimization;
  2.  
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import org.json.simple.JSONArray;
  5. import org.json.simple.JSONObject;
  6. import org.json.simple.parser.JSONParser;
  7.  
  8. import java.util.Set;
  9.  
  10. public class BandWidthOptimizer {
  11.  
  12. private ObjectMapper mapper = new ObjectMapper();
  13. private JSONParser parser = new JSONParser();
  14. private String lastJSON = "{}";
  15.  
  16. private String[] preserveKeys;
  17.  
  18. public BandWidthOptimizer() {
  19. this.preserveKeys = new String[0];
  20. }
  21.  
  22. public BandWidthOptimizer(String[] preserveKeys) {
  23. this.preserveKeys = preserveKeys;
  24. }
  25.  
  26. public String optimize(Object obj) throws Exception {
  27. String json = mapper.writeValueAsString(obj);
  28. Object nobj = parser.parse(json);
  29. Object oobj = parser.parse(lastJSON);
  30.  
  31. JSONObject newJsonObj = (JSONObject)nobj;
  32. JSONObject oldJsonObj = (JSONObject)oobj;
  33.  
  34. JSONObject res = getJSONObjectDiff(newJsonObj, oldJsonObj);
  35.  
  36. lastJSON = json;
  37.  
  38. return res.toJSONString();
  39. }
  40.  
  41. private JSONObject getJSONObjectDiff(JSONObject obj1, JSONObject obj2) {
  42. JSONObject res = new JSONObject();
  43. Set set = obj1.keySet();
  44.  
  45. for (Object key : set) {
  46. // If doesn't exist put it in the diff
  47. if (!obj2.containsKey(key)) {
  48. res.put(key, obj1.get(key));
  49. } else {
  50. // Get the values from both objects
  51. Object val1 = obj1.get(key);
  52. Object val2 = obj2.get(key);
  53. // If their instances are of the same type
  54. if(val1 == null) {
  55. continue;
  56. }
  57. if(val2 == null) {
  58. res.put(key, val1);
  59. continue;
  60. }
  61. if (val1.getClass().equals(val2.getClass())) {
  62. // If they are JSONObject
  63. if (val1 instanceof JSONObject) {
  64. // Recursively parse JSONObject with all of it's properties
  65. JSONObject nested = getJSONObjectDiff((JSONObject) obj1.get(key), (JSONObject) obj2.get(key));
  66. // If it contains any keys
  67. if(nested.keySet().size() > 0) {
  68. // Store the diff into final diff
  69. res.put(key, nested);
  70. }
  71. // If they are JSONArrays
  72. } else if (val1 instanceof JSONArray) {
  73. // If val1 contains some values (is not empty)
  74. if(((JSONArray) val1).size() > 0) {
  75. // Get their diff
  76. JSONArray arr = getJSONArrayDiff((JSONArray) val1, (JSONArray) val2);
  77. // If array is not empty
  78. if (arr.size() > 0) {
  79. // put it into the diff
  80. res.put(key, arr);
  81. }
  82. }
  83. // If they are just a pure values
  84. } else {
  85. // Compare them - If they're not equal
  86. if(!val1.equals(val2)) {
  87. // put the val1 into diff
  88. res.put(key, val1);
  89. }
  90. }
  91. } else {
  92. res.put(key, val1);
  93. }
  94. }
  95. }
  96. return res;
  97. }
  98.  
  99. private JSONArray getJSONArrayDiff(JSONArray arr1, JSONArray arr2) {
  100.  
  101. JSONArray res = new JSONArray();
  102.  
  103. // For every element
  104. for(int i = 0; i < arr1.size(); i++) {
  105. Object val1 = arr1.get(i);
  106. // If i is out of arr2 bounds
  107. if(i > arr2.size()) {
  108. // put the arr1 item into the diff
  109. res.add(val1);
  110. }
  111.  
  112. Object val2 = arr2.get(i);
  113.  
  114. if(val1 == null) {
  115. continue;
  116. }
  117.  
  118. if(val2 == null) {
  119. res.add(val1);
  120. continue;
  121. }
  122.  
  123. // If their types are equal
  124. if(val1.getClass().equals(val2.getClass())) {
  125. // If they are JSONObjects
  126. if(val1 instanceof JSONObject) {
  127. // Get their diff
  128. JSONObject obj = getJSONObjectDiff((JSONObject) val1, (JSONObject) val2);
  129. // If it contains any keys
  130. if(obj.keySet().size() > 0) {
  131. // Store the diff into final diff
  132. res.add(obj);
  133. }
  134. // If they are JSONArrays
  135. } else if (val1 instanceof JSONArray) {
  136. // Get their diff
  137. JSONArray arr = getJSONArrayDiff((JSONArray) val1, (JSONArray) val2);
  138. // If array is not empty
  139. if(arr.size() > 0) {
  140. // put it into the diff
  141. res.add(arr);
  142. }
  143. // If they are just a pure values
  144. } else {
  145. // Compare them - If they're not equal
  146. if(val1 != val2) {
  147. // add the val1 into diff
  148. res.add(val1);
  149. }
  150. }
  151. } else {
  152. res.add(val1);
  153. }
  154. }
  155. return res;
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement