Acronym { public String transform(String str) { TokenizedString tokenizedString = new TokenizedString(str, delimiterPattern); List<String> tokens = tokenizedString.getTokens(); if (tokens.size() == 0) { return StringUtils.EMPTY; } int start = 0; String firstApplied; do { firstApplied = abbrevMode.apply(tokens.get(start)); start++; } while (firstApplied.isEmpty() && start < tokens.size()); StringBuilder sb = new StringBuilder(firstApplied); if (separator == AcronymSeparator.KEEP_SPECIAL_CHARS) { Pattern specialCharPattern = Pattern.compile(separator.getValue()); List<String> separators = tokenizedString.getSeparators(); int nextSeparator = tokenizedString.isStartingWithSeparator() ? 1 : 0; for (int i = 1; i < tokens.size(); i++) { String chars = abbrevMode.apply(tokens.get(i)); if (!chars.isEmpty()) { sb.append(getSpecialChars(separators.get(nextSeparator), specialCharPattern)).append( abbrevMode.apply(tokens.get(i))); } nextSeparator++; } } else { for (int i = start; i < tokens.size(); i++) { String chars = abbrevMode.apply(tokens.get(i)); if (!chars.isEmpty()) { sb.append(separator.getValue()).append(abbrevMode.apply(tokens.get(i))); } } if (sb.length() > 0 && AcronymSeparator.PERIOD == separator) { sb.append(separator.getValue()); } } return sb.toString(); } private  Acronym(AbbreviationMode abbrevMode, AcronymSeparator separator, String delimiterPattern); AcronymSeparator getSeparator(); AbbreviationMode getAbbrevMode(); String transform(String str); static AcronymBuilder newBuilder();  }
@Test public void transformNullString() { Acronym acronym = buildAcronym(FIRST_LETTERS_IGNORE_NUMERIC, AcronymSeparator.NONE); assertEquals(StringUtils.EMPTY, acronym.transform(null)); }
@Test public void noTokenAppliedWithSeparator() { Acronym acronym = buildAcronym(FIRST_LETTERS_IGNORE_NUMERIC, AcronymSeparator.DASH); assertEquals(StringUtils.EMPTY, acronym.transform("06 01 02 03 04")); }
@Test public void noTokenAppliedKeepSpecialChars() { Acronym acronym = buildAcronym(FIRST_LETTERS_IGNORE_NUMERIC, AcronymSeparator.KEEP_SPECIAL_CHARS); assertEquals(StringUtils.EMPTY, acronym.transform("06.01.02.03.04")); assertEquals(StringUtils.EMPTY, acronym.transform("06-01-02-03-04")); }
@Test public void firstTokenNotApplied() { Acronym acronym = buildAcronym(FIRST_LETTERS_IGNORE_NUMERIC, AcronymSeparator.DASH); assertEquals("D-O-F", acronym.transform("3 Degrees Of Freedom")); }
@Test public void middleTokenNotApplied() { Acronym acronym = buildAcronym(FIRST_LETTERS_IGNORE_NUMERIC, AcronymSeparator.DASH); assertEquals("D-O-F", acronym.transform("Degrees 3 Of Freedom")); }
@Test public void lastTokenNotApplied() { Acronym acronym = buildAcronym(FIRST_LETTERS_IGNORE_NUMERIC, AcronymSeparator.DASH); assertEquals("D-O-F", acronym.transform("Degrees Of Freedom 3")); }
@Test public void firstLettersIgnoreNumericsNoSeparators() { Acronym acronym = buildAcronym(FIRST_LETTERS_IGNORE_NUMERIC, AcronymSeparator.NONE); List<String> expected = Arrays.asList("UNESaCO", "T", "At", "ER", "BNaL", "ATMPL", "am", "IO", "ATT", "Cc", "VUA"); assert (expected.size() == inputs.size()); for (int i = 0; i < inputs.size(); i++) { assertEquals(expected.get(i), acronym.transform(inputs.get(i))); } }
@Test public void firstLettersKeepNumericsNoSeparators() { Acronym acronym = buildAcronym(FIRST_LETTERS_KEEP_NUMERIC, AcronymSeparator.NONE); List<String> expected = Arrays.asList("UNESaCO", "T", "At", "E2R", "BNaL", "ATMPL", "am", "IO", "ATT", "3c", "V5UA"); assert (expected.size() == inputs.size()); for (int i = 0; i < inputs.size(); i++) { assertEquals(expected.get(i), acronym.transform(inputs.get(i))); } }
@Test public void firstUpperCaseLettersIgnoreNumericsNoSeparators() { Acronym acronym = buildAcronym(FIRST_UPPER_CASE_LETTERS_IGNORE_NUMERIC, AcronymSeparator.NONE); List<String> expected = Arrays.asList("UNESCO", "T", "A", "ER", "BNL", "ATMPL", "", "IO", "ATT", "C", "VUA"); assert (expected.size() == inputs.size()); for (int i = 0; i < inputs.size(); i++) { assertEquals(expected.get(i), acronym.transform(inputs.get(i))); } }
@Test public void firstUpperCaseLettersKeepNumericsNoSeparators() { Acronym acronym = buildAcronym(FIRST_UPPER_CASE_LETTERS_KEEP_NUMERIC, AcronymSeparator.NONE); List<String> expected = Arrays.asList("UNESCO", "T", "A", "E2R", "BNL", "ATMPL", "", "IO", "ATT", "3", "V5UA"); assert (expected.size() == inputs.size()); for (int i = 0; i < inputs.size(); i++) { assertEquals(expected.get(i), acronym.transform(inputs.get(i))); } }
@Test public void allUpperCaseLettersIgnoreNumericsNoSeparators() { Acronym acronym = buildAcronym(ALL_UPPER_CASE_LETTERS_IGNORE_NUMERIC, AcronymSeparator.NONE); List<String> expected = Arrays.asList("UNESCO", "TNT", "ASYNC", "ER", "BENELUX", "ATTMPL", "", "IO", "ATT", "COM", "VUA"); assert (expected.size() == inputs.size()); for (int i = 0; i < inputs.size(); i++) { assertEquals(expected.get(i), acronym.transform(inputs.get(i))); } }
@Test public void allUpperCaseLettersKeepNumericsNoSeparators() { Acronym acronym = buildAcronym(ALL_UPPER_CASE_LETTERS_KEEP_NUMERIC, AcronymSeparator.NONE); List<String> expected = Arrays.asList("UNESCO", "TNT", "ASYNC", "E2R", "BENELUX", "ATTMPL", "", "IO", "ATT", "3COM", "V5UA"); assert (expected.size() == inputs.size()); for (int i = 0; i < inputs.size(); i++) { assertEquals(expected.get(i), acronym.transform(inputs.get(i))); } }
@Test public void firstLettersWithPeriods() { Acronym acronym = buildAcronym(FIRST_LETTERS_IGNORE_NUMERIC, AcronymSeparator.PERIOD); List<String> expected = Arrays.asList("U.N.E.S.a.C.O.", "T.", "A.t.", "E.R.", "B.N.a.L.", "A.T.M.P.L.", "a.m.", "I.O.", "A.T.T.", "C.c.", "V.U.A."); assert (expected.size() == inputs.size()); for (int i = 0; i < inputs.size(); i++) { assertEquals(expected.get(i), acronym.transform(inputs.get(i))); } }
@Test public void firstUpperCaseLettersWithSpaces() { Acronym acronym = buildAcronym(FIRST_UPPER_CASE_LETTERS_IGNORE_NUMERIC, AcronymSeparator.SPACE); List<String> expected = Arrays.asList("U N E S C O", "T", "A", "E R", "B N L", "A T M P L", "", "I O", "A T T", "C", "V U A"); assert (expected.size() == inputs.size()); for (int i = 0; i < inputs.size(); i++) { assertEquals(expected.get(i), acronym.transform(inputs.get(i))); } }
@Test public void firstUpperCaseLettersWithDashes() { Acronym acronym = buildAcronym(FIRST_UPPER_CASE_LETTERS_IGNORE_NUMERIC, AcronymSeparator.DASH); List<String> expected = Arrays.asList("U-N-E-S-C-O", "T", "A", "E-R", "B-N-L", "A-T-M-P-L", "", "I-O", "A-T-T", "C", "V-U-A"); assert (expected.size() == inputs.size()); for (int i = 0; i < inputs.size(); i++) { assertEquals(expected.get(i), acronym.transform(inputs.get(i))); } }
@Test public void allUpperCaseLettersKeepSpecialChars() { Acronym acronym = buildAcronym(ALL_UPPER_CASE_LETTERS_KEEP_NUMERIC, AcronymSeparator.KEEP_SPECIAL_CHARS); List<String> expected = Arrays.asList("UNESCO", "TNT", "ASYNC", "E-2-R", "BENELUX", "AT&TMPL", "", "I/O", "AT&T", "3COM", "V5UA"); assert (expected.size() == inputs.size()); for (int i = 0; i < inputs.size(); i++) { assertEquals(expected.get(i), acronym.transform(inputs.get(i))); } }