commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ki...@apache.org
Subject [text] TEXT-16: Make ParserOptions immutable
Date Sat, 02 May 2015 11:46:22 GMT
Repository: commons-text
Updated Branches:
  refs/heads/SANDBOX-498-OPTIONS 331f80bfc -> 4310744eb


TEXT-16: Make ParserOptions 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/4310744e
Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/4310744e
Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/4310744e

Branch: refs/heads/SANDBOX-498-OPTIONS
Commit: 4310744eb62e735a39ff84840cd4eb41ae6eb5a6
Parents: 331f80b
Author: Bruno P. Kinoshita <brunodepaulak@yahoo.com.br>
Authored: Sat May 2 23:46:06 2015 +1200
Committer: Bruno P. Kinoshita <brunodepaulak@yahoo.com.br>
Committed: Sat May 2 23:46:06 2015 +1200

----------------------------------------------------------------------
 .../commons/text/names/ParserOptions.java       | 44 +++++++++---
 .../commons/text/names/NameStringTest.java      |  6 --
 .../commons/text/names/ParserOptionsTest.java   | 72 ++++++++++++++++++++
 3 files changed, 105 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-text/blob/4310744e/src/main/java/org/apache/commons/text/names/ParserOptions.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/names/ParserOptions.java b/src/main/java/org/apache/commons/text/names/ParserOptions.java
index 6bca771..dcde9ae 100644
--- a/src/main/java/org/apache/commons/text/names/ParserOptions.java
+++ b/src/main/java/org/apache/commons/text/names/ParserOptions.java
@@ -17,43 +17,65 @@
 package org.apache.commons.text.names;
 
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.Set;
 
+import org.apache.commons.lang3.Validate;
+
 /**
  * Options for the {@link HumanNameParser} parser.
  */
 public final class ParserOptions {
 
-    public static final ParserOptions DEFAULT_OPTIONS = new ParserOptions();
+    private static final Set<String> DEFAULT_PREFIXES = new HashSet<String>(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"));
+
+    private static final Set<String> DEFAULT_SUFFIXES = new HashSet<String>(Arrays.asList(
+            "esq", "esquire", "jr",
+            "sr", "2", "ii", "iii", "iv"));
 
     private final Set<String> suffixes;
 
     private final Set<String> prefixes;
 
+    public static final ParserOptions DEFAULT_OPTIONS = new ParserOptions();
+
+    /**
+     * Creates options for the human names parser.
+     */
     public ParserOptions() {
-        this.suffixes = new HashSet<String>(Arrays.asList(
-                "esq", "esquire", "jr",
-                "sr", "2", "ii", "iii", "iv"));
-        this.prefixes = new HashSet<String>(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"));
+        this(DEFAULT_PREFIXES, DEFAULT_SUFFIXES);
+    }
+
+    /**
+     * Creates options for the human names parser.
+     *
+     * @param prefixes name prefixes, must not be null
+     * @param suffixes name suffixes, must not be null
+     */
+    public ParserOptions(Set<String> prefixes, Set<String> suffixes) {
+        Validate.notNull(prefixes, "Prefixes must not be null");
+        Validate.notNull(prefixes, "Suffixes must not be null");
+        this.prefixes = prefixes;
+        this.suffixes = suffixes;
     }
 
     /**
      * @return the suffixes
      */
     public Set<String> getSuffixes() {
-        return suffixes;
+        return Collections.unmodifiableSet(suffixes);
     }
 
     /**
      * @return the prefixes
      */
     public Set<String> getPrefixes() {
-        return prefixes;
+        return Collections.unmodifiableSet(prefixes);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/commons-text/blob/4310744e/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 e087579..4431d4a 100644
--- a/src/test/java/org/apache/commons/text/names/NameStringTest.java
+++ b/src/test/java/org/apache/commons/text/names/NameStringTest.java
@@ -37,14 +37,11 @@ public class NameStringTest {
 
     @Test
     public void testChopWithRegexReturnsChoppedSubstring() {
-        NameString nameString = new NameString("Björn O'Malley");
-
         assertThat(nameString.chopWithRegex("(^([^ ]+))(.+)", 1), equalTo("Björn"));
     }
 
     @Test
     public void testChopWithRegexChopsStartOffNameStr() {
-        NameString nameString = new NameString("Björn O'Malley");
         nameString.chopWithRegex("(^[^ ]+)", 0);
 
         assertThat(nameString.getWrappedString(), equalTo("O'Malley"));
@@ -52,7 +49,6 @@ public class NameStringTest {
 
     @Test
     public void testChopWithRegexChopsEndOffNameStr() {
-        NameString nameString = new NameString("Björn O'Malley");
         nameString.chopWithRegex("( (.+)$)", 1);
 
         assertThat(nameString.getWrappedString(), equalTo("Björn"));
@@ -60,7 +56,6 @@ public class NameStringTest {
 
     @Test
     public void testChopWithRegexChopsMiddleFromNameStr() {
-        NameString nameString = new NameString("Björn 'Bill' O'Malley");
         nameString.chopWithRegex("( '[^']+' )", 0);
 
         assertThat(nameString.getWrappedString(), equalTo("Björn O'Malley"));
@@ -68,7 +63,6 @@ public class NameStringTest {
 
     @Test
     public void testFlip() {
-        NameString nameString = new NameString("O'Malley, Björn");
         nameString.flip(",");
 
         assertThat(nameString.getWrappedString(), equalTo("Björn O'Malley"));

http://git-wip-us.apache.org/repos/asf/commons-text/blob/4310744e/src/test/java/org/apache/commons/text/names/ParserOptionsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/names/ParserOptionsTest.java b/src/test/java/org/apache/commons/text/names/ParserOptionsTest.java
new file mode 100644
index 0000000..a2ba727
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/names/ParserOptionsTest.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.names;
+
+import static org.junit.Assert.assertTrue;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests for {@code ParserOptions}.
+ */
+public class ParserOptionsTest {
+
+    private ParserOptions p1;
+
+    @Before
+    public void setUp() {
+        p1 = new ParserOptions();
+    }
+    
+    @Test(expected=UnsupportedOperationException.class)
+    public void testParserOptionsIsImmutableForPrefixes() {
+        p1.getPrefixes().add("Invalid entry");
+    }
+
+    @Test(expected=UnsupportedOperationException.class)
+    public void testParserOptionsIsImmutableForSuffixes() {
+        p1.getSuffixes().add("Invalid entry");
+    }
+
+    @Test
+    public void testParserOptionsUsesDefaultOptions() {
+        assertTrue(p1.getPrefixes().contains("van"));
+        assertTrue(p1.getPrefixes().contains("san"));
+        assertTrue(p1.getPrefixes().contains("bin"));
+        assertTrue(p1.getSuffixes().contains("esq"));
+        assertTrue(p1.getSuffixes().contains("esquire"));
+        assertTrue(p1.getSuffixes().contains("iv"));
+    }
+
+    @Test
+    public void testParseOptionsShouldBeConfigurable() {
+        Set<String> prefixes = new HashSet<String>(Arrays.asList("sr", "nho",
"sinho"));
+        p1 = new ParserOptions(prefixes, Collections.<String>emptySet());
+        assertTrue(p1.getPrefixes().contains("nho"));
+
+        Set<String> suffixes = new HashSet<String>(Arrays.asList("primeiro",
"segundo", "neto"));
+        p1 = new ParserOptions(Collections.<String>emptySet(), suffixes);
+        assertTrue(p1.getSuffixes().contains("neto"));
+    }
+
+}


Mime
View raw message