@Test public void testChars2() { StringGrouper token = new StringGrouper(new char[] { '\'' }, new int[] { 1 }); Assert.assertEquals("1'2'3'4'5'6'7'8'9", token.group("123456789")); token = new StringGrouper(new char[] { '-','-',',','-','-','-','-','-','-','-','-' }, new int[] { 1 }); Assert.assertEquals("1-2-3-4-5-6,7-8-9", token.group("123456789")); token = new StringGrouper(new char[] { '\'','x',',' }, new int[] { 1 }); Assert.assertEquals("1,2,3,4,5,6,7x8'9", token.group("123456789")); }
public String group(String input) { if (groupSizes.length == 0 || groupCharacters.length == 0) { return input; } if (reverse) { return formatInternalReverse(input); } int groupIndex = 0; int sizeIndex = 0; char groupChar = groupCharacters[groupIndex]; int groupSize = groupSizes[sizeIndex]; if (groupSize <= 0) { return input; } char[] group = new char[groupSize]; int pos = input.length(); StringBuilder result = new StringBuilder(input.length() + 4); while (pos > 0) { if (groupSize == 0) { return input; } int start = pos - groupSize; if (!(result.length() == 0)) { result.insert(0, groupChar); if (groupIndex < (groupCharacters.length - 1)) { groupChar = groupCharacters[++groupIndex]; } } if (start >= 0) { input.getChars(start, pos, group, 0); result.insert(0, group, 0, groupSize); pos -= groupSize; if (sizeIndex < (groupSizes.length - 1)) { groupSize = groupSizes[++sizeIndex]; if (groupSize > group.length) { group = new char[groupSize]; } } } else { input.getChars(0, pos, group, 0); result.insert(0, group, 0, pos); break; } } return result.toString(); }
StringGrouper { public String group(String input) { if (groupSizes.length == 0 || groupCharacters.length == 0) { return input; } if (reverse) { return formatInternalReverse(input); } int groupIndex = 0; int sizeIndex = 0; char groupChar = groupCharacters[groupIndex]; int groupSize = groupSizes[sizeIndex]; if (groupSize <= 0) { return input; } char[] group = new char[groupSize]; int pos = input.length(); StringBuilder result = new StringBuilder(input.length() + 4); while (pos > 0) { if (groupSize == 0) { return input; } int start = pos - groupSize; if (!(result.length() == 0)) { result.insert(0, groupChar); if (groupIndex < (groupCharacters.length - 1)) { groupChar = groupCharacters[++groupIndex]; } } if (start >= 0) { input.getChars(start, pos, group, 0); result.insert(0, group, 0, groupSize); pos -= groupSize; if (sizeIndex < (groupSizes.length - 1)) { groupSize = groupSizes[++sizeIndex]; if (groupSize > group.length) { group = new char[groupSize]; } } } else { input.getChars(0, pos, group, 0); result.insert(0, group, 0, pos); break; } } return result.toString(); } }
StringGrouper { public String group(String input) { if (groupSizes.length == 0 || groupCharacters.length == 0) { return input; } if (reverse) { return formatInternalReverse(input); } int groupIndex = 0; int sizeIndex = 0; char groupChar = groupCharacters[groupIndex]; int groupSize = groupSizes[sizeIndex]; if (groupSize <= 0) { return input; } char[] group = new char[groupSize]; int pos = input.length(); StringBuilder result = new StringBuilder(input.length() + 4); while (pos > 0) { if (groupSize == 0) { return input; } int start = pos - groupSize; if (!(result.length() == 0)) { result.insert(0, groupChar); if (groupIndex < (groupCharacters.length - 1)) { groupChar = groupCharacters[++groupIndex]; } } if (start >= 0) { input.getChars(start, pos, group, 0); result.insert(0, group, 0, groupSize); pos -= groupSize; if (sizeIndex < (groupSizes.length - 1)) { groupSize = groupSizes[++sizeIndex]; if (groupSize > group.length) { group = new char[groupSize]; } } } else { input.getChars(0, pos, group, 0); result.insert(0, group, 0, pos); break; } } return result.toString(); } StringGrouper();  StringGrouper(char... groupCharacters);  StringGrouper(char groupCharacter, int... groupSizes);  StringGrouper(char[] groupCharacters, int... groupSizes); }
StringGrouper { public String group(String input) { if (groupSizes.length == 0 || groupCharacters.length == 0) { return input; } if (reverse) { return formatInternalReverse(input); } int groupIndex = 0; int sizeIndex = 0; char groupChar = groupCharacters[groupIndex]; int groupSize = groupSizes[sizeIndex]; if (groupSize <= 0) { return input; } char[] group = new char[groupSize]; int pos = input.length(); StringBuilder result = new StringBuilder(input.length() + 4); while (pos > 0) { if (groupSize == 0) { return input; } int start = pos - groupSize; if (!(result.length() == 0)) { result.insert(0, groupChar); if (groupIndex < (groupCharacters.length - 1)) { groupChar = groupCharacters[++groupIndex]; } } if (start >= 0) { input.getChars(start, pos, group, 0); result.insert(0, group, 0, groupSize); pos -= groupSize; if (sizeIndex < (groupSizes.length - 1)) { groupSize = groupSizes[++sizeIndex]; if (groupSize > group.length) { group = new char[groupSize]; } } } else { input.getChars(0, pos, group, 0); result.insert(0, group, 0, pos); break; } } return result.toString(); } StringGrouper();  StringGrouper(char... groupCharacters);  StringGrouper(char groupCharacter, int... groupSizes);  StringGrouper(char[] groupCharacters, int... groupSizes); StringGrouper setGroupChars(char... groupCharacters); char[] getGroupChars(); int[] getGroupSizes(); StringGrouper setGroupSizes(int... groupSizes); StringGrouper setReverse(boolean reverse); boolean isReverse(); String group(String input); }
StringGrouper { public String group(String input) { if (groupSizes.length == 0 || groupCharacters.length == 0) { return input; } if (reverse) { return formatInternalReverse(input); } int groupIndex = 0; int sizeIndex = 0; char groupChar = groupCharacters[groupIndex]; int groupSize = groupSizes[sizeIndex]; if (groupSize <= 0) { return input; } char[] group = new char[groupSize]; int pos = input.length(); StringBuilder result = new StringBuilder(input.length() + 4); while (pos > 0) { if (groupSize == 0) { return input; } int start = pos - groupSize; if (!(result.length() == 0)) { result.insert(0, groupChar); if (groupIndex < (groupCharacters.length - 1)) { groupChar = groupCharacters[++groupIndex]; } } if (start >= 0) { input.getChars(start, pos, group, 0); result.insert(0, group, 0, groupSize); pos -= groupSize; if (sizeIndex < (groupSizes.length - 1)) { groupSize = groupSizes[++sizeIndex]; if (groupSize > group.length) { group = new char[groupSize]; } } } else { input.getChars(0, pos, group, 0); result.insert(0, group, 0, pos); break; } } return result.toString(); } StringGrouper();  StringGrouper(char... groupCharacters);  StringGrouper(char groupCharacter, int... groupSizes);  StringGrouper(char[] groupCharacters, int... groupSizes); StringGrouper setGroupChars(char... groupCharacters); char[] getGroupChars(); int[] getGroupSizes(); StringGrouper setGroupSizes(int... groupSizes); StringGrouper setReverse(boolean reverse); boolean isReverse(); String group(String input);  }