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 2022/09/24 14:08:30 UTC

[commons-text] branch master updated (ad930224 -> aec0a326)

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 ad930224 Workaround for https://github.com/siom79/japicmp/issues/265 no longer needed
     new 72dfb535 Reuse existing methods
     new 834adcb7 Better use of Stream and Map APIs
     new 9323ea8c Sort members
     new aec0a326 Bump junit-jupiter from 5.9.0 to 5.9.1

The 4 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:
 pom.xml                                            |   2 +-
 .../org/apache/commons/text/AlphabetConverter.java |  29 ++-
 .../apache/commons/text/ExtendedMessageFormat.java |   7 +-
 .../java/org/apache/commons/text/StrBuilder.java   |  25 +--
 .../org/apache/commons/text/TextStringBuilder.java |   9 +-
 .../org/apache/commons/text/diff/EditScript.java   |   4 +-
 .../text/lookup/InterpolatorStringLookup.java      |   9 +-
 .../commons/text/lookup/StringLookupFactory.java   | 212 ++++++++++-----------
 .../apache/commons/text/similarity/Counter.java    |   9 +-
 .../text/similarity/IntersectionSimilarity.java    |  32 ++--
 .../commons/text/similarity/RegexTokenizer.java    |   2 +-
 .../text/translate/AggregateTranslator.java        |   8 +-
 .../commons/text/translate/EntityArrays.java       |   8 +-
 ...ubstitutorWithInterpolatorStringLookupTest.java |  36 ++--
 .../text/lookup/StringLookupFactoryTest.java       | 196 +++++++++----------
 15 files changed, 273 insertions(+), 315 deletions(-)


[commons-text] 04/04: Bump junit-jupiter from 5.9.0 to 5.9.1

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 aec0a326c3659ec19c6d5b1bd446b05d83bfd81e
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Sep 24 10:08:25 2022 -0400

    Bump junit-jupiter from 5.9.0 to 5.9.1
---
 pom.xml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pom.xml b/pom.xml
index 0959780c..43eb2b21 100644
--- a/pom.xml
+++ b/pom.xml
@@ -47,6 +47,7 @@
     <commons.scmPubUrl>https://svn.apache.org/repos/infra/websites/production/commons/content/proper/commons-text</commons.scmPubUrl>
     <commons.scmPubCheckoutDirectory>site-content</commons.scmPubCheckoutDirectory>
 
+    <commons.junit.version>5.9.1</commons.junit.version>
     <checkstyle.plugin.version>3.2.0</checkstyle.plugin.version>
     <checkstyle.version>9.3</checkstyle.version>
 
@@ -93,7 +94,6 @@
     <dependency>
       <groupId>org.junit.jupiter</groupId>
       <artifactId>junit-jupiter</artifactId>
-      <version>5.9.1</version>
       <scope>test</scope>
     </dependency>
     <dependency>


[commons-text] 02/04: Better use of Stream and Map APIs

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 834adcb761a92700f7f2d6ecb45ff77bebf28384
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Sep 24 09:33:19 2022 -0400

    Better use of Stream and Map APIs
    
    Make some package private types final
---
 .../org/apache/commons/text/AlphabetConverter.java | 29 ++++++++------------
 .../apache/commons/text/ExtendedMessageFormat.java |  7 +----
 .../java/org/apache/commons/text/StrBuilder.java   |  4 +--
 .../org/apache/commons/text/diff/EditScript.java   |  4 +--
 .../text/lookup/InterpolatorStringLookup.java      |  9 ++----
 .../apache/commons/text/similarity/Counter.java    |  9 ++----
 .../text/similarity/IntersectionSimilarity.java    | 32 ++++++++++------------
 .../commons/text/similarity/RegexTokenizer.java    |  2 +-
 .../text/translate/AggregateTranslator.java        |  8 ++----
 .../commons/text/translate/EntityArrays.java       |  8 ++----
 10 files changed, 42 insertions(+), 70 deletions(-)

