CharSequences { public static Iterable<CharSequence> generateSuffixes(final CharSequence input) { return new Iterable<CharSequence>() { @Override public Iterator<CharSequence> iterator() { return new Iterator<CharSequence>() { int currentIndex = 0; @Override public boolean hasNext() { return currentIndex < input.length(); } @Override public CharSequence next() { return input.subSequence(currentIndex++, input.length()); } @Override public void remove() { throw new UnsupportedOperationException("Modification not supported"); } }; } }; } CharSequences(); static Iterable<CharSequence> generateSuffixes(final CharSequence input); static Iterable<CharSequence> generatePrefixes(final CharSequence input); static CharSequence getCommonPrefix(CharSequence first, CharSequence second); static CharSequence getSuffix(CharSequence input, int startIndex); static CharSequence getPrefix(CharSequence input, int endIndex); static CharSequence subtractPrefix(CharSequence main, CharSequence prefix); static CharSequence concatenate(final CharSequence first, final CharSequence second); static CharSequence reverse(CharSequence input); static CharSequence fromCharArray(final char[] characters); static char[] toCharArray(CharSequence charSequence); static String toString(CharSequence charSequence);  }
@Test public void testGenerateSuffixes() throws Exception { final CharSequence input = "BANANAS"; List<CharSequence> expected = Arrays.<CharSequence>asList("BANANAS", "ANANAS", "NANAS", "ANAS", "NAS", "AS", "S"); int index = 0; for (CharSequence suffix : CharSequences.generateSuffixes(input)) { Assert.assertEquals(expected.get(index++), suffix); } }
@Test(expected = UnsupportedOperationException.class) public void testGenerateSuffixes_IteratorRemove() throws Exception { final CharSequence input = "BANANAS"; Iterator<CharSequence> iterator = CharSequences.generateSuffixes(input).iterator(); iterator.next(); iterator.remove(); }