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 2019/07/26 13:37:42 UTC

[commons-text] branch master updated (e29ecb7 -> 67a6372)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git.


    from e29ecb7  Fix the site's source repository link.
     new 6de9942  Sort members.
     new 67a6372  [TEXT-169] Add helper factory method org.apache.commons.text.StringSubstitutor.createInterpolator().

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/changes/changes.xml                            |   1 +
 .../org/apache/commons/text/StringSubstitutor.java | 164 +++++++++++----------
 ...ubstitutorWithInterpolatorStringLookupTest.java |  12 +-
 .../text/lookup/PropertiesStringLookupTest.java    |   6 +-
 4 files changed, 94 insertions(+), 89 deletions(-)


[commons-text] 02/02: [TEXT-169] Add helper factory method org.apache.commons.text.StringSubstitutor.createInterpolator().

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git

commit 67a6372dbe665d1791357763ea3699dd2dfa4711
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Jul 26 09:37:37 2019 -0400

    [TEXT-169] Add helper factory method
    org.apache.commons.text.StringSubstitutor.createInterpolator().
---
 src/changes/changes.xml                                      |  1 +
 src/main/java/org/apache/commons/text/StringSubstitutor.java | 12 +++++++++++-
 .../StringSubstitutorWithInterpolatorStringLookupTest.java   | 12 ++++--------
 .../commons/text/lookup/PropertiesStringLookupTest.java      |  6 ++----
 4 files changed, 18 insertions(+), 13 deletions(-)

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 @@ The <action> type attribute can be add,update,fix,remove.
   <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 class StringSubstitutor {
     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 @@ public class StringSubstitutorWithInterpolatorStringLookupTest {
 
     @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 @@ public class StringSubstitutorWithInterpolatorStringLookupTest {
 
     @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 @@ public class PropertiesStringLookupTest {
 
     @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(


[commons-text] 01/02: Sort members.

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git

commit 6de99420fa4c0f180b5bfbe6a7764227c4d42450
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Jul 26 09:23:41 2019 -0400

    Sort members.
---
 .../org/apache/commons/text/StringSubstitutor.java | 152 ++++++++++-----------
 1 file changed, 76 insertions(+), 76 deletions(-)

diff --git a/src/main/java/org/apache/commons/text/StringSubstitutor.java b/src/main/java/org/apache/commons/text/StringSubstitutor.java
index f4b53ee..6341b0c 100644
--- a/src/main/java/org/apache/commons/text/StringSubstitutor.java
+++ b/src/main/java/org/apache/commons/text/StringSubstitutor.java
@@ -748,52 +748,6 @@ public class StringSubstitutor {
     // -----------------------------------------------------------------------
     /**
      * Replaces all the occurrences of variables with their matching values from the resolver using the given source
-     * builder as a template. The builder is not altered by this method.
-     *
-     * @param source
-     *            the builder to use as a template, not changed, null returns null
-     * @return the result of the replace operation
-     * @throws IllegalArgumentException
-     *             if variable is not found when its allowed to throw exception
-     */
-    public String replace(final TextStringBuilder source) {
-        if (source == null) {
-            return null;
-        }
-        final TextStringBuilder buf = new TextStringBuilder(source.length()).append(source);
-        substitute(buf, 0, buf.length());
-        return buf.toString();
-    }
-
-    /**
-     * Replaces all the occurrences of variables with their matching values from the resolver using the given source
-     * builder as a template. The builder is not altered by this method.
-     * <p>
-     * Only the specified portion of the builder will be processed. The rest of the builder is not processed, and is not
-     * returned.
-     *
-     * @param source
-     *            the builder to use as a template, not changed, null returns null
-     * @param offset
-     *            the start offset within the array, must be valid
-     * @param length
-     *            the length within the array to be processed, must be valid
-     * @return the result of the replace operation
-      * @throws IllegalArgumentException
-     *             if variable is not found when its allowed to throw exception
-     */
-    public String replace(final TextStringBuilder source, final int offset, final int length) {
-        if (source == null) {
-            return null;
-        }
-        final TextStringBuilder buf = new TextStringBuilder(length).append(source, offset, length);
-        substitute(buf, 0, length);
-        return buf.toString();
-    }
-
-    // -----------------------------------------------------------------------
-    /**
-     * Replaces all the occurrences of variables with their matching values from the resolver using the given source
      * string as a template.
      *
      * @param source
@@ -889,44 +843,48 @@ public class StringSubstitutor {
 
     // -----------------------------------------------------------------------
     /**
-     * Replaces all the occurrences of variables within the given source builder with their matching values from the
-     * resolver.
+     * Replaces all the occurrences of variables with their matching values from the resolver using the given source
+     * builder as a template. The builder is not altered by this method.
      *
      * @param source
-     *            the builder to replace in, updated, null returns zero
-     * @return true if altered
+     *            the builder to use as a template, not changed, null returns null
+     * @return the result of the replace operation
      * @throws IllegalArgumentException
      *             if variable is not found when its allowed to throw exception
      */
-    public boolean replaceIn(final TextStringBuilder source) {
+    public String replace(final TextStringBuilder source) {
         if (source == null) {
-            return false;
+            return null;
         }
-        return substitute(source, 0, source.length());
+        final TextStringBuilder buf = new TextStringBuilder(source.length()).append(source);
+        substitute(buf, 0, buf.length());
+        return buf.toString();
     }
 
     /**
-     * Replaces all the occurrences of variables within the given source builder with their matching values from the
-     * resolver.
+     * Replaces all the occurrences of variables with their matching values from the resolver using the given source
+     * builder as a template. The builder is not altered by this method.
      * <p>
-     * Only the specified portion of the builder will be processed. The rest of the builder is not processed, but it is
-     * not deleted.
+     * Only the specified portion of the builder will be processed. The rest of the builder is not processed, and is not
+     * returned.
      *
      * @param source
-     *            the builder to replace in, null returns zero
+     *            the builder to use as a template, not changed, null returns null
      * @param offset
      *            the start offset within the array, must be valid
      * @param length
-     *            the length within the builder to be processed, must be valid
-     * @return true if altered
-     * @throws IllegalArgumentException
+     *            the length within the array to be processed, must be valid
+     * @return the result of the replace operation
+      * @throws IllegalArgumentException
      *             if variable is not found when its allowed to throw exception
      */
-    public boolean replaceIn(final TextStringBuilder source, final int offset, final int length) {
+    public String replace(final TextStringBuilder source, final int offset, final int length) {
         if (source == null) {
-            return false;
+            return null;
         }
-        return substitute(source, offset, length);
+        final TextStringBuilder buf = new TextStringBuilder(length).append(source, offset, length);
+        substitute(buf, 0, length);
+        return buf.toString();
     }
 
     // -----------------------------------------------------------------------
@@ -1019,6 +977,48 @@ public class StringSubstitutor {
         return true;
     }
 
+    // -----------------------------------------------------------------------
+    /**
+     * Replaces all the occurrences of variables within the given source builder with their matching values from the
+     * resolver.
+     *
+     * @param source
+     *            the builder to replace in, updated, null returns zero
+     * @return true if altered
+     * @throws IllegalArgumentException
+     *             if variable is not found when its allowed to throw exception
+     */
+    public boolean replaceIn(final TextStringBuilder source) {
+        if (source == null) {
+            return false;
+        }
+        return substitute(source, 0, source.length());
+    }
+
+    /**
+     * Replaces all the occurrences of variables within the given source builder with their matching values from the
+     * resolver.
+     * <p>
+     * Only the specified portion of the builder will be processed. The rest of the builder is not processed, but it is
+     * not deleted.
+     *
+     * @param source
+     *            the builder to replace in, null returns zero
+     * @param offset
+     *            the start offset within the array, must be valid
+     * @param length
+     *            the length within the builder to be processed, must be valid
+     * @return true if altered
+     * @throws IllegalArgumentException
+     *             if variable is not found when its allowed to throw exception
+     */
+    public boolean replaceIn(final TextStringBuilder source, final int offset, final int length) {
+        if (source == null) {
+            return false;
+        }
+        return substitute(source, offset, length);
+    }
+
     /**
      * Internal method that resolves the value of a variable.
      * <p>
@@ -1061,18 +1061,6 @@ public class StringSubstitutor {
     }
 
     /**
-     * Sets a flag whether exception should be thrown if any variable is undefined.
-     *
-     * @param failOnUndefinedVariable
-     *            true if exception should be thrown on undefined variable
-     * @return this, to enable chaining
-     */
-    public StringSubstitutor setEnableUndefinedVariableException(final boolean failOnUndefinedVariable) {
-        this.enableUndefinedVariableException = failOnUndefinedVariable;
-        return this;
-    }
-
-    /**
      * Sets a flag whether substitution is done in variable names. If set to <b>true</b>, the names of variables can
      * contain other variables which are processed first before the original variable is evaluated, e.g.
      * <code>${jre-${java.version}}</code>. The default value is <b>false</b>.
@@ -1087,6 +1075,18 @@ public class StringSubstitutor {
     }
 
     /**
+     * Sets a flag whether exception should be thrown if any variable is undefined.
+     *
+     * @param failOnUndefinedVariable
+     *            true if exception should be thrown on undefined variable
+     * @return this, to enable chaining
+     */
+    public StringSubstitutor setEnableUndefinedVariableException(final boolean failOnUndefinedVariable) {
+        this.enableUndefinedVariableException = failOnUndefinedVariable;
+        return this;
+    }
+
+    /**
      * Sets the escape character. If this character is placed before a variable reference in the source text, this
      * variable will be ignored.
      *