Created
June 22, 2013 16:32
-
-
Save sortega/5841498 to your computer and use it in GitHub Desktop.
Name inverter in Java by Uncle Bob
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nameInverter; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
public class NameInverter { | |
String invertName(String name) { | |
if (name == null || name.length() <= 0) | |
return ""; | |
else | |
return formatName(removeHonorifics(splitNames(name))); | |
} | |
String formatName(List<String> names) { | |
if (names.size() == 1) { | |
return names.get(0); | |
} else { | |
return formatMultiElementName(names); | |
} | |
} | |
String formatMultiElementName(List<String> names) { | |
String postNominal = getPostNominals(names); | |
String firstName = names.get(0); | |
String lastName = names.get(1); | |
return String.format("%s, %s %s", lastName, firstName, postNominal).trim(); | |
} | |
String getPostNominals(List<String> names) { | |
String postNominalString = ""; | |
if (names.size() > 2) { | |
List<String> postNominals = names.subList(2, names.size()); | |
for (String pn : postNominals) | |
postNominalString += pn + " "; | |
} | |
return postNominalString; | |
} | |
List<String> removeHonorifics(List<String> names) { | |
if (names.size() > 1 && isHonorific(names.get(0))) | |
names.remove(0); | |
return names; | |
} | |
boolean isHonorific(String word) { | |
return word.matches("Mr\\.|Mrs\\."); | |
} | |
ArrayList<String> splitNames(String name) { | |
return new ArrayList<String>(Arrays.asList(name.trim().split("\\s+"))); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nameInverter; | |
import org.junit.Before; | |
import org.junit.Test; | |
import static junit.framework.Assert.assertEquals; | |
public class NameInverterTest { | |
private NameInverter nameInverter; | |
@Before | |
public void setup() { | |
nameInverter = new NameInverter(); | |
} | |
private void assertInverted(String originalName, String invertedName) { | |
assertEquals(invertedName, nameInverter.invertName(originalName)); | |
} | |
@Test | |
public void givenNull_returnsEmptyString() throws Exception { | |
assertInverted(null, ""); | |
} | |
@Test | |
public void givenEmptyString_returnEmptyString() throws Exception { | |
assertInverted("", ""); | |
} | |
@Test | |
public void givenSimpleName_returnSimpleName() throws Exception { | |
assertInverted("Name", "Name"); | |
} | |
@Test | |
public void givenASimpleNameWithSpaces_returnSimpleNameWithoutSpaces() throws Exception { | |
assertInverted(" Name ", "Name"); | |
} | |
@Test | |
public void givenFirstLast_returnLastFirst() throws Exception { | |
assertInverted("First Last", "Last, First"); | |
} | |
@Test | |
public void givenFirstLastWithExtraSpaces_returnLastFirst() throws Exception { | |
assertInverted(" First Last ", "Last, First"); | |
} | |
@Test | |
public void ignoreHonorifics() throws Exception { | |
assertInverted("Mr. First Last", "Last, First"); | |
assertInverted("Mrs. First Last", "Last, First"); | |
} | |
@Test | |
public void postNominals_stayAtEnd() throws Exception { | |
assertInverted("First Last Sr.", "Last, First Sr."); | |
assertInverted("First Last BS. Phd.", "Last, First BS. Phd."); | |
} | |
@Test | |
public void integration() throws Exception { | |
assertInverted(" Robert Martin III esq. ", "Martin, Robert III esq."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment