commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brit...@apache.org
Subject [09/12] [text] Drop unused code from NameString and clean up NameStringTest
Date Sun, 19 Apr 2015 15:14:58 GMT
Drop unused code from NameString and clean up NameStringTest


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

Branch: refs/heads/SANDBOX-498
Commit: ed985cd51220e956f516acecf1039defd0141d34
Parents: 9e34064
Author: Benedikt Ritter <britter@apache.org>
Authored: Sun Apr 19 16:44:32 2015 +0200
Committer: Benedikt Ritter <britter@apache.org>
Committed: Sun Apr 19 16:44:32 2015 +0200

----------------------------------------------------------------------
 .../commons/text/names/HumanNameParser.java     |  5 +-
 .../apache/commons/text/names/NameString.java   | 24 ++-----
 .../commons/text/names/NameStringTest.java      | 67 ++++++--------------
 3 files changed, 30 insertions(+), 66 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-text/blob/ed985cd5/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 a29e375..b5c0aa3 100644
--- a/src/main/java/org/apache/commons/text/names/HumanNameParser.java
+++ b/src/main/java/org/apache/commons/text/names/HumanNameParser.java
@@ -100,6 +100,7 @@ public final class HumanNameParser {
         Objects.requireNonNull(name, "Parameter 'name' must not be null.");
 
         NameString nameString = new NameString(name);
+        // TODO compile regexes only once when the parser is created
         String suffixes = StringUtils.join(this.suffixes, "\\.*|") + "\\.*";
         String prefixes = StringUtils.join(this.prefixes, " |") + " ";
 
@@ -132,11 +133,11 @@ public final class HumanNameParser {
         // 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()
+ "}'");
+            throw new NameParseException("Couldn't find a first name in '{" + nameString.getWrappedString()
+ "}'");
         }
 
         // if anything's left, that's the middle name
-        String middle = nameString.getStr();
+        String middle = nameString.getWrappedString();
         
         return new Name(leadingInit, first, nickname, middle, last, suffix);
     }

