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;
    }
@Test
    public void testTruncation() {
        String a = "abbc";
        assertSame(a, StringUtilities.truncateSequencesIfNecessary(a, 2));
        assertNotSame(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));
    }