Strings { public static String abbreviateMiddle(String string, int maxLength) { if (string == null || maxLength >= string.length()) { return string; } if (maxLength == 0) { return ""; } if (maxLength <= ELLIPSIS.length()) { return string.substring(0, maxLength); } int targetSting = max(1, maxLength - ELLIPSIS.length()); int startOffset = targetSting / 2 + targetSting % 2; int endOffset = string.length() - targetSting / 2; return string.substring(0, startOffset) + ELLIPSIS + string.substring(endOffset); } private  Strings(); static String abbreviate(String str, int offset, int maxWidth); static String abbreviateMiddle(String string, int maxLength); static String abbreviateFqClassName(String fqcn); static String capitalize(String str); static String substringAfterLast(String str, String separator); static String strip(String str, String stripChars); static String stripStart(String str, String stripChars); static String stripEnd(String str, String stripChars); static String getParent(String path); static String getDomain(String url); static String sanitize(String str);  }
@Test public void abbreviateMiddle() throws Exception { assertNull(Strings.abbreviateMiddle(null, 0)); assertNull(Strings.abbreviateMiddle(null, 1)); assertNull(Strings.abbreviateMiddle(null, 100)); assertEquals("", Strings.abbreviateMiddle("", 0)); assertEquals("", Strings.abbreviateMiddle("", 1)); assertEquals("", Strings.abbreviateMiddle("", 100)); assertEquals("", Strings.abbreviateMiddle("foo", 0)); assertEquals("f", Strings.abbreviateMiddle("foo", 1)); assertEquals("fo", Strings.abbreviateMiddle("foo", 2)); assertEquals("foo", Strings.abbreviateMiddle("foo", 3)); assertEquals("foo", Strings.abbreviateMiddle("foo", 4)); assertEquals("foo", Strings.abbreviateMiddle("foo", 100)); assertEquals("", Strings.abbreviateMiddle("foobar", 0)); assertEquals("f", Strings.abbreviateMiddle("foobar", 1)); assertEquals("fo", Strings.abbreviateMiddle("foobar", 2)); assertEquals("foo", Strings.abbreviateMiddle("foobar", 3)); assertEquals("f...", Strings.abbreviateMiddle("foobar", 4)); assertEquals("f...r", Strings.abbreviateMiddle("foobar", 5)); assertEquals("foobar", Strings.abbreviateMiddle("foobar", 6)); assertEquals("foobar", Strings.abbreviateMiddle("foobar", 7)); assertEquals("foobar", Strings.abbreviateMiddle("foobar", 100)); assertEquals("", Strings.abbreviateMiddle("Lorem ipsum dolor sit amet", 0)); assertEquals("L", Strings.abbreviateMiddle("Lorem ipsum dolor sit amet", 1)); assertEquals("Lore...met", Strings.abbreviateMiddle("Lorem ipsum dolor sit amet", 10)); assertEquals("Lorem ipsum dolor sit amet", Strings.abbreviateMiddle("Lorem ipsum dolor sit amet", 26)); assertEquals("Lorem ipsum dolor sit amet", Strings.abbreviateMiddle("Lorem ipsum dolor sit amet", 100)); }