You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ki...@apache.org on 2015/04/20 04:59:02 UTC

[05/13] [text] Remove state from HumanNameParser, making it immutable

Remove state from HumanNameParser, making it immutable


Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/1f6c5dae
Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/1f6c5dae
Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/1f6c5dae

Branch: refs/heads/master
Commit: 1f6c5daecded67a17c07371a564f74ef623b3f29
Parents: 685f9a8
Author: Benedikt Ritter <br...@apache.org>
Authored: Sun Apr 19 16:28:37 2015 +0200
Committer: Benedikt Ritter <br...@apache.org>
Committed: Sun Apr 19 16:28:37 2015 +0200

----------------------------------------------------------------------
 .../commons/text/names/HumanNameParser.java     | 141 +++----------------
 .../org/apache/commons/text/names/Name.java     |  32 +++++
 2 files changed, 51 insertions(+), 122 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-text/blob/1f6c5dae/src/main/java/org/apache/commons/text/names/HumanNameParser.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/names/HumanNameParser.java b/src/main/java/org/apache/commons/text/names/HumanNameParser.java
index df8e55c..c47abde 100644
--- a/src/main/java/org/apache/commons/text/names/HumanNameParser.java
+++ b/src/main/java/org/apache/commons/text/names/HumanNameParser.java
@@ -61,135 +61,32 @@ import org.apache.commons.lang3.StringUtils;
  * <p>This implementation is based on the Java implementation, with additions
  * suggested in <a href="https://issues.apache.org/jira/browse/SANDBOX-487">SANDBOX-487</a>.</p>
  *
- * <p>This class is not thread-safe.</p>
+ * <p>This class is immutable.</p>
  */
 public class HumanNameParser {
 
     /**
-     * Leading init part.
-     */
-    private String leadingInit;
-    /**
-     * First name.
-     */
-    private String first;
-    /**
-     * Single nickname found in the name input.
-     */
-    private String nickname;
-    /**
-     * Middle name.
-     */
-    private String middle;
-    /**
-     * Last name.
-     */
-    private String last;
-    /**
-     * Name suffix.
-     */
-    private String suffix;
-    /**
      * Suffixes found.
      */
-    private List<String> suffixes;
+    private final List<String> suffixes;
     /**
      * Prefixes found.
      */
-    private List<String> prefixes;
+    private final List<String> prefixes;
 
     /**
      * Creates a parser given a string name.
      */
     public HumanNameParser() {
-        this.leadingInit = "";
-        this.first = "";
-        this.nickname = "";
-        this.middle = "";
-        this.last = "";
-        this.suffix = "";
-
-        this.suffixes = Arrays.asList(new String[]{
+        // TODO make this configurable
+        this.suffixes = Arrays.asList(
                 "esq", "esquire", "jr",
-                "sr", "2", "ii", "iii", "iv"});
-        this.prefixes = Arrays
-            .asList(new String[] {
+                "sr", "2", "ii", "iii", "iv");
+        this.prefixes = Arrays.asList(
                     "bar", "ben", "bin", "da", "dal",
                     "de la", "de", "del", "der", "di", "ibn", "la", "le",
                     "san", "st", "ste", "van", "van der", "van den", "vel",
-                    "von" });
-    }
-
-    /**
-     * Gets the leading init part of the name.
-     *
-     * @return the leading init part of the name
-     */
-    public String getLeadingInit() {
-        return leadingInit;
-    }
-
-    /**
-     * Gets the first name.
-     *
-     * @return first name
-     */
-    public String getFirst() {
-        return first;
-    }
-
-    /**
-     * Gets the nickname.
-     *
-     * @return the nickname
-     */
-    public String getNickname() {
-        return nickname;
-    }
-
-    /**
-     * Gets the middle name.
-     *
-     * @return the middle name
-     */
-    public String getMiddle() {
-        return middle;
-    }
-
-    /**
-     * Gets the last name.
-     *
-     * @return the last name
-     */
-    public String getLast() {
-        return last;
-    }
-
-    /**
-     * Gets the suffix part of the name.
-     *
-     * @return the name suffix
-     */
-    public String getSuffix() {
-        return suffix;
-    }
-
-    /**
-     * Gets the name suffixes.
-     *
-     * @return the name suffixes
-     */
-    public List<String> getSuffixes() {
-        return suffixes;
-    }
-
-    /**
-     * Gets the name prefixes.
-     *
-     * @return the name prefixes
-     */
-    public List<String> getPrefixes() {
-        return prefixes;
+                    "von" );
     }
 
     /**
@@ -218,28 +115,28 @@ public class HumanNameParser {
         String firstRegex = "(?i)^([^ ]+)";
 
         // get nickname, if there is one
-        this.nickname = nameString.chopWithRegex(nicknamesRegex, 2);
+        String nickname = nameString.chopWithRegex(nicknamesRegex, 2);
 
         // get suffix, if there is one
-        this.suffix = nameString.chopWithRegex(suffixRegex, 1);
+        String suffix = nameString.chopWithRegex(suffixRegex, 1);
 
-        // flip the before-comma and after-comma parts of the nameString
+        // flip the before-comma and after-comma parts of the name
         nameString.flip(",");
 
-        // get the last nameString
-        this.last = nameString.chopWithRegex(lastRegex, 0);
+        // get the last name
+        String last = nameString.chopWithRegex(lastRegex, 0);
 
         // get the first initial, if there is one
-        this.leadingInit = nameString.chopWithRegex(leadingInitRegex, 1);
+        String leadingInit = nameString.chopWithRegex(leadingInitRegex, 1);
 
-        // get the first nameString
-        this.first = nameString.chopWithRegex(firstRegex, 0);
-        if (StringUtils.isBlank(this.first)) {
+        // get the first name
+        String first = nameString.chopWithRegex(firstRegex, 0);
+        if (StringUtils.isBlank(first)) {
             throw new NameParseException("Couldn't find a first name in '{" + nameString.getStr() + "}'");
         }
 
-        // if anything's left, that's the middle nameString
-        this.middle = nameString.getStr();
+        // if anything's left, that's the middle name
+        String middle = nameString.getStr();
         
         return new Name(leadingInit, first, nickname, middle, last, suffix);
     }

http://git-wip-us.apache.org/repos/asf/commons-text/blob/1f6c5dae/src/main/java/org/apache/commons/text/names/Name.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/names/Name.java b/src/main/java/org/apache/commons/text/names/Name.java
index 3067ba5..7e32de4 100644
--- a/src/main/java/org/apache/commons/text/names/Name.java
+++ b/src/main/java/org/apache/commons/text/names/Name.java
@@ -41,26 +41,58 @@ public final class Name {
         this.suffix = suffix;
     }
 
+    // TODO Add an example to each getter
+
+    /**
+     * Gets the leading init part of the name.
+     *
+     * @return the leading init part of the name
+     */
     public String getLeadingInitial() {
         return leadingInitial;
     }
 
+    /**
+     * Gets the first name.
+     *
+     * @return first name
+     */
     public String getFirstName() {
         return firstName;
     }
 
+    /**
+     * Gets the nickname.
+     *
+     * @return the nickname
+     */
     public String getNickName() {
         return nickName;
     }
 
+    /**
+     * Gets the middle name.
+     *
+     * @return the middle name
+     */
     public String getMiddleName() {
         return middleName;
     }
 
+    /**
+     * Gets the last name.
+     *
+     * @return the last name
+     */
     public String getLastName() {
         return lastName;
     }
 
+    /**
+     * Gets the suffix part of the name.
+     *
+     * @return the name suffix
+     */
     public String getSuffix() {
         return suffix;
     }