diff --git a/src/main/java/org/apache/commons/text/AlphabetConverter.java b/src/main/java/org/apache/commons/text/AlphabetConverter.java
index bad9460d..605d32ac 100644
--- a/src/main/java/org/apache/commons/text/AlphabetConverter.java
+++ b/src/main/java/org/apache/commons/text/AlphabetConverter.java
@@ -264,16 +264,13 @@ public final class AlphabetConverter {
      * @return The reconstructed AlphabetConverter
      * @see AlphabetConverter#getOriginalToEncoded()
      */
-    public static AlphabetConverter createConverterFromMap(
-            final Map<Integer, String> originalToEncoded) {
-        final Map<Integer, String> unmodifiableOriginalToEncoded =
-                Collections.unmodifiableMap(originalToEncoded);
+    public static AlphabetConverter createConverterFromMap(final Map<Integer, String> originalToEncoded) {
+        final Map<Integer, String> unmodifiableOriginalToEncoded = Collections.unmodifiableMap(originalToEncoded);
         final Map<String, String> encodedToOriginal = new LinkedHashMap<>();
 
         int encodedLetterLength = 1;
 
-        for (final Entry<Integer, String> e
-                : unmodifiableOriginalToEncoded.entrySet()) {
+        for (final Entry<Integer, String> e : unmodifiableOriginalToEncoded.entrySet()) {
             final String originalAsString = codePointToString(e.getKey());
             encodedToOriginal.put(e.getValue(), originalAsString);
 
@@ -282,9 +279,7 @@ public final class AlphabetConverter {
             }
         }
 
-        return new AlphabetConverter(unmodifiableOriginalToEncoded,
-                encodedToOriginal,
-                encodedLetterLength);
+        return new AlphabetConverter(unmodifiableOriginalToEncoded, encodedToOriginal, encodedLetterLength);
     }
 
     /**
@@ -508,14 +503,14 @@ public final class AlphabetConverter {
     @Override
     public String toString() {
         final StringBuilder sb = new StringBuilder();
-
-        for (final Entry<Integer, String> entry
-                : originalToEncoded.entrySet()) {
-            sb.append(codePointToString(entry.getKey()))
-                    .append(ARROW)
-                    .append(entry.getValue()).append(System.lineSeparator());
-        }
-
+        // @formatter:off
+        originalToEncoded.forEach((k, v) -> {
+            sb.append(codePointToString(k))
+              .append(ARROW)
+              .append(k)
+              .append(System.lineSeparator());
+        });
+        // @formatter:on
         return sb.toString();
     }
 }
diff --git a/src/main/java/org/apache/commons/text/ExtendedMessageFormat.java b/src/main/java/org/apache/commons/text/ExtendedMessageFormat.java
index 6b42afe7..1e149408 100644
--- a/src/main/java/org/apache/commons/text/ExtendedMessageFormat.java
+++ b/src/main/java/org/apache/commons/text/ExtendedMessageFormat.java
@@ -289,12 +289,7 @@ public class ExtendedMessageFormat extends MessageFormat {
         if (coll == null || coll.isEmpty()) {
             return false;
         }
-        for (final Object name : coll) {
-            if (name != null) {
-                return true;
-            }
-        }
-        return false;
+        return coll.stream().anyMatch(Objects::nonNull);
     }
 
     /**
diff --git a/src/main/java/org/apache/commons/text/StrBuilder.java b/src/main/java/org/apache/commons/text/StrBuilder.java
index f0c0099d..ef9b0176 100644
--- a/src/main/java/org/apache/commons/text/StrBuilder.java
+++ b/src/main/java/org/apache/commons/text/StrBuilder.java
@@ -767,9 +767,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable, Build
      */
     public StrBuilder appendAll(final Iterable<?> iterable) {
         if (iterable != null) {
-            for (final Object o : iterable) {
-                append(o);
-            }
+            iterable.forEach(this::append);
         }
         return this;
     }
diff --git a/src/main/java/org/apache/commons/text/diff/EditScript.java b/src/main/java/org/apache/commons/text/diff/EditScript.java
index 60de7136..06e8a1c3 100644
--- a/src/main/java/org/apache/commons/text/diff/EditScript.java
+++ b/src/main/java/org/apache/commons/text/diff/EditScript.java
@@ -127,9 +127,7 @@ public class EditScript<T> {
      * @param visitor  the visitor that will visit all commands in turn
      */
     public void visit(final CommandVisitor<T> visitor) {
-        for (final EditCommand<T> command : commands) {
-            command.accept(visitor);
-        }
+        commands.forEach(command -> command.accept(visitor));
     }
 
 }
diff --git a/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java b/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
index 72b36c31..c78e0017 100644
--- a/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
@@ -17,9 +17,9 @@
 package org.apache.commons.text.lookup;
 
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.stream.Collectors;
 
 /**
  * Proxies other {@link StringLookup}s using a keys within ${} markers using the format "${StringLookup:Key}".
@@ -63,12 +63,9 @@ class InterpolatorStringLookup extends AbstractStringLookup {
      * @param addDefaultLookups whether the default lookups should be used.
      */
     InterpolatorStringLookup(final Map<String, StringLookup> stringLookupMap, final StringLookup defaultStringLookup,
-        final boolean addDefaultLookups) {
+            final boolean addDefaultLookups) {
         this.defaultStringLookup = defaultStringLookup;
-        this.stringLookupMap = new HashMap<>(stringLookupMap.size());
-        for (final Entry<String, StringLookup> entry : stringLookupMap.entrySet()) {
-            this.stringLookupMap.put(StringLookupFactory.toKey(entry.getKey()), entry.getValue());
-        }
+        this.stringLookupMap = stringLookupMap.entrySet().stream().collect(Collectors.toMap(e -> StringLookupFactory.toKey(e.getKey()), Entry::getValue));
         if (addDefaultLookups) {
             StringLookupFactory.INSTANCE.addDefaultStringLookups(this.stringLookupMap);
         }
diff --git a/src/main/java/org/apache/commons/text/similarity/Counter.java b/src/main/java/org/apache/commons/text/similarity/Counter.java
index b49a1b30..b73a763a 100644
--- a/src/main/java/org/apache/commons/text/similarity/Counter.java
+++ b/src/main/java/org/apache/commons/text/similarity/Counter.java
@@ -33,7 +33,7 @@ import java.util.Map;
 final class Counter {
 
     /**
-     * It counts how many times each element provided occurred in an array and
+     * Counts how many times each element provided occurred in an array and
      * returns a dict with the element as key and the count as value.
      *
      * @param tokens array of tokens
@@ -42,12 +42,7 @@ final class Counter {
     public static Map<CharSequence, Integer> of(final CharSequence[] tokens) {
         final Map<CharSequence, Integer> innerCounter = new HashMap<>();
         for (final CharSequence token : tokens) {
-            if (innerCounter.containsKey(token)) {
-                int value = innerCounter.get(token);
-                innerCounter.put(token, ++value);
-            } else {
-                innerCounter.put(token, 1);
-            }
+            innerCounter.put(token, innerCounter.containsKey(token) ? innerCounter.get(token) + 1 : 1);
         }
         return innerCounter;
     }
diff --git a/src/main/java/org/apache/commons/text/similarity/IntersectionSimilarity.java b/src/main/java/org/apache/commons/text/similarity/IntersectionSimilarity.java
index b3bd4f34..496af222 100644
--- a/src/main/java/org/apache/commons/text/similarity/IntersectionSimilarity.java
+++ b/src/main/java/org/apache/commons/text/similarity/IntersectionSimilarity.java
@@ -36,12 +36,21 @@ import java.util.function.Function;
  * @see HashMap
  */
 public class IntersectionSimilarity<T> implements SimilarityScore<IntersectionResult> {
+
     /**
      * Mutable counter class for storing the count of elements.
      */
-    private static class BagCount {
-        /** The count. This is initialized to 1 upon construction. */
-        int count = 1;
+    private static final class BagCount {
+
+        /** Private, mutable but must be used as immutable. */
+        private static final BagCount ZERO = new BagCount();
+
+        private BagCount() {
+            this.count = 0;
+        }
+
+        /** The count. */
+        int count;
     }
 
     // The following is adapted from commons-collections for a Bag.
@@ -73,12 +82,7 @@ public class IntersectionSimilarity<T> implements SimilarityScore<IntersectionRe
          * @param object the object to add
          */
         void add(final T object) {
-            final BagCount mut = map.get(object);
-            if (mut == null) {
-                map.put(object, new BagCount());
-            } else {
-                mut.count++;
-            }
+            map.computeIfAbsent(object, k -> new BagCount()).count++;
         }
 
         /**
@@ -98,11 +102,7 @@ public class IntersectionSimilarity<T> implements SimilarityScore<IntersectionRe
          * @return The number of occurrences of the object, zero if not found
          */
         int getCount(final Object object) {
-            final BagCount count = map.get(object);
-            if (count != null) {
-                return count.count;
-            }
-            return 0;
+            return map.getOrDefault(object, BagCount.ZERO).count;
         }
 
         /**
@@ -231,9 +231,7 @@ public class IntersectionSimilarity<T> implements SimilarityScore<IntersectionRe
      */
     private TinyBag toBag(final Collection<T> objects) {
         final TinyBag bag = new TinyBag(objects.size());
-        for (final T t : objects) {
-            bag.add(t);
-        }
+        objects.forEach(bag::add);
         return bag;
     }
 }
diff --git a/src/main/java/org/apache/commons/text/similarity/RegexTokenizer.java b/src/main/java/org/apache/commons/text/similarity/RegexTokenizer.java
index af5a5a97..213e01c7 100644
--- a/src/main/java/org/apache/commons/text/similarity/RegexTokenizer.java
+++ b/src/main/java/org/apache/commons/text/similarity/RegexTokenizer.java
@@ -32,7 +32,7 @@ import org.apache.commons.lang3.Validate;
  *
  * @since 1.0
  */
-class RegexTokenizer implements Tokenizer<CharSequence> {
+final class RegexTokenizer implements Tokenizer<CharSequence> {
 
     /** The whitespace pattern. */
     private static final Pattern PATTERN = Pattern.compile("(\\w)+");
diff --git a/src/main/java/org/apache/commons/text/translate/AggregateTranslator.java b/src/main/java/org/apache/commons/text/translate/AggregateTranslator.java
index 53d404f6..e13fa576 100644
--- a/src/main/java/org/apache/commons/text/translate/AggregateTranslator.java
+++ b/src/main/java/org/apache/commons/text/translate/AggregateTranslator.java
@@ -20,6 +20,8 @@ import java.io.IOException;
 import java.io.Writer;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Objects;
+import java.util.stream.Stream;
 
 /**
  * Executes a sequence of translators one after the other. Execution ends whenever
@@ -41,11 +43,7 @@ public class AggregateTranslator extends CharSequenceTranslator {
      */
     public AggregateTranslator(final CharSequenceTranslator... translators) {
         if (translators != null) {
-            for (final CharSequenceTranslator translator : translators) {
-                if (translator != null) {
-                    this.translators.add(translator);
-                }
-            }
+            Stream.of(translators).filter(Objects::nonNull).forEach(this.translators::add);
         }
     }
 
diff --git a/src/main/java/org/apache/commons/text/translate/EntityArrays.java b/src/main/java/org/apache/commons/text/translate/EntityArrays.java
index 73f9e473..6420af5b 100644
--- a/src/main/java/org/apache/commons/text/translate/EntityArrays.java
+++ b/src/main/java/org/apache/commons/text/translate/EntityArrays.java
@@ -19,6 +19,8 @@ package org.apache.commons.text.translate;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Map.Entry;
+import java.util.stream.Collectors;
 
 /**
  * Class holding various entity data for HTML and XML - generally for use with
@@ -442,11 +444,7 @@ public class EntityArrays {
      * @return Map&lt;String, String&gt; inverted array
      */
     public static Map<CharSequence, CharSequence> invert(final Map<CharSequence, CharSequence> map) {
-        final Map<CharSequence, CharSequence> newMap = new HashMap<>();
-        for (final Map.Entry<CharSequence, CharSequence> pair : map.entrySet()) {
-            newMap.put(pair.getValue(), pair.getKey());
-        }
-        return newMap;
+        return map.entrySet().stream().collect(Collectors.toMap(Entry::getValue, Entry::getKey));
     }
 
 }


[commons-text] 03/04: 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 9323ea8c20569b34eeb0bd5c2260f602e4af5e3f
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Sep 24 09:36:55 2022 -0400

    Sort members
---
 .../commons/text/lookup/StringLookupFactory.java   | 212 ++++++++++-----------
 .../text/similarity/IntersectionSimilarity.java    |   6 +-
 ...ubstitutorWithInterpolatorStringLookupTest.java |  36 ++--
 .../text/lookup/StringLookupFactoryTest.java       | 196 +++++++++----------
 4 files changed, 225 insertions(+), 225 deletions(-)

diff --git a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
index bd397f87..bc98f75d 100644
--- a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
+++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
@@ -205,6 +205,103 @@ import org.apache.commons.text.StringSubstitutor;
  */
 public final class StringLookupFactory {
 
+    /**
+     * Internal class used to construct the default {@link StringLookup} map used by
+     * {@link StringLookupFactory#addDefaultStringLookups(Map)}.
+     */
+    static final class DefaultStringLookupsHolder {
+
+        /** Singleton instance, initialized with the system properties. */
+        static final DefaultStringLookupsHolder INSTANCE = new DefaultStringLookupsHolder(System.getProperties());
+
+        /**
+         * Add the key and string lookup from {@code lookup} to {@code map}, also adding any additional
+         * key aliases if needed. Keys are normalized using the {@link #toKey(String)} method.
+         * @param lookup lookup to add
+         * @param map map to add to
+         */
+        private static void addLookup(final DefaultStringLookup lookup, final Map<String, StringLookup> map) {
+            map.put(toKey(lookup.getKey()), lookup.getStringLookup());
+
+            if (DefaultStringLookup.BASE64_DECODER.equals(lookup)) {
+                // "base64" is deprecated in favor of KEY_BASE64_DECODER.
+                map.put(toKey("base64"), lookup.getStringLookup());
+            }
+        }
+
+        /**
+         * Create the lookup map used when the user has requested no customization.
+         * @return default lookup map
+         */
+        private static Map<String, StringLookup> createDefaultStringLookups() {
+            final Map<String, StringLookup> lookupMap = new HashMap<>();
+
+            addLookup(DefaultStringLookup.BASE64_DECODER, lookupMap);
+            addLookup(DefaultStringLookup.BASE64_ENCODER, lookupMap);
+            addLookup(DefaultStringLookup.CONST, lookupMap);
+            addLookup(DefaultStringLookup.DATE, lookupMap);
+            addLookup(DefaultStringLookup.ENVIRONMENT, lookupMap);
+            addLookup(DefaultStringLookup.FILE, lookupMap);
+            addLookup(DefaultStringLookup.JAVA, lookupMap);
+            addLookup(DefaultStringLookup.LOCAL_HOST, lookupMap);
+            addLookup(DefaultStringLookup.PROPERTIES, lookupMap);
+            addLookup(DefaultStringLookup.RESOURCE_BUNDLE, lookupMap);
+            addLookup(DefaultStringLookup.SYSTEM_PROPERTIES, lookupMap);
+            addLookup(DefaultStringLookup.URL_DECODER, lookupMap);
+            addLookup(DefaultStringLookup.URL_ENCODER, lookupMap);
+            addLookup(DefaultStringLookup.XML, lookupMap);
+
+            return lookupMap;
+        }
+
+        /**
+         * Construct a lookup map by parsing the given string. The string is expected to contain
+         * comma or space-separated names of values from the {@link DefaultStringLookup} enum. If
+         * the given string is null or empty, an empty map is returned.
+         * @param str string to parse; may be null or empty
+         * @return lookup map parsed from the given string
+         */
+        private static Map<String, StringLookup> parseStringLookups(final String str) {
+            final Map<String, StringLookup> lookupMap = new HashMap<>();
+
+            try {
+                for (final String lookupName : str.split("[\\s,]+")) {
+                    if (!lookupName.isEmpty()) {
+                        addLookup(DefaultStringLookup.valueOf(lookupName.toUpperCase()), lookupMap);
+                    }
+                }
+            } catch (IllegalArgumentException exc) {
+                throw new IllegalArgumentException("Invalid default string lookups definition: " + str, exc);
+            }
+
+            return lookupMap;
+        }
+
+        /** Default string lookup map. */
+        private final Map<String, StringLookup> defaultStringLookups;
+
+        /**
+         * Construct a new instance initialized with the given properties.
+         * @param props initialization properties
+         */
+        DefaultStringLookupsHolder(final Properties props) {
+            final Map<String, StringLookup> lookups =
+                    props.containsKey(StringLookupFactory.DEFAULT_STRING_LOOKUPS_PROPERTY)
+                        ? parseStringLookups(props.getProperty(StringLookupFactory.DEFAULT_STRING_LOOKUPS_PROPERTY))
+                        : createDefaultStringLookups();
+
+            defaultStringLookups = Collections.unmodifiableMap(lookups);
+        }
+
+        /**
+         * Get the default string lookups map.
+         * @return default string lookups map
+         */
+        Map<String, StringLookup> getDefaultStringLookups() {
+            return defaultStringLookups;
+        }
+    }
+
     /**
      * Defines the singleton for this class.
      */
@@ -429,6 +526,15 @@ public final class StringLookupFactory {
         ConstantStringLookup.clear();
     }
 
+    /**
+     * Get a string suitable for use as a key in the string lookup map.
+     * @param key string to convert to a string lookup map key
+     * @return string lookup map key
+     */
+    static String toKey(final String key) {
+        return key.toLowerCase(Locale.ROOT);
+    }
+
     /**
      * Returns the given map if the input is non-null or an empty immutable map if the input is null.
      *
@@ -1210,110 +1316,4 @@ public final class StringLookupFactory {
     public StringLookup xmlStringLookup() {
         return XmlStringLookup.INSTANCE;
     }
-
-    /**
-     * Get a string suitable for use as a key in the string lookup map.
-     * @param key string to convert to a string lookup map key
-     * @return string lookup map key
-     */
-    static String toKey(final String key) {
-        return key.toLowerCase(Locale.ROOT);
-    }
-
-    /**
-     * Internal class used to construct the default {@link StringLookup} map used by
-     * {@link StringLookupFactory#addDefaultStringLookups(Map)}.
-     */
-    static final class DefaultStringLookupsHolder {
-
-        /** Singleton instance, initialized with the system properties. */
-        static final DefaultStringLookupsHolder INSTANCE = new DefaultStringLookupsHolder(System.getProperties());
-
-        /** Default string lookup map. */
-        private final Map<String, StringLookup> defaultStringLookups;
-
-        /**
-         * Construct a new instance initialized with the given properties.
-         * @param props initialization properties
-         */
-        DefaultStringLookupsHolder(final Properties props) {
-            final Map<String, StringLookup> lookups =
-                    props.containsKey(StringLookupFactory.DEFAULT_STRING_LOOKUPS_PROPERTY)
-                        ? parseStringLookups(props.getProperty(StringLookupFactory.DEFAULT_STRING_LOOKUPS_PROPERTY))
-                        : createDefaultStringLookups();
-
-            defaultStringLookups = Collections.unmodifiableMap(lookups);
-        }
-
-        /**
-         * Get the default string lookups map.
-         * @return default string lookups map
-         */
-        Map<String, StringLookup> getDefaultStringLookups() {
-            return defaultStringLookups;
-        }
-
-        /**
-         * Create the lookup map used when the user has requested no customization.
-         * @return default lookup map
-         */
-        private static Map<String, StringLookup> createDefaultStringLookups() {
-            final Map<String, StringLookup> lookupMap = new HashMap<>();
-
-            addLookup(DefaultStringLookup.BASE64_DECODER, lookupMap);
-            addLookup(DefaultStringLookup.BASE64_ENCODER, lookupMap);
-            addLookup(DefaultStringLookup.CONST, lookupMap);
-            addLookup(DefaultStringLookup.DATE, lookupMap);
-            addLookup(DefaultStringLookup.ENVIRONMENT, lookupMap);
-            addLookup(DefaultStringLookup.FILE, lookupMap);
-            addLookup(DefaultStringLookup.JAVA, lookupMap);
-            addLookup(DefaultStringLookup.LOCAL_HOST, lookupMap);
-            addLookup(DefaultStringLookup.PROPERTIES, lookupMap);
-            addLookup(DefaultStringLookup.RESOURCE_BUNDLE, lookupMap);
-            addLookup(DefaultStringLookup.SYSTEM_PROPERTIES, lookupMap);
-            addLookup(DefaultStringLookup.URL_DECODER, lookupMap);
-            addLookup(DefaultStringLookup.URL_ENCODER, lookupMap);
-            addLookup(DefaultStringLookup.XML, lookupMap);
-
-            return lookupMap;
-        }
-
-        /**
-         * Construct a lookup map by parsing the given string. The string is expected to contain
-         * comma or space-separated names of values from the {@link DefaultStringLookup} enum. If
-         * the given string is null or empty, an empty map is returned.
-         * @param str string to parse; may be null or empty
-         * @return lookup map parsed from the given string
-         */
-        private static Map<String, StringLookup> parseStringLookups(final String str) {
-            final Map<String, StringLookup> lookupMap = new HashMap<>();
-
-            try {
-                for (final String lookupName : str.split("[\\s,]+")) {
-                    if (!lookupName.isEmpty()) {
-                        addLookup(DefaultStringLookup.valueOf(lookupName.toUpperCase()), lookupMap);
-                    }
-                }
-            } catch (IllegalArgumentException exc) {
-                throw new IllegalArgumentException("Invalid default string lookups definition: " + str, exc);
-            }
-
-            return lookupMap;
-        }
-
-        /**
-         * Add the key and string lookup from {@code lookup} to {@code map}, also adding any additional
-         * key aliases if needed. Keys are normalized using the {@link #toKey(String)} method.
-         * @param lookup lookup to add
-         * @param map map to add to
-         */
-        private static void addLookup(final DefaultStringLookup lookup, final Map<String, StringLookup> map) {
-            map.put(toKey(lookup.getKey()), lookup.getStringLookup());
-
-            if (DefaultStringLookup.BASE64_DECODER.equals(lookup)) {
-                // "base64" is deprecated in favor of KEY_BASE64_DECODER.
-                map.put(toKey("base64"), lookup.getStringLookup());
-            }
-        }
-    }
 }
diff --git a/src/main/java/org/apache/commons/text/similarity/IntersectionSimilarity.java b/src/main/java/org/apache/commons/text/similarity/IntersectionSimilarity.java
index 496af222..c62e9b5f 100644
--- a/src/main/java/org/apache/commons/text/similarity/IntersectionSimilarity.java
+++ b/src/main/java/org/apache/commons/text/similarity/IntersectionSimilarity.java
@@ -45,12 +45,12 @@ public class IntersectionSimilarity<T> implements SimilarityScore<IntersectionRe
         /** Private, mutable but must be used as immutable. */
         private static final BagCount ZERO = new BagCount();
 
+        /** The count. */
+        int count;
+
         private BagCount() {
             this.count = 0;
         }
-
-        /** The count. */
-        int count;
     }
 
     // The following is adapted from commons-collections for a Bag.
diff --git a/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java b/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
index e19e7f0d..f9ccc623 100644
--- a/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
+++ b/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
@@ -32,6 +32,15 @@ import org.junit.jupiter.api.Test;
 
 public class StringSubstitutorWithInterpolatorStringLookupTest {
 
+    private static StringLookup createInterpolatorWithLookups(final DefaultStringLookup... lookups) {
+        final Map<String, StringLookup> lookupMap = new HashMap<>();
+        for (final DefaultStringLookup lookup : lookups) {
+            lookupMap.put(lookup.getKey().toLowerCase(), lookup.getStringLookup());
+        }
+
+        return StringLookupFactory.INSTANCE.interpolatorStringLookup(lookupMap, null, false);
+    }
+
     @Test
     public void testCustomFunctionWithDefaults() {
         testCustomFunctionWithDefaults(true);
@@ -86,7 +95,6 @@ public class StringSubstitutorWithInterpolatorStringLookupTest {
     public void testCustomMapWithoutDefaults() {
         testCustomMapWithDefaults(false);
     }
-
     @Test
     public void testDefaultInterpolator() {
         // Used to cut and paste into the docs.
@@ -117,6 +125,7 @@ public class StringSubstitutorWithInterpolatorStringLookupTest {
         Assertions.assertFalse(text.contains("${urlEncoder:Hello World!}"));
         Assertions.assertFalse(text.contains("${resourceBundle:org.apache.commons.text.example.testResourceBundleLookup:mykey}"));
     }
+
     @Test
     public void testDefaultValueForMissingKeyInResourceBundle() {
         final StringLookup interpolatorStringLookup = StringLookupFactory.INSTANCE.interpolatorStringLookup(
@@ -136,6 +145,14 @@ public class StringSubstitutorWithInterpolatorStringLookupTest {
             strSubst.replace("${dns:" + hostName + "}"));
     }
 
+    @Test
+    public void testDnsLookup_disabledByDefault() throws UnknownHostException {
+        final StringSubstitutor strSubst = StringSubstitutor.createInterpolator();
+        final String hostName = InetAddress.getLocalHost().getHostName();
+        final String input = "${dns:" + hostName + "}";
+        Assertions.assertEquals(input, strSubst.replace(input));
+    }
+
     @Test
     public void testDnsLookupAddress() throws UnknownHostException {
         final StringSubstitutor strSubst =
@@ -180,14 +197,6 @@ public class StringSubstitutorWithInterpolatorStringLookupTest {
         Assertions.assertEquals(unknown, strSubst.replace(unknown));
     }
 
-    @Test
-    public void testDnsLookup_disabledByDefault() throws UnknownHostException {
-        final StringSubstitutor strSubst = StringSubstitutor.createInterpolator();
-        final String hostName = InetAddress.getLocalHost().getHostName();
-        final String input = "${dns:" + hostName + "}";
-        Assertions.assertEquals(input, strSubst.replace(input));
-    }
-
     @Test
     public void testJavaScript() {
         final StringSubstitutor strSubst =
@@ -251,13 +260,4 @@ public class StringSubstitutorWithInterpolatorStringLookupTest {
         Assertions.assertEquals(System.getProperty(spKey), strSubst.replace("${" + spKey + "}"));
         Assertions.assertEquals(System.getProperty(spKey), strSubst.replace("${sys:" + spKey + "}"));
     }
-
-    private static StringLookup createInterpolatorWithLookups(final DefaultStringLookup... lookups) {
-        final Map<String, StringLookup> lookupMap = new HashMap<>();
-        for (final DefaultStringLookup lookup : lookups) {
-            lookupMap.put(lookup.getKey().toLowerCase(), lookup.getStringLookup());
-        }
-
-        return StringLookupFactory.INSTANCE.interpolatorStringLookup(lookupMap, null, false);
-    }
 }
diff --git a/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java b/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java
index 20614bfb..682cdbd9 100644
--- a/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java
+++ b/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java
@@ -50,6 +50,42 @@ public class StringLookupFactoryTest {
                 StringLookupFactory.KEY_XML);
     }
 
+    private static void assertMappedLookups(final Map<String, StringLookup> lookupMap, final String... keys) {
+        final Set<String> remainingKeys = new HashSet<>(lookupMap.keySet());
+
+        for (final String key : keys) {
+            final String normalizedKey = StringLookupFactory.toKey(key);
+            Assertions.assertNotNull(normalizedKey, () -> "Expected map to contain string lookup for key " + key);
+
+            remainingKeys.remove(normalizedKey);
+        }
+
+        Assertions.assertTrue(remainingKeys.isEmpty(), () -> "Unexpected keys in lookup map: " + remainingKeys);
+    }
+
+    private static void checkDefaultStringLookupsHolder(final Properties props, final String... keys) {
+        final StringLookupFactory.DefaultStringLookupsHolder holder =
+                new StringLookupFactory.DefaultStringLookupsHolder(props);
+
+        final Map<String, StringLookup> lookupMap = holder.getDefaultStringLookups();
+
+        assertMappedLookups(lookupMap, keys);
+    }
+
+    /**
+     * Main method used to verify the default string lookups resolved during JVM execution.
+     * @param args
+     */
+    public static void main(final String[] args) {
+        final Map<String, StringLookup> lookupMap = new HashMap<>();
+        StringLookupFactory.INSTANCE.addDefaultStringLookups(lookupMap);
+
+        System.out.println("Default string lookups");
+        for (final String key : lookupMap.keySet()) {
+            System.out.println("- " + key);
+        }
+    }
+
     @Test
     public void testAddDefaultStringLookupsMap() {
         final Map<String, StringLookup> stringLookupMap = new HashMap<>();
@@ -62,38 +98,15 @@ public class StringLookupFactoryTest {
         StringLookupFactory.INSTANCE.addDefaultStringLookups(null);
     }
 
-    /**
-     * Tests that we return the singleton.
-     */
     @Test
-    public void testSingletons() {
-        final StringLookupFactory stringLookupFactory = StringLookupFactory.INSTANCE;
-        Assertions.assertSame(StringLookupFactory.INSTANCE_BASE64_DECODER,
-            stringLookupFactory.base64DecoderStringLookup());
-        Assertions.assertSame(StringLookupFactory.INSTANCE_BASE64_ENCODER,
-            stringLookupFactory.base64EncoderStringLookup());
-        Assertions.assertSame(ConstantStringLookup.INSTANCE, stringLookupFactory.constantStringLookup());
-        Assertions.assertSame(DateStringLookup.INSTANCE, stringLookupFactory.dateStringLookup());
-        Assertions.assertSame(DnsStringLookup.INSTANCE, stringLookupFactory.dnsStringLookup());
-        Assertions.assertSame(StringLookupFactory.INSTANCE_ENVIRONMENT_VARIABLES,
-            stringLookupFactory.environmentVariableStringLookup());
-        Assertions.assertSame(InterpolatorStringLookup.INSTANCE, stringLookupFactory.interpolatorStringLookup());
-        Assertions.assertSame(JavaPlatformStringLookup.INSTANCE, stringLookupFactory.javaPlatformStringLookup());
-        Assertions.assertSame(LocalHostStringLookup.INSTANCE, stringLookupFactory.localHostStringLookup());
-        Assertions.assertSame(StringLookupFactory.INSTANCE_NULL, stringLookupFactory.nullStringLookup());
-        Assertions.assertSame(ResourceBundleStringLookup.INSTANCE, stringLookupFactory.resourceBundleStringLookup());
-        Assertions.assertSame(ScriptStringLookup.INSTANCE, stringLookupFactory.scriptStringLookup());
-        Assertions.assertSame(StringLookupFactory.INSTANCE_SYSTEM_PROPERTIES,
-            stringLookupFactory.systemPropertyStringLookup());
-        Assertions.assertSame(UrlDecoderStringLookup.INSTANCE, stringLookupFactory.urlDecoderStringLookup());
-        Assertions.assertSame(UrlEncoderStringLookup.INSTANCE, stringLookupFactory.urlEncoderStringLookup());
-        Assertions.assertSame(UrlStringLookup.INSTANCE, stringLookupFactory.urlStringLookup());
-        Assertions.assertSame(XmlStringLookup.INSTANCE, stringLookupFactory.xmlStringLookup());
-    }
+    public void testDefaultStringLookupsHolder_allLookups() {
+        final Properties props = new Properties();
+        props.setProperty(StringLookupFactory.DEFAULT_STRING_LOOKUPS_PROPERTY,
+                "BASE64_DECODER BASE64_ENCODER const, date, dns, environment "
+                + "file ,java, local_host properties, resource_bundle,script,system_properties "
+                + "url url_decoder  , url_encoder, xml");
 
-    @Test
-    public void testDefaultStringLookupsHolder_lookupsPropertyNotPresent() {
-        checkDefaultStringLookupsHolder(new Properties(),
+        checkDefaultStringLookupsHolder(props,
                 "base64",
                 StringLookupFactory.KEY_BASE64_DECODER,
                 StringLookupFactory.KEY_BASE64_ENCODER,
@@ -108,20 +121,11 @@ public class StringLookupFactoryTest {
                 StringLookupFactory.KEY_SYS,
                 StringLookupFactory.KEY_URL_DECODER,
                 StringLookupFactory.KEY_URL_ENCODER,
-                StringLookupFactory.KEY_XML);
-    }
-
-    @Test
-    public void testDefaultStringLookupsHolder_lookupsPropertyEmptyAndBlank() {
-        final Properties propsWithNull = new Properties();
-        propsWithNull.setProperty(StringLookupFactory.DEFAULT_STRING_LOOKUPS_PROPERTY, "");
-
-        checkDefaultStringLookupsHolder(propsWithNull);
-
-        final Properties propsWithBlank = new Properties();
-        propsWithBlank.setProperty(StringLookupFactory.DEFAULT_STRING_LOOKUPS_PROPERTY, " ");
+                StringLookupFactory.KEY_XML,
 
-        checkDefaultStringLookupsHolder(propsWithBlank);
+                StringLookupFactory.KEY_DNS,
+                StringLookupFactory.KEY_URL,
+                StringLookupFactory.KEY_SCRIPT);
     }
 
     @Test
@@ -143,25 +147,31 @@ public class StringLookupFactoryTest {
     }
 
     @Test
-    public void testDefaultStringLookupsHolder_multipleLookups() {
+    public void testDefaultStringLookupsHolder_invalidLookupsDefinition() {
         final Properties props = new Properties();
-        props.setProperty(StringLookupFactory.DEFAULT_STRING_LOOKUPS_PROPERTY, "dns, url script ");
+        props.setProperty(StringLookupFactory.DEFAULT_STRING_LOOKUPS_PROPERTY, "base64_encoder nope");
 
-        checkDefaultStringLookupsHolder(props,
-                StringLookupFactory.KEY_DNS,
-                StringLookupFactory.KEY_URL,
-                StringLookupFactory.KEY_SCRIPT);
+        final Exception exc = Assertions.assertThrows(IllegalArgumentException.class,
+                () -> new StringLookupFactory.DefaultStringLookupsHolder(props));
+        Assertions.assertEquals("Invalid default string lookups definition: base64_encoder nope", exc.getMessage());
     }
 
     @Test
-    public void testDefaultStringLookupsHolder_allLookups() {
-        final Properties props = new Properties();
-        props.setProperty(StringLookupFactory.DEFAULT_STRING_LOOKUPS_PROPERTY,
-                "BASE64_DECODER BASE64_ENCODER const, date, dns, environment "
-                + "file ,java, local_host properties, resource_bundle,script,system_properties "
-                + "url url_decoder  , url_encoder, xml");
+    public void testDefaultStringLookupsHolder_lookupsPropertyEmptyAndBlank() {
+        final Properties propsWithNull = new Properties();
+        propsWithNull.setProperty(StringLookupFactory.DEFAULT_STRING_LOOKUPS_PROPERTY, "");
 
-        checkDefaultStringLookupsHolder(props,
+        checkDefaultStringLookupsHolder(propsWithNull);
+
+        final Properties propsWithBlank = new Properties();
+        propsWithBlank.setProperty(StringLookupFactory.DEFAULT_STRING_LOOKUPS_PROPERTY, " ");
+
+        checkDefaultStringLookupsHolder(propsWithBlank);
+    }
+
+    @Test
+    public void testDefaultStringLookupsHolder_lookupsPropertyNotPresent() {
+        checkDefaultStringLookupsHolder(new Properties(),
                 "base64",
                 StringLookupFactory.KEY_BASE64_DECODER,
                 StringLookupFactory.KEY_BASE64_ENCODER,
@@ -176,56 +186,46 @@ public class StringLookupFactoryTest {
                 StringLookupFactory.KEY_SYS,
                 StringLookupFactory.KEY_URL_DECODER,
                 StringLookupFactory.KEY_URL_ENCODER,
-                StringLookupFactory.KEY_XML,
-
-                StringLookupFactory.KEY_DNS,
-                StringLookupFactory.KEY_URL,
-                StringLookupFactory.KEY_SCRIPT);
+                StringLookupFactory.KEY_XML);
     }
 
     @Test
-    public void testDefaultStringLookupsHolder_invalidLookupsDefinition() {
+    public void testDefaultStringLookupsHolder_multipleLookups() {
         final Properties props = new Properties();
-        props.setProperty(StringLookupFactory.DEFAULT_STRING_LOOKUPS_PROPERTY, "base64_encoder nope");
-
-        final Exception exc = Assertions.assertThrows(IllegalArgumentException.class,
-                () -> new StringLookupFactory.DefaultStringLookupsHolder(props));
-        Assertions.assertEquals("Invalid default string lookups definition: base64_encoder nope", exc.getMessage());
-    }
-
-    private static void checkDefaultStringLookupsHolder(final Properties props, final String... keys) {
-        final StringLookupFactory.DefaultStringLookupsHolder holder =
-                new StringLookupFactory.DefaultStringLookupsHolder(props);
-
-        final Map<String, StringLookup> lookupMap = holder.getDefaultStringLookups();
-
-        assertMappedLookups(lookupMap, keys);
-    }
-
-    private static void assertMappedLookups(final Map<String, StringLookup> lookupMap, final String... keys) {
-        final Set<String> remainingKeys = new HashSet<>(lookupMap.keySet());
-
-        for (final String key : keys) {
-            final String normalizedKey = StringLookupFactory.toKey(key);
-            Assertions.assertNotNull(normalizedKey, () -> "Expected map to contain string lookup for key " + key);
-
-            remainingKeys.remove(normalizedKey);
-        }
+        props.setProperty(StringLookupFactory.DEFAULT_STRING_LOOKUPS_PROPERTY, "dns, url script ");
 
-        Assertions.assertTrue(remainingKeys.isEmpty(), () -> "Unexpected keys in lookup map: " + remainingKeys);
+        checkDefaultStringLookupsHolder(props,
+                StringLookupFactory.KEY_DNS,
+                StringLookupFactory.KEY_URL,
+                StringLookupFactory.KEY_SCRIPT);
     }
 
     /**
-     * Main method used to verify the default string lookups resolved during JVM execution.
-     * @param args
+     * Tests that we return the singleton.
      */
-    public static void main(final String[] args) {
-        final Map<String, StringLookup> lookupMap = new HashMap<>();
-        StringLookupFactory.INSTANCE.addDefaultStringLookups(lookupMap);
-
-        System.out.println("Default string lookups");
-        for (final String key : lookupMap.keySet()) {
-            System.out.println("- " + key);
-        }
+    @Test
+    public void testSingletons() {
+        final StringLookupFactory stringLookupFactory = StringLookupFactory.INSTANCE;
+        Assertions.assertSame(StringLookupFactory.INSTANCE_BASE64_DECODER,
+            stringLookupFactory.base64DecoderStringLookup());
+        Assertions.assertSame(StringLookupFactory.INSTANCE_BASE64_ENCODER,
+            stringLookupFactory.base64EncoderStringLookup());
+        Assertions.assertSame(ConstantStringLookup.INSTANCE, stringLookupFactory.constantStringLookup());
+        Assertions.assertSame(DateStringLookup.INSTANCE, stringLookupFactory.dateStringLookup());
+        Assertions.assertSame(DnsStringLookup.INSTANCE, stringLookupFactory.dnsStringLookup());
+        Assertions.assertSame(StringLookupFactory.INSTANCE_ENVIRONMENT_VARIABLES,
+            stringLookupFactory.environmentVariableStringLookup());
+        Assertions.assertSame(InterpolatorStringLookup.INSTANCE, stringLookupFactory.interpolatorStringLookup());
+        Assertions.assertSame(JavaPlatformStringLookup.INSTANCE, stringLookupFactory.javaPlatformStringLookup());
+        Assertions.assertSame(LocalHostStringLookup.INSTANCE, stringLookupFactory.localHostStringLookup());
+        Assertions.assertSame(StringLookupFactory.INSTANCE_NULL, stringLookupFactory.nullStringLookup());
+        Assertions.assertSame(ResourceBundleStringLookup.INSTANCE, stringLookupFactory.resourceBundleStringLookup());
+        Assertions.assertSame(ScriptStringLookup.INSTANCE, stringLookupFactory.scriptStringLookup());
+        Assertions.assertSame(StringLookupFactory.INSTANCE_SYSTEM_PROPERTIES,
+            stringLookupFactory.systemPropertyStringLookup());
+        Assertions.assertSame(UrlDecoderStringLookup.INSTANCE, stringLookupFactory.urlDecoderStringLookup());
+        Assertions.assertSame(UrlEncoderStringLookup.INSTANCE, stringLookupFactory.urlEncoderStringLookup());
+        Assertions.assertSame(UrlStringLookup.INSTANCE, stringLookupFactory.urlStringLookup());
+        Assertions.assertSame(XmlStringLookup.INSTANCE, stringLookupFactory.xmlStringLookup());
     }
 }


[commons-text] 01/04: Reuse existing methods

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 72dfb53537c8fac6f229c6678d33b3ecd2a76a22
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Sep 24 08:17:10 2022 -0400

    Reuse existing methods
---
 .../java/org/apache/commons/text/StrBuilder.java    | 21 +++++++--------------
 .../org/apache/commons/text/TextStringBuilder.java  |  9 +--------
 2 files changed, 8 insertions(+), 22 deletions(-)

diff --git a/src/main/java/org/apache/commons/text/StrBuilder.java b/src/main/java/org/apache/commons/text/StrBuilder.java
index e5530a31..f0c0099d 100644
--- a/src/main/java/org/apache/commons/text/StrBuilder.java
+++ b/src/main/java/org/apache/commons/text/StrBuilder.java
@@ -1364,14 +1364,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable, Build
      */
     public StrBuilder appendWithSeparators(final Iterable<?> iterable, final String separator) {
         if (iterable != null) {
-            final String sep = Objects.toString(separator, StringUtils.EMPTY);
-            final Iterator<?> it = iterable.iterator();
-            while (it.hasNext()) {
-                append(it.next());
-                if (it.hasNext()) {
-                    append(sep);
-                }
-            }
+            appendWithSeparators(iterable.iterator(), separator);
         }
         return this;
     }
@@ -1382,16 +1375,16 @@ public class StrBuilder implements CharSequence, Appendable, Serializable, Build
      * Appending a null iterator will have no effect.
      * Each object is appended using {@link #append(Object)}.
      *
-     * @param it  the iterator to append
+     * @param iterator  the iterator to append
      * @param separator  the separator to use, null means no separator
      * @return this, to enable chaining
      */
-    public StrBuilder appendWithSeparators(final Iterator<?> it, final String separator) {
-        if (it != null) {
+    public StrBuilder appendWithSeparators(final Iterator<?> iterator, final String separator) {
+        if (iterator != null) {
             final String sep = Objects.toString(separator, StringUtils.EMPTY);
-            while (it.hasNext()) {
-                append(it.next());
-                if (it.hasNext()) {
+            while (iterator.hasNext()) {
+                append(iterator.next());
+                if (iterator.hasNext()) {
                     append(sep);
                 }
             }
diff --git a/src/main/java/org/apache/commons/text/TextStringBuilder.java b/src/main/java/org/apache/commons/text/TextStringBuilder.java
index 21805783..1c63f828 100644
--- a/src/main/java/org/apache/commons/text/TextStringBuilder.java
+++ b/src/main/java/org/apache/commons/text/TextStringBuilder.java
@@ -1377,14 +1377,7 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
      */
     public TextStringBuilder appendWithSeparators(final Iterable<?> iterable, final String separator) {
         if (iterable != null) {
-            final String sep = Objects.toString(separator, StringUtils.EMPTY);
-            final Iterator<?> it = iterable.iterator();
-            while (it.hasNext()) {
-                append(it.next());
-                if (it.hasNext()) {
-                    append(sep);
-                }
-            }
+            appendWithSeparators(iterable.iterator(), separator);
         }
         return this;
     }