StringUtilities { public static String truncateSequencesIfNecessary(String text, int maxConsecutiveLength) { char prev = 0; int sequenceCount = 1; for (int i = 0, m = text.length(); i < m ; i++) { char curr = text.charAt(i); if (prev == curr) { sequenceCount++; if (sequenceCount > maxConsecutiveLength) { return truncateSequences(text, maxConsecutiveLength, i); } } else { sequenceCount = 1; prev = curr; } } return text; }  static String escape(String source); static String escape(String source, char delimiter); static String unescape(String source); static String implode(String[] array, String sepString); static String implodeMultiline(List<String> lines); static String truncateSequencesIfNecessary(String text, int maxConsecutiveLength); static String stripSuffix(String string, String suffix); static String quote(Object object); static Set<String> split(String s);  }
@Test public void testTruncation() { String a = "abbc"; assertTrue(a == StringUtilities.truncateSequencesIfNecessary(a, 2)); assertTrue(a != StringUtilities.truncateSequencesIfNecessary(a, 1)); assertEquals("abc", StringUtilities.truncateSequencesIfNecessary(a, 1)); assertEquals("abc", StringUtilities.truncateSequencesIfNecessary("aabbccc", 1)); assertEquals("abc", StringUtilities.truncateSequencesIfNecessary("abcc", 1)); assertEquals("abc", StringUtilities.truncateSequencesIfNecessary("aabc", 1)); assertEquals("abcb", StringUtilities.truncateSequencesIfNecessary("abcb", 1)); assertEquals("g g g g g g g g g g\n g g g g g g g g g g\n g g g g g g g g g g", StringUtilities.truncateSequencesIfNecessary("g g g g g g g g g g\n g g g g g g g g g g\n g g g g g g g g g g", 5)); }