Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- public class Namer {
- private String firstName;
- private String lastName;
- public Namer(String firstName, String lastName) {
- this.firstName = firstName;
- this.lastName = lastName;
- }
- public String getFirstName() {
- return firstName;
- }
- public String getLastName() {
- return lastName;
- }
- }
- public class NameParserSimple {
- public static Namer parseName(String name) {
- int commaIdx = name.indexOf(',');
- if (commaIdx != -1) {
- return new Namer(name.substring(commaIdx + 1).trim(), name.substring(0, commaIdx).trim());
- }
- int spaceIdx = name.indexOf(' ');
- if (spaceIdx != -1) {
- return new Namer(name.substring(0, spaceIdx).trim(), name.substring(spaceIdx + 1).trim());
- }
- return new Namer("", name);
- }
- }
- public class NameParserRegex {
- static List<String> nameFormats = { "\{0}, \{1}", "\{1} \{0}" };
- public static Namer parseName(String name) {
- for (format : nameFormats) {
- Namer namer = parseName(name, format);
- if (namer != null)
- return namer;
- }
- return new Namer("", name);
- }
- public static Namer parseName(String name, Regex nameFormat) {
- Match match = nameFormat.match(name);
- if (!match)
- return null;
- return new Namer(match.group(0), match.group(1));
- }
- }
- public class NameParserConfigurable {
- private List<String> nameFormats;
- public NameParser(List<String> nameFormats) {
- this.nameFormats = nameFormats;
- }
- public Namer parseName(String name) {
- for (format : nameFormats) {
- Namer namer = parseName(name, format);
- if (namer != null)
- return namer;
- }
- return new Namer("", name);
- }
- public static Namer parseName(String name, Regex nameFormat) {
- Match match = nameFormat.match(name);
- if (!match)
- return null;
- return new Namer(match.group(0), match.group(1));
- }
- }
Add Comment
Please, Sign In to add comment