You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2017/09/22 16:50:27 UTC
[text] [TEXT-102] StrLookup.resourceBundleLookup(ResourceBundle).
Repository: commons-text
Updated Branches:
refs/heads/master 30a29e2cf -> 28e26b2b6
[TEXT-102] StrLookup.resourceBundleLookup(ResourceBundle).
Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/28e26b2b
Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/28e26b2b
Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/28e26b2b
Branch: refs/heads/master
Commit: 28e26b2b68f0fe999bc3d036e77ff2eed6a09c0b
Parents: 30a29e2
Author: Gary Gregory <ga...@gmail.com>
Authored: Fri Sep 22 10:50:24 2017 -0600
Committer: Gary Gregory <ga...@gmail.com>
Committed: Fri Sep 22 10:50:24 2017 -0600
----------------------------------------------------------------------
src/changes/changes.xml | 1 +
.../java/org/apache/commons/text/StrLookup.java | 96 +++++++++++++-------
.../org/apache/commons/text/StrLookupTest.java | 20 ++++
.../testResourceBundleLookup.properties | 2 +
4 files changed, 86 insertions(+), 33 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-text/blob/28e26b2b/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 7ceeafb..c558378 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -58,6 +58,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="TEXT-83" type="update" dev="chtompki" due-to="Amey Jadiye">Document that commons-csv should be used in preference to CsvTranslators</action>
<action issue="TEXT-67" type="update" dev="kinow">NumericEntityUnescaper.options - fix TODO</action>
<action issue="TEXT-84" type="update" dev="djones">RandomStringGenerator claims to be immutable, but isn't</action>
+ <action issue="TEXT-102" type="add" dev="ggregory">Add StrLookup.resourceBundleLookup(ResourceBundle)</action>
</release>
<release version="1.1" date="2017-05-23" description="Release 1.1">
http://git-wip-us.apache.org/repos/asf/commons-text/blob/28e26b2b/src/main/java/org/apache/commons/text/StrLookup.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/StrLookup.java b/src/main/java/org/apache/commons/text/StrLookup.java
index d5abf43..2344c82 100644
--- a/src/main/java/org/apache/commons/text/StrLookup.java
+++ b/src/main/java/org/apache/commons/text/StrLookup.java
@@ -17,19 +17,19 @@
package org.apache.commons.text;
import java.util.Map;
+import java.util.ResourceBundle;
/**
* Lookup a String key to a String value.
* <p>
- * This class represents the simplest form of a string to string map.
- * It has a benefit over a map in that it can create the result on
- * demand based on the key.
+ * This class represents the simplest form of a string to string map. It has a benefit over a map in that it can create
+ * the result on demand based on the key.
* <p>
- * This class comes complete with various factory methods.
- * If these do not suffice, you can subclass and implement your own matcher.
+ * This class comes complete with various factory methods. If these do not suffice, you can subclass and implement your
+ * own matcher.
* <p>
- * For example, it would be possible to implement a lookup that used the
- * key as a primary key, and looked up the value on demand from the database
+ * For example, it would be possible to implement a lookup that used the key as a primary key, and looked up the value
+ * on demand from the database
*
* @param <V> the type of the values supported by the lookup
* @since 1.0
@@ -46,7 +46,7 @@ public abstract class StrLookup<V> {
*/
private static final StrLookup<String> SYSTEM_PROPERTIES_LOOKUP = new SystemPropertiesStrLookup();
- //-----------------------------------------------------------------------
+ // -----------------------------------------------------------------------
/**
* Returns a lookup which always returns null.
*
@@ -57,11 +57,9 @@ public abstract class StrLookup<V> {
}
/**
- * Returns a new lookup which uses a copy of the current
- * {@link System#getProperties() System properties}.
+ * Returns a new lookup which uses a copy of the current {@link System#getProperties() System properties}.
* <p>
- * If a security manager blocked access to system properties, then null will
- * be returned from every lookup.
+ * If a security manager blocked access to system properties, then null will be returned from every lookup.
* <p>
* If a null key is used, this lookup will throw a NullPointerException.
*
@@ -74,18 +72,31 @@ public abstract class StrLookup<V> {
/**
* Returns a lookup which looks up values using a map.
* <p>
- * If the map is null, then null will be returned from every lookup.
- * The map result object is converted to a string using toString().
+ * If the map is null, then null will be returned from every lookup. The map result object is converted to a string
+ * using toString().
*
* @param <V> the type of the values supported by the lookup
- * @param map the map of keys to values, may be null
+ * @param map the map of keys to values, may be null
* @return a lookup using the map, not null
*/
public static <V> StrLookup<V> mapLookup(final Map<String, V> map) {
return new MapStrLookup<>(map);
}
- //-----------------------------------------------------------------------
+ /**
+ * Returns a lookup which looks up values using a ResourceBundle.
+ * <p>
+ * If the ResourceBundle is null, then null will be returned from every lookup. The map result object is converted
+ * to a string using toString().
+ *
+ * @param resourceBundle the map of keys to values, may be null
+ * @return a lookup using the map, not null
+ */
+ public static StrLookup<String> resourceBundleLookup(final ResourceBundle resourceBundle) {
+ return new ResourceBundleLookup(resourceBundle);
+ }
+
+ // -----------------------------------------------------------------------
/**
* Constructor.
*/
@@ -96,29 +107,28 @@ public abstract class StrLookup<V> {
/**
* Looks up a String key to a String value.
* <p>
- * The internal implementation may use any mechanism to return the value.
- * The simplest implementation is to use a Map. However, virtually any
- * implementation is possible.
+ * The internal implementation may use any mechanism to return the value. The simplest implementation is to use a
+ * Map. However, virtually any implementation is possible.
* <p>
- * For example, it would be possible to implement a lookup that used the
- * key as a primary key, and looked up the value on demand from the database
- * Or, a numeric based implementation could be created that treats the key
- * as an integer, increments the value and return the result as a string -
- * converting 1 to 2, 15 to 16 etc.
+ * For example, it would be possible to implement a lookup that used the key as a primary key, and looked up the
+ * value on demand from the database Or, a numeric based implementation could be created that treats the key as an
+ * integer, increments the value and return the result as a string - converting 1 to 2, 15 to 16 etc.
* <p>
- * The {@link #lookup(String)} method always returns a String, regardless of
- * the underlying data, by converting it as necessary. For example:
+ * The {@link #lookup(String)} method always returns a String, regardless of the underlying data, by converting it
+ * as necessary. For example:
+ *
* <pre>
* Map<String, Object> map = new HashMap<String, Object>();
* map.put("number", Integer.valueOf(2));
* assertEquals("2", StrLookup.mapLookup(map).lookup("number"));
* </pre>
- * @param key the key to be looked up, may be null
+ *
+ * @param key the key to be looked up, may be null
* @return the matching value, null if no match
*/
public abstract String lookup(String key);
- //-----------------------------------------------------------------------
+ // -----------------------------------------------------------------------
/**
* Lookup implementation that uses a Map.
*/
@@ -130,7 +140,7 @@ public abstract class StrLookup<V> {
/**
* Creates a new instance backed by a Map.
*
- * @param map the map of keys to values, may be null
+ * @param map the map of keys to values, may be null
*/
MapStrLookup(final Map<String, V> map) {
this.map = map;
@@ -139,10 +149,9 @@ public abstract class StrLookup<V> {
/**
* Looks up a String key to a String value using the map.
* <p>
- * If the map is null, then null is returned.
- * The map result object is converted to a string using toString().
+ * If the map is null, then null is returned. The map result object is converted to a string using toString().
*
- * @param key the key to be looked up, may be null
+ * @param key the key to be looked up, may be null
* @return the matching value, null if no match
*/
@Override
@@ -158,7 +167,28 @@ public abstract class StrLookup<V> {
}
}
- //-----------------------------------------------------------------------
+ // -----------------------------------------------------------------------
+ /**
+ * Lookup implementation based on a ResourceBundle.
+ */
+ static class ResourceBundleLookup extends StrLookup<String> {
+
+ private final ResourceBundle resourceBundle;
+
+ public ResourceBundleLookup(ResourceBundle resourceBundle) {
+ this.resourceBundle = resourceBundle;
+ }
+
+ @Override
+ public String lookup(String key) {
+ if (resourceBundle == null || key == null || !resourceBundle.containsKey(key)) {
+ return null;
+ }
+ return resourceBundle.getString(key);
+ }
+ }
+
+ // -----------------------------------------------------------------------
/**
* Lookup implementation based on system properties.
*/
http://git-wip-us.apache.org/repos/asf/commons-text/blob/28e26b2b/src/test/java/org/apache/commons/text/StrLookupTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/StrLookupTest.java b/src/test/java/org/apache/commons/text/StrLookupTest.java
index dfd8933..4ac1e88 100644
--- a/src/test/java/org/apache/commons/text/StrLookupTest.java
+++ b/src/test/java/org/apache/commons/text/StrLookupTest.java
@@ -22,6 +22,7 @@ import static org.junit.Assert.fail;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
+import java.util.ResourceBundle;
import org.junit.Test;
@@ -105,6 +106,24 @@ public class StrLookupTest {
}
@Test
+ public void testResourceBundleLookup() {
+ final ResourceBundle map = ResourceBundle.getBundle("testResourceBundleLookup");
+ assertEquals("value", StrLookup.resourceBundleLookup(map).lookup("key"));
+ assertEquals("2", StrLookup.resourceBundleLookup(map).lookup("number"));
+ assertEquals(null, StrLookup.resourceBundleLookup(map).lookup(null));
+ assertEquals(null, StrLookup.resourceBundleLookup(map).lookup(""));
+ assertEquals(null, StrLookup.resourceBundleLookup(map).lookup("other"));
+ }
+
+ @Test
+ public void testResourceBundleLookup_nullMap() {
+ final ResourceBundle resourceBundle = null;
+ assertEquals(null, StrLookup.resourceBundleLookup(resourceBundle).lookup(null));
+ assertEquals(null, StrLookup.resourceBundleLookup(resourceBundle).lookup(""));
+ assertEquals(null, StrLookup.resourceBundleLookup(resourceBundle).lookup("any"));
+ }
+
+ @Test
public void testMapLookup_nullMap() {
final Map<String, ?> map = null;
assertEquals(null, StrLookup.mapLookup(map).lookup(null));
@@ -112,4 +131,5 @@ public class StrLookupTest {
assertEquals(null, StrLookup.mapLookup(map).lookup("any"));
}
+
}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/28e26b2b/src/test/resources/testResourceBundleLookup.properties
----------------------------------------------------------------------
diff --git a/src/test/resources/testResourceBundleLookup.properties b/src/test/resources/testResourceBundleLookup.properties
new file mode 100644
index 0000000..f1394e7
--- /dev/null
+++ b/src/test/resources/testResourceBundleLookup.properties
@@ -0,0 +1,2 @@
+key = value
+number = 2