[TEXT-169] Add helper factory method
org.apache.commons.text.StringSubstitutor.createInterpolator().
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 6ac5fea..b66bb0a 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -48,6 +48,7 @@
<release version="1.8" date="2019-MM-DD" description="Release 1.8">
<action issue="TEXT-167" type="fix" dev="ggregory" due-to="Larry West">commons-text web page missing "RELEASE-NOTES-1.7.txt"</action>
<action issue="TEXT-168" type="fix" dev="ggregory" due-to="luksan47">(doc) Fixed wrong value for Jaro-Winkler example #117</action>
+ <action issue="TEXT-169" type="add" dev="ggregory" due-to="Gary Gregory">Add helper factory method org.apache.commons.text.StringSubstitutor.createInterpolator().</action>
</release>
<release version="1.7" date="2019-06-30" description="Release 1.7">
diff --git a/src/main/java/org/apache/commons/text/StringSubstitutor.java b/src/main/java/org/apache/commons/text/StringSubstitutor.java
index 6341b0c..fa33d61 100644
--- a/src/main/java/org/apache/commons/text/StringSubstitutor.java
+++ b/src/main/java/org/apache/commons/text/StringSubstitutor.java
@@ -181,7 +181,17 @@
public static final StringMatcher DEFAULT_VALUE_DELIMITER =
StringMatcherFactory.INSTANCE.stringMatcher(DEFAULT_VAR_DEFAULT);
- // -----------------------------------------------------------------------
+ /**
+ * Creates a new instance using the interpolator string lookup {@link StringLookupFactory#interpolatorStringLookup()}.
+ *
+ * @return a new instance using the interpolator string lookup.
+ * @see StringLookupFactory#interpolatorStringLookup()
+ * @since 1.8
+ */
+ public static StringSubstitutor createInterpolator() {
+ return new StringSubstitutor(StringLookupFactory.INSTANCE.interpolatorStringLookup());
+ }
+
/**
* Replaces all the occurrences of variables in the given source object with their matching values from the map.
*
diff --git a/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java b/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
index ec592fb..e002f22 100644
--- a/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
+++ b/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
@@ -70,23 +70,20 @@
@Test
public void testLocalHostLookup_Address() throws UnknownHostException {
- final StringSubstitutor strSubst = new StringSubstitutor(
- StringLookupFactory.INSTANCE.interpolatorStringLookup());
+ final StringSubstitutor strSubst = StringSubstitutor.createInterpolator();
Assertions.assertEquals(InetAddress.getLocalHost().getHostAddress(), strSubst.replace("${localhost:address}"));
}
@Test
public void testLocalHostLookup_CanonicalName() throws UnknownHostException {
- final StringSubstitutor strSubst = new StringSubstitutor(
- StringLookupFactory.INSTANCE.interpolatorStringLookup());
+ final StringSubstitutor strSubst = StringSubstitutor.createInterpolator();
Assertions.assertEquals(InetAddress.getLocalHost().getCanonicalHostName(),
strSubst.replace("${localhost:canonical-name}"));
}
@Test
public void testLocalHostLookup_Name() throws UnknownHostException {
- final StringSubstitutor strSubst = new StringSubstitutor(
- StringLookupFactory.INSTANCE.interpolatorStringLookup());
+ final StringSubstitutor strSubst = StringSubstitutor.createInterpolator();
Assertions.assertEquals(InetAddress.getLocalHost().getHostName(), strSubst.replace("${localhost:name}"));
}
@@ -105,8 +102,7 @@
@Test
public void testSystemProperty() {
- final StringSubstitutor strSubst = new StringSubstitutor(
- StringLookupFactory.INSTANCE.interpolatorStringLookup());
+ final StringSubstitutor strSubst = StringSubstitutor.createInterpolator();
final String spKey = "user.name";
Assertions.assertEquals(System.getProperty(spKey), strSubst.replace("${sys:" + spKey + "}"));
}
diff --git a/src/test/java/org/apache/commons/text/lookup/PropertiesStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/PropertiesStringLookupTest.java
index 69aa78e..c57348b 100644
--- a/src/test/java/org/apache/commons/text/lookup/PropertiesStringLookupTest.java
+++ b/src/test/java/org/apache/commons/text/lookup/PropertiesStringLookupTest.java
@@ -35,15 +35,13 @@
@Test
public void testInterpolator() {
- final StringSubstitutor stringSubstitutor = new StringSubstitutor(
- StringLookupFactory.INSTANCE.interpolatorStringLookup());
+ final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
Assertions.assertEquals("Hello World!", stringSubstitutor.replace("${properties:" + KEY_PATH + "}"));
}
@Test
public void testInterpolatorNestedColon() {
- final StringSubstitutor stringSubstitutor = new StringSubstitutor(
- StringLookupFactory.INSTANCE.interpolatorStringLookup());
+ final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
// Need to handle "C:" in the sys prop user.dir.
final String replaced = stringSubstitutor.replace("$${properties:${sys:user.dir}/" + KEY_PATH + "}");
Assertions.assertEquals(