PrintUtil { public static String limit(String in, int length) { if (in.length() <= length) { return in; } else { if (length < 5) { return in.substring(0, length); } final int breakpoint = (length - DOTS.length()) / 2; final StringBuilder sb = new StringBuilder(); sb.append(in.substring(0, breakpoint)); sb.append(DOTS); final int suffixLength = length - sb.length(); sb.append(in.substring(in.length() - suffixLength)); return sb.toString(); } } private  PrintUtil(); static String limit(String in, int length); static final String HORIZONTAL_LINE; }
@Test void limitSmall() { assertEquals("a", PrintUtil.limit(CHARS, 1)); }
@Test void limitLarge() { assertEquals(CHARS, PrintUtil.limit(CHARS, CHARS.length())); }
@Test void limit() { assertEquals("abc...wxyz", PrintUtil.limit(CHARS, 10)); assertEquals("abcd...wxyz", PrintUtil.limit(CHARS, 11)); assertEquals("abcd...vwxyz", PrintUtil.limit(CHARS, 12)); }