http://git-wip-us.apache.org/repos/asf/commons-text/blob/ed985cd5/src/main/java/org/apache/commons/text/names/NameString.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/names/NameString.java b/src/main/java/org/apache/commons/text/names/NameString.java
index 8f606f2..54e2753 100644
--- a/src/main/java/org/apache/commons/text/names/NameString.java
+++ b/src/main/java/org/apache/commons/text/names/NameString.java
@@ -37,30 +37,20 @@ final class NameString {
      *
      * @param str encapsulated string.
      */
-    public NameString(String str) {
+    NameString(String str) {
         this.str = str;
     }
 
     /**
-     * Gets the encapsulated string.
+     * Gets the wrapped string.
      *
-     * @return encapsulated string
+     * @return wrapped string
      */
-    public String getStr() {
+    String getWrappedString() {
         return str;
     }
 
     /**
-     * Sets the encapsulated string value.
-     *
-     * @param str string value
-     */
-    public void setStr(String str) {
-        this.str = str;
-        this.norm();
-    }
-
-    /**
      * Uses a regex to chop off and return part of the namestring.
      * There are two parts: first, it returns the matched substring,
      * and then it removes that substring from the encapsulated
@@ -70,7 +60,7 @@ final class NameString {
      * @param submatchIndex which of the parenthesized submatches to use
      * @return the part of the namestring that got chopped off
      */
-    public String chopWithRegex(String regex, int submatchIndex) {
+    String chopWithRegex(String regex, int submatchIndex) {
         String chopped = "";
         Pattern pattern = Pattern.compile(regex);
         Matcher matcher = pattern.matcher(this.str);
@@ -106,7 +96,7 @@ final class NameString {
      * @param flipAroundChar the character(s) demarcating the two halves you want to flip.
      * @throws NameParseException if a regex fails or a condition is not expected
      */
-    public void flip(String flipAroundChar) {
+    void flip(String flipAroundChar) {
         String[] parts = this.str.split(flipAroundChar);
         if (parts != null) {
             if (parts.length == 2) {
@@ -125,7 +115,7 @@ final class NameString {
      * <p>Strips whitespace chars from ends, strips redundant whitespace, converts
      * whitespace chars to " ".</p>
      */
-    public void norm() {
+    private void norm() {
         this.str = this.str.trim();
         this.str = this.str.replaceAll("\\s+", " ");
         this.str = this.str.replaceAll(",$", " ");

http://git-wip-us.apache.org/repos/asf/commons-text/blob/ed985cd5/src/test/java/org/apache/commons/text/names/NameStringTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/names/NameStringTest.java b/src/test/java/org/apache/commons/text/names/NameStringTest.java
index 494c70b..e087579 100644
--- a/src/test/java/org/apache/commons/text/names/NameStringTest.java
+++ b/src/test/java/org/apache/commons/text/names/NameStringTest.java
@@ -16,7 +16,8 @@
  */
 package org.apache.commons.text.names;
 
-import static org.junit.Assert.assertEquals;
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertThat;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -35,70 +36,42 @@ public class NameStringTest {
     }
 
     @Test
-    public void testSetStrRemovesWhitespaceAtEnds() {
-        nameString.setStr("    Björn O'Malley \r\n");
-        assertEquals(
-            "Björn O'Malley",
-            nameString.getStr()
-        );
-    }
+    public void testChopWithRegexReturnsChoppedSubstring() {
+        NameString nameString = new NameString("Björn O'Malley");
 
-    @Test
-    public void testSetStrRemovesRedudentantWhitespace(){
-        nameString.setStr(" Björn    O'Malley");
-        assertEquals(
-            "Björn O'Malley",
-            nameString.getStr()
-        );
-    }
-
-    @Test
-    public void testChopWithRegexReturnsChoppedSubstring(){
-        nameString.setStr("Björn O'Malley");
-        assertEquals(
-            "Björn",
-            nameString.chopWithRegex("(^([^ ]+))(.+)", 1)
-        );
+        assertThat(nameString.chopWithRegex("(^([^ ]+))(.+)", 1), equalTo("Björn"));
     }
 
     @Test
-    public void testChopWithRegexChopsStartOffNameStr(){
-        nameString.setStr("Björn O'Malley");
+    public void testChopWithRegexChopsStartOffNameStr() {
+        NameString nameString = new NameString("Björn O'Malley");
         nameString.chopWithRegex("(^[^ ]+)", 0);
-        assertEquals(
-                "O'Malley",
-            nameString.getStr()
-        );
+
+        assertThat(nameString.getWrappedString(), equalTo("O'Malley"));
     }
 
     @Test
-    public void testChopWithRegexChopsEndOffNameStr(){
-        nameString.setStr("Björn O'Malley");
+    public void testChopWithRegexChopsEndOffNameStr() {
+        NameString nameString = new NameString("Björn O'Malley");
         nameString.chopWithRegex("( (.+)$)", 1);
-        assertEquals(
-            "Björn",
-            nameString.getStr()
-        );
+
+        assertThat(nameString.getWrappedString(), equalTo("Björn"));
     }
 
     @Test
-    public void testChopWithRegexChopsMiddleFromNameStr(){
-        nameString.setStr("Björn 'Bill' O'Malley");
+    public void testChopWithRegexChopsMiddleFromNameStr() {
+        NameString nameString = new NameString("Björn 'Bill' O'Malley");
         nameString.chopWithRegex("( '[^']+' )", 0);
-        assertEquals(
-            "Björn O'Malley",
-            nameString.getStr()
-        );
+
+        assertThat(nameString.getWrappedString(), equalTo("Björn O'Malley"));
     }
 
     @Test
     public void testFlip() {
-        nameString.setStr("O'Malley, Björn");
+        NameString nameString = new NameString("O'Malley, Björn");
         nameString.flip(",");
-        assertEquals(
-            "Björn O'Malley",
-            nameString.getStr()
-        );
+
+        assertThat(nameString.getWrappedString(), equalTo("Björn O'Malley"));
     }
 
 }


Mime
View raw message