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:58:59 UTC

[02/13] [text] Pass the name to parse as parameter to the parse method

Pass the name to parse as parameter to the parse method


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

Branch: refs/heads/master
Commit: df7e7a7b0aba73a1bf09c41dbd32e913252a8707
Parents: aa29350
Author: Benedikt Ritter <br...@apache.org>
Authored: Sun Apr 19 16:02:55 2015 +0200
Committer: Benedikt Ritter <br...@apache.org>
Committed: Sun Apr 19 16:02:55 2015 +0200

----------------------------------------------------------------------
 .../commons/text/names/HumanNameParser.java     | 52 ++++++--------------
 .../commons/text/names/HumanNameParserTest.java |  4 +-
 2 files changed, 16 insertions(+), 40 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-text/blob/df7e7a7b/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 5088bba..bf8f9ed 100644
--- a/src/main/java/org/apache/commons/text/names/HumanNameParser.java
+++ b/src/main/java/org/apache/commons/text/names/HumanNameParser.java
@@ -65,10 +65,6 @@ import org.apache.commons.lang3.StringUtils;
 public class HumanNameParser {
 
     /**
-     * Name parsed.
-     */
-    private Name name;
-    /**
      * Leading init part.
      */
     private String leadingInit;
@@ -103,21 +99,8 @@ public class HumanNameParser {
 
     /**
      * Creates a parser given a string name.
-     *
-     * @param name string name
-     */
-    public HumanNameParser(String name) {
-        this(new Name(name));
-    }
-
-    /**
-     * Creates a parser given a {@code Name} object.
-     *
-     * @param name {@code Name}
      */
-    public HumanNameParser(Name name) {
-        this.name = name;
-
+    public HumanNameParser() {
         this.leadingInit = "";
         this.first = "";
         this.nickname = "";
@@ -125,9 +108,9 @@ public class HumanNameParser {
         this.last = "";
         this.suffix = "";
 
-        this.suffixes = Arrays.asList(new String[] {
+        this.suffixes = Arrays.asList(new String[]{
                 "esq", "esquire", "jr",
-                "sr", "2", "ii", "iii", "iv" });
+                "sr", "2", "ii", "iii", "iv"});
         this.prefixes = Arrays
             .asList(new String[] {
                     "bar", "ben", "bin", "da", "dal",
@@ -137,15 +120,6 @@ public class HumanNameParser {
     }
 
     /**
-     * Gets the {@code Name} object.
-     *
-     * @return the {@code Name} object
-     */
-    public Name getName() {
-        return name;
-    }
-
-    /**
      * Gets the leading init part of the name.
      *
      * @return the leading init part of the name
@@ -220,9 +194,11 @@ public class HumanNameParser {
     /**
      * Consumes the string and creates the name parts.
      *
+     * @param nameStr the name to parse.
      * @throws NameParseException if the parser fails to retrieve the name parts
      */
-    public void parse() {
+    public void parse(String nameStr) {
+        Name name = new Name(nameStr);
         String suffixes = StringUtils.join(this.suffixes, "\\.*|") + "\\.*";
         String prefixes = StringUtils.join(this.prefixes, " |") + " ";
 
@@ -238,28 +214,28 @@ public class HumanNameParser {
         String firstRegex = "(?i)^([^ ]+)";
 
         // get nickname, if there is one
-        this.nickname = this.name.chopWithRegex(nicknamesRegex, 2);
+        this.nickname = name.chopWithRegex(nicknamesRegex, 2);
 
         // get suffix, if there is one
-        this.suffix = this.name.chopWithRegex(suffixRegex, 1);
+        this.suffix = name.chopWithRegex(suffixRegex, 1);
 
         // flip the before-comma and after-comma parts of the name
-        this.name.flip(",");
+        name.flip(",");
 
         // get the last name
-        this.last = this.name.chopWithRegex(lastRegex, 0);
+        this.last = name.chopWithRegex(lastRegex, 0);
 
         // get the first initial, if there is one
-        this.leadingInit = this.name.chopWithRegex(leadingInitRegex, 1);
+        this.leadingInit = name.chopWithRegex(leadingInitRegex, 1);
 
         // get the first name
-        this.first = this.name.chopWithRegex(firstRegex, 0);
+        this.first = name.chopWithRegex(firstRegex, 0);
         if (StringUtils.isBlank(this.first)) {
-            throw new NameParseException("Couldn't find a first name in '{" + this.name.getStr() + "}'");
+            throw new NameParseException("Couldn't find a first name in '{" + name.getStr() + "}'");
         }
 
         // if anything's left, that's the middle name
-        this.middle = this.name.getStr();
+        this.middle = name.getStr();
     }
 
 }

http://git-wip-us.apache.org/repos/asf/commons-text/blob/df7e7a7b/src/test/java/org/apache/commons/text/names/HumanNameParserTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/names/HumanNameParserTest.java b/src/test/java/org/apache/commons/text/names/HumanNameParserTest.java
index 5ff7805..478d19c 100644
--- a/src/test/java/org/apache/commons/text/names/HumanNameParserTest.java
+++ b/src/test/java/org/apache/commons/text/names/HumanNameParserTest.java
@@ -64,8 +64,8 @@ public class HumanNameParserTest {
      * @param record a CSVRecord representing one record in the input file.
      */
     private void validateRecord(CSVRecord record) {
-        HumanNameParser parser = new HumanNameParser(record.get(Colums.Name));
-        parser.parse();
+        HumanNameParser parser = new HumanNameParser();
+        parser.parse(record.get(Colums.Name));
 
         long recordNum = record.getRecordNumber();
         assertThat("Wrong LeadingInit in record " + recordNum,