Skip to content

Commit 70cecfb

Browse files
committed
Interface StringLookup now extends UnaryOperator<String>
1 parent 3f05bf3 commit 70cecfb

35 files changed

+211
-173
lines changed

src/changes/changes.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ The <action> type attribute can be add,update,fix,remove.
4545
<title>Apache Commons Text Changes</title>
4646
</properties>
4747
<body>
48-
<release version="1.13.2" date="YYYY-MM-DD" description="Release 1.13.2. Requires Java 8 or above.">
48+
<release version="1.14.0" date="YYYY-MM-DD" description="Release 1.13.2. Requires Java 8 or above.">
4949
<!-- FIX -->
5050
<!-- ADD -->
51+
<action type="add" dev="ggregory" due-to="Gary Gregory">Interface StringLookup now extends UnaryOperator&lt;String&gt;.</action>
5152
<!-- UPDATE -->
5253
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump org.apache.commons:commons-parent from 81 to 84 #668.</action>
5354
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-io:commons-io from 2.18.0 to 2.19.0.</action>

src/main/java/org/apache/commons/text/StrSubstitutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ protected String resolveVariable(final String variableName,
874874
if (resolver == null) {
875875
return null;
876876
}
877-
return resolver.lookup(variableName);
877+
return resolver.apply(variableName);
878878
}
879879

880880
/**

src/main/java/org/apache/commons/text/StringSubstitutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,7 @@ protected String resolveVariable(final String variableName, final TextStringBuil
11521152
if (resolver == null) {
11531153
return null;
11541154
}
1155-
return resolver.lookup(variableName);
1155+
return resolver.apply(variableName);
11561156
}
11571157

11581158
/**

src/main/java/org/apache/commons/text/lookup/BiStringLookup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public interface BiStringLookup<U> extends StringLookup {
6868
* @return The matching value, null if no match.
6969
*/
7070
default String lookup(final String key, final U object) {
71-
return lookup(key);
71+
return apply(key);
7272
}
7373

7474
}

