SentryUtils { public static String collapseNumsToString(List<Long> nums) { List<String> collapsedStrings = new ArrayList<>(); int startIndex = 0; int movingIndex = 0; int currentGroupSize = 0; int listSize = nums.size(); while (startIndex < listSize) { if (movingIndex == listSize || nums.get(startIndex) + currentGroupSize != nums.get(movingIndex)) { long startID = nums.get(startIndex); long endID = nums.get(movingIndex - 1); if (startID == endID) { collapsedStrings.add(String.format("%d", startID)); } else if (endID - startID == 1) { collapsedStrings.add(String.format("%d", startID)); collapsedStrings.add(String.format("%d", endID)); } else { collapsedStrings.add(String.format("%d-%d", startID, endID)); } startIndex = movingIndex; currentGroupSize = 0; continue; } movingIndex++; currentGroupSize++; } return collapsedStrings.toString(); }  static String collapseNumsToString(List<Long> nums); static boolean isNULL(String s);  }
@Test public void testCollapseNumsToString() throws Exception { assertEquals("Collapsed string should match", SentryUtils.collapseNumsToString(Arrays.asList(1L)), "[1]"); assertEquals("Collapsed string should match", SentryUtils.collapseNumsToString(Arrays.asList(1L, 2L)), "[1, 2]"); assertEquals("Collapsed string should match", SentryUtils.collapseNumsToString(Arrays.asList(1L, 2L, 4L)), "[1, 2, 4]"); assertEquals("Collapsed string should match", SentryUtils.collapseNumsToString(Arrays.asList(1L, 2L, 4L, 5L)), "[1, 2, 4, 5]"); assertEquals("Collapsed string should match", SentryUtils.collapseNumsToString(Arrays.asList(1L, 2L, 4L, 5L, 6L)), "[1, 2, 4-6]"); assertEquals("Collapsed string should match", SentryUtils.collapseNumsToString(Arrays.asList(1L, 2L, 4L, 5L, 6L, 8L)), "[1, 2, 4-6, 8]"); assertEquals("Collapsed string should just return empty string", SentryUtils.collapseNumsToString(Collections.<Long>emptyList()), "[]"); }