JSONUtils { public static JSONArray splitJsonArray(int chunkSize, JSONArray nodes) { JSONArray nodeGroups = new JSONArray(); if (nodes == null) { return nodeGroups; } if (chunkSize <= 0 ) { nodeGroups.put(0, nodes); return nodeGroups; } int numberOfChunks = (int)Math.ceil((double)nodes.length()/chunkSize); for (int i = 0; i < numberOfChunks; i++) { int start = i * chunkSize; int tmpLength = Math.min(nodes.length() - start, chunkSize); JSONArray tmpArray = new JSONArray(); int tmpCount = 0; for (int x = start; x < (start+tmpLength); x++) { tmpArray.put(tmpCount++,nodes.get(x)); } nodeGroups.put(i, tmpArray); } return nodeGroups; }  static JSONObject diff(JSONObject prev, JSONObject curr); static JSONObject apply(JSONObject json, JSONObject patch); static JSONArray splitJsonArray(int chunkSize, JSONArray nodes); static void keyChange(Map<String, Object> map, String regex, String replace); static ArrayList toArrayList(Collection collection); static Map toMap(JSONObject input); static ArrayList toArray(JSONArray jsonArray); static JSONArray toJson(Set set); static JSONObject toJson(Map map); static void main(String[] args);  }
@Test public void testSplitJsonArrayTest() { JSONArray nodes = new JSONArray(); nodes.put(1);nodes.put(2);nodes.put(3);nodes.put(4);nodes.put(5);nodes.put(6);nodes.put(7); System.out.println("Original nodes ="+nodes.toString()); JSONArray result1 = JSONUtils.splitJsonArray(3, nodes); assertEquals(result1.get(0).toString(), "[1,2,3]"); assertEquals(result1.get(1).toString(), "[4,5,6]"); assertEquals(result1.get(2).toString(), "[7]"); assertEquals(result1.length(),3); System.out.println(result1.toString()); JSONArray result2 = JSONUtils.splitJsonArray(0, nodes); assertEquals(result2.length(),1); assertEquals(result2.get(0).toString(), "[1,2,3,4,5,6,7]"); JSONArray nodesEven = new JSONArray(); nodesEven.put(1);nodesEven.put(2);nodesEven.put(3);nodesEven.put(4);nodesEven.put(5);nodesEven.put(6); JSONArray result3 = JSONUtils.splitJsonArray(3, nodesEven); assertEquals(result3.length(),2); assertEquals(result3.get(0).toString(), "[1,2,3]"); assertEquals(result3.get(1).toString(), "[4,5,6]"); System.out.println(result3.toString()); JSONArray result4 = JSONUtils.splitJsonArray(3, null); assertEquals(result4.length(),0); System.out.println(result4.toString()); JSONArray jj = new JSONArray(); JSONArray result5 = JSONUtils.splitJsonArray(3, jj); assertEquals(result4.length(),0); System.out.println(result4.toString()); }