src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public String lookup(String key) {
124124
final StringLookup lookup = stringLookupMap.get(prefix);
125125
String value = null;
126126
if (lookup != null) {
127-
value = lookup.lookup(name);
127+
value = lookup.apply(name);
128128
}
129129

130130
if (value != null) {
@@ -133,7 +133,7 @@ public String lookup(String key) {
133133
key = key.substring(prefixPos + 1);
134134
}
135135
if (defaultStringLookup != null) {
136-
return defaultStringLookup.lookup(key);
136+
return defaultStringLookup.apply(key);
137137
}
138138
return null;
139139
}

src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ String getRuntime() {
146146
* @return a system property value.
147147
*/
148148
private String getSystemProperty(final String name) {
149-
return StringLookupFactory.INSTANCE_SYSTEM_PROPERTIES.lookup(name);
149+
return StringLookupFactory.INSTANCE_SYSTEM_PROPERTIES.apply(name);
150150
}
151151

152152
/**

src/main/java/org/apache/commons/text/lookup/StringLookup.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.apache.commons.text.lookup;
1919

20+
import java.util.function.UnaryOperator;
21+
2022
/**
2123
* Lookups a String key for a String value.
2224
* <p>
@@ -31,7 +33,38 @@
3133
* @since 1.3
3234
*/
3335
@FunctionalInterface
34-
public interface StringLookup {
36+
public interface StringLookup extends UnaryOperator<String> {
37+
38+
/**
39+
* Looks up a String key to provide a String value.
40+
* <p>
41+
* The internal implementation may use any mechanism to return the value. The simplest implementation is to use a
42+
* Map. However, virtually any implementation is possible.
43+
* </p>
44+
* <p>
45+
* For example, it would be possible to implement a lookup that used the key as a primary key, and looked up the
46+
* value on demand from the database Or, a numeric based implementation could be created that treats the key as an
47+
* integer, increments the value and return the result as a string - converting 1 to 2, 15 to 16 etc.
48+
* </p>
49+
* <p>
50+
* This method always returns a String, regardless of the underlying data, by converting it as necessary. For
51+
* example:
52+
* </p>
53+
*
54+
* <pre>
55+
* Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
56+
* map.put("number", Integer.valueOf(2));
57+
* assertEquals("2", StringLookupFactory.mapStringLookup(map).lookup("number"));
58+
* </pre>
59+
*
60+
* @param key the key to look up, may be null.
61+
* @return The matching value, null if no match.
62+
* @since 1.14.0
63+
*/
64+
@Override
65+
default String apply(String key) {
66+
return lookup(key);
67+
}
3568

3669
/**
3770
* Looks up a String key to provide a String value.
@@ -57,6 +90,8 @@ public interface StringLookup {
5790
*
5891
* @param key the key to look up, may be null.
5992
* @return The matching value, null if no match.
93+
* @deprecated Use {@link #apply(String)}.
6094
*/
95+
@Deprecated
6196
String lookup(String key);
6297
}

src/test/java/org/apache/commons/text/StrLookupTest.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,52 +40,52 @@ public void testMapLookup() {
4040
final Map<String, Object> map = new HashMap<>();
4141
map.put("key", "value");
4242
map.put("number", 2);
43-
assertEquals("value", StrLookup.mapLookup(map).lookup("key"));
44-
assertEquals("2", StrLookup.mapLookup(map).lookup("number"));
45-
assertNull(StrLookup.mapLookup(map).lookup(null));
46-
assertNull(StrLookup.mapLookup(map).lookup(""));
47-
assertNull(StrLookup.mapLookup(map).lookup("other"));
43+
assertEquals("value", StrLookup.mapLookup(map).apply("key"));
44+
assertEquals("2", StrLookup.mapLookup(map).apply("number"));
45+
assertNull(StrLookup.mapLookup(map).apply(null));
46+
assertNull(StrLookup.mapLookup(map).apply(""));
47+
assertNull(StrLookup.mapLookup(map).apply("other"));
4848
}
4949

5050
@Test
5151
public void testMapLookup_nullMap() {
5252
final Map<String, ?> map = null;
53-
assertNull(StrLookup.mapLookup(map).lookup(null));
54-
assertNull(StrLookup.mapLookup(map).lookup(""));
55-
assertNull(StrLookup.mapLookup(map).lookup("any"));
53+
assertNull(StrLookup.mapLookup(map).apply(null));
54+
assertNull(StrLookup.mapLookup(map).apply(""));
55+
assertNull(StrLookup.mapLookup(map).apply("any"));
5656
}
5757

5858
@Test
5959
public void testNoneLookup() {
60-
assertNull(StrLookup.noneLookup().lookup(null));
61-
assertNull(StrLookup.noneLookup().lookup(""));
62-
assertNull(StrLookup.noneLookup().lookup("any"));
60+
assertNull(StrLookup.noneLookup().apply(null));
61+
assertNull(StrLookup.noneLookup().apply(""));
62+
assertNull(StrLookup.noneLookup().apply("any"));
6363
}
6464

6565
@Test
6666
public void testResourceBundleLookup() {
6767
final ResourceBundle map = ResourceBundle.getBundle("org.apache.commons.text.example.testResourceBundleLookup");
68-
assertEquals("value", StrLookup.resourceBundleLookup(map).lookup("key"));
68+
assertEquals("value", StrLookup.resourceBundleLookup(map).apply("key"));
6969
assertEquals("2", StrLookup.resourceBundleLookup(map).lookup("number"));
70-
assertNull(StrLookup.resourceBundleLookup(map).lookup(null));
71-
assertNull(StrLookup.resourceBundleLookup(map).lookup(""));
72-
assertNull(StrLookup.resourceBundleLookup(map).lookup("other"));
70+
assertNull(StrLookup.resourceBundleLookup(map).apply(null));
71+
assertNull(StrLookup.resourceBundleLookup(map).apply(""));
72+
assertNull(StrLookup.resourceBundleLookup(map).apply("other"));
7373
}
7474

7575
@Test
7676
public void testResourceBundleLookup_nullMap() {
7777
final ResourceBundle resourceBundle = null;
78-
assertNull(StrLookup.resourceBundleLookup(resourceBundle).lookup(null));
79-
assertNull(StrLookup.resourceBundleLookup(resourceBundle).lookup(""));
80-
assertNull(StrLookup.resourceBundleLookup(resourceBundle).lookup("any"));
78+
assertNull(StrLookup.resourceBundleLookup(resourceBundle).apply(null));
79+
assertNull(StrLookup.resourceBundleLookup(resourceBundle).apply(""));
80+
assertNull(StrLookup.resourceBundleLookup(resourceBundle).apply("any"));
8181
}
8282

8383
@Test
8484
public void testSystemPropertiesLookup() {
85-
assertEquals(System.getProperty("os.name"), StrLookup.systemPropertiesLookup().lookup("os.name"));
86-
assertNull(StrLookup.systemPropertiesLookup().lookup(""));
87-
assertNull(StrLookup.systemPropertiesLookup().lookup("other"));
88-
assertThrows(NullPointerException.class, () -> StrLookup.systemPropertiesLookup().lookup(null));
85+
assertEquals(System.getProperty("os.name"), StrLookup.systemPropertiesLookup().apply("os.name"));
86+
assertNull(StrLookup.systemPropertiesLookup().apply(""));
87+
assertNull(StrLookup.systemPropertiesLookup().apply("other"));
88+
assertThrows(NullPointerException.class, () -> StrLookup.systemPropertiesLookup().apply(null));
8989
}
9090

9191
/**
@@ -104,7 +104,7 @@ public void testSystemPropertiesLookupReplacedProperties() {
104104
newProps.setProperty(osName, newOsName);
105105
System.setProperties(newProps);
106106
try {
107-
assertEquals(newOsName, sysLookup.lookup(osName), "Changed properties not detected");
107+
assertEquals(newOsName, sysLookup.apply(osName), "Changed properties not detected");
108108
} finally {
109109
System.setProperties(oldProperties);
110110
}
@@ -123,7 +123,7 @@ public void testSystemPropertiesLookupUpdatedProperty() {
123123
final StrLookup<String> sysLookup = StrLookup.systemPropertiesLookup();
124124
System.setProperty(osName, newOsName);
125125
try {
126-
assertEquals(newOsName, sysLookup.lookup(osName), "Changed properties not detected");
126+
assertEquals(newOsName, sysLookup.apply(osName), "Changed properties not detected");
127127
} finally {
128128
System.setProperty(osName, oldOs);
129129
}

src/test/java/org/apache/commons/text/StringSubstitutorTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ public void tearDown() throws Exception {
201201
public void testConstructorNullMap() {
202202
final Map<String, Object> parameters = null;
203203
final StringSubstitutor s = new StringSubstitutor(parameters, "prefix", "suffix");
204+
assertNull(s.getStringLookup().apply("X"));
204205
assertNull(s.getStringLookup().lookup("X"));
205206
}
206207

src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public void testDefaultInterpolator() {
130130
public void testDefaultValueForMissingKeyInResourceBundle() {
131131
final StringLookup interpolatorStringLookup = StringLookupFactory.INSTANCE.interpolatorStringLookup(
132132
StringLookupFactory.INSTANCE.resourceBundleStringLookup("org.apache.commons.text.example.testResourceBundleLookup"));
133+
assertEquals("${missingKey:-defaultValue}", interpolatorStringLookup.apply("keyWithMissingKey"));
133134
assertEquals("${missingKey:-defaultValue}", interpolatorStringLookup.lookup("keyWithMissingKey"));
134135
final StringSubstitutor stringSubstitutor = new StringSubstitutor(interpolatorStringLookup);
135136
// The following would throw a MissingResourceException before TEXT-165.

0 commit comments

Comments
 (0)