@Test public void testLogNumberArrayInRange() throws Exception { List<Integer> input = Arrays.asList(1, 3, 6, 4, 7, 12, 9, 10, 8); Assert.assertEquals(LogUtils.logNumberArrayInRange(input), "[1, 3-4, 6-10, 12]"); input = Arrays.asList(3, 1, 4, 6, 7, 8, 9, 9, 9, 10, 11, 12); Assert.assertEquals(LogUtils.logNumberArrayInRange(input), "[1, 3-4, 6-12]"); Assert.assertEquals(input.get(0).intValue(), 3); input = Arrays.asList(1, 5, 6, 4, 2, 3); Assert.assertEquals(LogUtils.logNumberArrayInRange(input), "[1-6]"); Assert.assertEquals(LogUtils.logNumberArrayInRange(null), "[]"); }
public static String logNumberArrayInRange(List<Integer> list) { if (list == null || list.isEmpty()) { return "[]"; } try { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("["); List<Integer> copiedList = new ArrayList<>(list); copiedList.sort(Integer::compareTo); int curStart = copiedList.get(0); int curTail = curStart; for (int i = 1; i < copiedList.size(); i++) { int num = copiedList.get(i); if (num <= curTail + 1) { curTail = num; } else { printNumberRange(stringBuilder, curStart, curTail); stringBuilder.append(", "); curStart = num; curTail = num; } } printNumberRange(stringBuilder, curStart, curTail); stringBuilder.append("]"); return stringBuilder.toString(); } catch (Exception e) { LOG.error("Failed to generate string for the int list in range", e); return list.toString(); } }
LogUtils { public static String logNumberArrayInRange(List<Integer> list) { if (list == null || list.isEmpty()) { return "[]"; } try { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("["); List<Integer> copiedList = new ArrayList<>(list); copiedList.sort(Integer::compareTo); int curStart = copiedList.get(0); int curTail = curStart; for (int i = 1; i < copiedList.size(); i++) { int num = copiedList.get(i); if (num <= curTail + 1) { curTail = num; } else { printNumberRange(stringBuilder, curStart, curTail); stringBuilder.append(", "); curStart = num; curTail = num; } } printNumberRange(stringBuilder, curStart, curTail); stringBuilder.append("]"); return stringBuilder.toString(); } catch (Exception e) { LOG.error("Failed to generate string for the int list in range", e); return list.toString(); } } }
LogUtils { public static String logNumberArrayInRange(List<Integer> list) { if (list == null || list.isEmpty()) { return "[]"; } try { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("["); List<Integer> copiedList = new ArrayList<>(list); copiedList.sort(Integer::compareTo); int curStart = copiedList.get(0); int curTail = curStart; for (int i = 1; i < copiedList.size(); i++) { int num = copiedList.get(i); if (num <= curTail + 1) { curTail = num; } else { printNumberRange(stringBuilder, curStart, curTail); stringBuilder.append(", "); curStart = num; curTail = num; } } printNumberRange(stringBuilder, curStart, curTail); stringBuilder.append("]"); return stringBuilder.toString(); } catch (Exception e) { LOG.error("Failed to generate string for the int list in range", e); return list.toString(); } }  }
LogUtils { public static String logNumberArrayInRange(List<Integer> list) { if (list == null || list.isEmpty()) { return "[]"; } try { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("["); List<Integer> copiedList = new ArrayList<>(list); copiedList.sort(Integer::compareTo); int curStart = copiedList.get(0); int curTail = curStart; for (int i = 1; i < copiedList.size(); i++) { int num = copiedList.get(i); if (num <= curTail + 1) { curTail = num; } else { printNumberRange(stringBuilder, curStart, curTail); stringBuilder.append(", "); curStart = num; curTail = num; } } printNumberRange(stringBuilder, curStart, curTail); stringBuilder.append("]"); return stringBuilder.toString(); } catch (Exception e) { LOG.error("Failed to generate string for the int list in range", e); return list.toString(); } }  static String logNumberArrayInRange(List<Integer> list); }
LogUtils { public static String logNumberArrayInRange(List<Integer> list) { if (list == null || list.isEmpty()) { return "[]"; } try { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("["); List<Integer> copiedList = new ArrayList<>(list); copiedList.sort(Integer::compareTo); int curStart = copiedList.get(0); int curTail = curStart; for (int i = 1; i < copiedList.size(); i++) { int num = copiedList.get(i); if (num <= curTail + 1) { curTail = num; } else { printNumberRange(stringBuilder, curStart, curTail); stringBuilder.append(", "); curStart = num; curTail = num; } } printNumberRange(stringBuilder, curStart, curTail); stringBuilder.append("]"); return stringBuilder.toString(); } catch (Exception e) { LOG.error("Failed to generate string for the int list in range", e); return list.toString(); } }  static String logNumberArrayInRange(List<Integer> list);  }