Repository: commons-text Updated Branches: refs/heads/master 0f06a6bc7 -> 388fa72c1
[TEXT-113] Add an interpolator string lookup. No long needs to subclass StrLookup. Fix Checkstyle errors. Add Javadoc comments. Clean ups. Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/388fa72c Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/388fa72c Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/388fa72c Branch: refs/heads/master Commit: 388fa72c18f12771aeb7621046fa4f5462d9af60 Parents: 0f06a6b Author: Gary Gregory <garydgreg...@gmail.com> Authored: Sun Feb 11 11:09:16 2018 -0700 Committer: Gary Gregory <garydgreg...@gmail.com> Committed: Sun Feb 11 11:09:16 2018 -0700 ---------------------------------------------------------------------- .../commons/text/diff/ReplacementsFinder.java | 4 +- .../text/lookup/AbstractStringLookup.java | 8 +--- .../commons/text/lookup/DateStringLookup.java | 11 +++++- .../lookup/EnvironmentVariableStringLookup.java | 2 +- .../text/lookup/IllegalArgumentExceptions.java | 20 ++++++++++ .../text/lookup/InterpolatorStringLookup.java | 4 ++ .../text/lookup/JavaPlatformStringLookup.java | 41 +++++++++++++++----- .../commons/text/lookup/MapStringLookup.java | 9 ++++- .../commons/text/lookup/NullStringLookup.java | 2 +- .../text/lookup/ResourceBundleStringLookup.java | 2 +- .../text/lookup/StringLookupFactory.java | 2 +- .../text/lookup/SystemPropertyStringLookup.java | 2 +- .../commons/text/lookup/package-info.java | 29 ++++++++++++++ 13 files changed, 111 insertions(+), 25 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-text/blob/388fa72c/src/main/java/org/apache/commons/text/diff/ReplacementsFinder.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/diff/ReplacementsFinder.java b/src/main/java/org/apache/commons/text/diff/ReplacementsFinder.java index 2c58ef5..004e496 100644 --- a/src/main/java/org/apache/commons/text/diff/ReplacementsFinder.java +++ b/src/main/java/org/apache/commons/text/diff/ReplacementsFinder.java @@ -56,12 +56,12 @@ public class ReplacementsFinder<T> implements CommandVisitor<T> { * List of pending insertions. */ private final List<T> pendingInsertions; - + /** * List of pending deletions. */ private final List<T> pendingDeletions; - + /** * Count of elements skipped. */ http://git-wip-us.apache.org/repos/asf/commons-text/blob/388fa72c/src/main/java/org/apache/commons/text/lookup/AbstractStringLookup.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/lookup/AbstractStringLookup.java b/src/main/java/org/apache/commons/text/lookup/AbstractStringLookup.java index 4b224c1..563d165 100644 --- a/src/main/java/org/apache/commons/text/lookup/AbstractStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/AbstractStringLookup.java @@ -27,11 +27,5 @@ package org.apache.commons.text.lookup; * @since 1.3 */ public abstract class AbstractStringLookup implements StringLookup { - - protected static final String EMPTY = ""; - - protected boolean isEmpty(final String value) { - return value == null ? true : value.isEmpty(); - } - + // nothing yet } http://git-wip-us.apache.org/repos/asf/commons-text/blob/388fa72c/src/main/java/org/apache/commons/text/lookup/DateStringLookup.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/lookup/DateStringLookup.java b/src/main/java/org/apache/commons/text/lookup/DateStringLookup.java index fe69198..de37188 100644 --- a/src/main/java/org/apache/commons/text/lookup/DateStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/DateStringLookup.java @@ -28,7 +28,7 @@ final class DateStringLookup extends AbstractStringLookup { /** * Defines the singleton for this class. */ - public static final StringLookup INSTANCE = new DateStringLookup(); + static final DateStringLookup INSTANCE = new DateStringLookup(); /** * No need to build instances for now. @@ -37,6 +37,15 @@ final class DateStringLookup extends AbstractStringLookup { // empty } + /** + * Formats the given {@code date} long with the given {@code format}. + * + * @param date + * the date to format + * @param format + * the format string for {@link SimpleDateFormat}. + * @return the formatted date + */ private String formatDate(final long date, final String format) { DateFormat dateFormat = null; if (format != null) { http://git-wip-us.apache.org/repos/asf/commons-text/blob/388fa72c/src/main/java/org/apache/commons/text/lookup/EnvironmentVariableStringLookup.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/lookup/EnvironmentVariableStringLookup.java b/src/main/java/org/apache/commons/text/lookup/EnvironmentVariableStringLookup.java index 23200c5..e6e5d49 100644 --- a/src/main/java/org/apache/commons/text/lookup/EnvironmentVariableStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/EnvironmentVariableStringLookup.java @@ -26,7 +26,7 @@ final class EnvironmentVariableStringLookup extends AbstractStringLookup { /** * Defines the singleton for this class. */ - public static final StringLookup INSTANCE = new EnvironmentVariableStringLookup(); + static final EnvironmentVariableStringLookup INSTANCE = new EnvironmentVariableStringLookup(); /** * No need to build instances for now. http://git-wip-us.apache.org/repos/asf/commons-text/blob/388fa72c/src/main/java/org/apache/commons/text/lookup/IllegalArgumentExceptions.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/lookup/IllegalArgumentExceptions.java b/src/main/java/org/apache/commons/text/lookup/IllegalArgumentExceptions.java index 0ead53e..585fe56 100644 --- a/src/main/java/org/apache/commons/text/lookup/IllegalArgumentExceptions.java +++ b/src/main/java/org/apache/commons/text/lookup/IllegalArgumentExceptions.java @@ -24,10 +24,30 @@ package org.apache.commons.text.lookup; */ final class IllegalArgumentExceptions { + /** + * Creates an {@link IllegalArgumentException} with a message formated with {@link String#format(String,Object...)}. + * + * @param format + * See {@link String#format(String,Object...)} + * @param args + * See {@link String#format(String,Object...)} + * @return an {@link IllegalArgumentException} with a message formated with {@link String#format(String,Object...)} + */ static IllegalArgumentException format(final String format, final Object... args) { return new IllegalArgumentException(String.format(format, args)); } + /** + * Creates an {@link IllegalArgumentException} with a message formated with {@link String#format(String,Object...)}. + * + * @param t + * the throwable cause + * @param format + * See {@link String#format(String,Object...)} + * @param args + * See {@link String#format(String,Object...)} + * @return an {@link IllegalArgumentException} with a message formated with {@link String#format(String,Object...)} + */ static IllegalArgumentException format(final Throwable t, final String format, final Object... args) { return new IllegalArgumentException(String.format(format, args), t); } http://git-wip-us.apache.org/repos/asf/commons-text/blob/388fa72c/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java ---------------------------------------------------------------------- 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 c175f67..ccf823b 100644 --- a/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java @@ -28,8 +28,10 @@ class InterpolatorStringLookup extends AbstractStringLookup { /** Constant for the prefix separator. */ private static final char PREFIX_SEPARATOR = ':'; + /** The default string lookup. */ private final StringLookup defaultStringLookup; + /** The map of String lookups keyed by prefix. */ private final Map<String, StringLookup> stringLookupMap = new HashMap<>(); /** @@ -60,6 +62,8 @@ class InterpolatorStringLookup extends AbstractStringLookup { * <li>"date" for the DateStringLookup.</li> * </ul> * + * @param <V> + * the map's value type. * @param defaultMap * the default map for string lookups. */ http://git-wip-us.apache.org/repos/asf/commons-text/blob/388fa72c/src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java b/src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java index 35f7607..ee7f556 100644 --- a/src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java @@ -18,6 +18,8 @@ package org.apache.commons.text.lookup; import java.util.Locale; +import org.apache.commons.lang3.StringUtils; + /** * Looks up keys related to Java: Java version, JRE version, VM version, and so on. * @@ -28,7 +30,7 @@ final class JavaPlatformStringLookup extends AbstractStringLookup { /** * Defines the singleton for this class. */ - public static final StringLookup INSTANCE = new JavaPlatformStringLookup(); + static final JavaPlatformStringLookup INSTANCE = new JavaPlatformStringLookup(); /** * No need to build instances for now. @@ -39,9 +41,10 @@ final class JavaPlatformStringLookup extends AbstractStringLookup { /** * Accessible through the Lookup key {@code hardware}. + * * @return hardware processor information. */ - public String getHardware() { + String getHardware() { return "processors: " + Runtime.getRuntime().availableProcessors() + ", architecture: " + getSystemProperty("os.arch") + this.getSystemProperty("-", "sun.arch.data.model") + this.getSystemProperty(", instruction sets: ", "sun.cpu.isalist"); @@ -49,17 +52,19 @@ final class JavaPlatformStringLookup extends AbstractStringLookup { /** * Accessible through the Lookup key {@code locale}. + * * @return system locale and file encoding information. */ - public String getLocale() { + String getLocale() { return "default locale: " + Locale.getDefault() + ", platform encoding: " + getSystemProperty("file.encoding"); } /** * Accessible through the Lookup key {@code os}. + * * @return operating system information. */ - public String getOperatingSystem() { + String getOperatingSystem() { return getSystemProperty("os.name") + " " + getSystemProperty("os.version") + getSystemProperty(" ", "sun.os.patch.level") + ", architecture: " + getSystemProperty("os.arch") + getSystemProperty("-", "sun.arch.data.model"); @@ -67,30 +72,48 @@ final class JavaPlatformStringLookup extends AbstractStringLookup { /** * Accessible through the Lookup key {@code runtime}. + * * @return Java Runtime Environment information. */ - public String getRuntime() { + String getRuntime() { return getSystemProperty("java.runtime.name") + " (build " + getSystemProperty("java.runtime.version") + ") from " + getSystemProperty("java.vendor"); } + /** + * Gets the given system property. + * + * @param name + * a system property name. + * @return a system property value. + */ private String getSystemProperty(final String name) { return SystemPropertyStringLookup.INSTANCE.lookup(name); } + /** + * Gets the given system property. + * + * @param prefix + * the prefix to use for the result string + * @param name + * a system property name. + * @return the prefix + a system property value. + */ private String getSystemProperty(final String prefix, final String name) { final String value = getSystemProperty(name); - if (isEmpty(value)) { - return EMPTY; + if (StringUtils.isEmpty(value)) { + return StringUtils.EMPTY; } return prefix + value; } /** * Accessible through the Lookup key {@code vm}. + * * @return Java Virtual Machine information. */ - public String getVirtualMachine() { + String getVirtualMachine() { return getSystemProperty("java.vm.name") + " (build " + getSystemProperty("java.vm.version") + ", " + getSystemProperty("java.vm.info") + ")"; } @@ -99,7 +122,7 @@ final class JavaPlatformStringLookup extends AbstractStringLookup { * Looks up the value of the Java platform key. * * @param key - * the key to be looked up, may be null + * the key to be looked up, may be null * @return The value of the environment variable. */ @Override http://git-wip-us.apache.org/repos/asf/commons-text/blob/388fa72c/src/main/java/org/apache/commons/text/lookup/MapStringLookup.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/lookup/MapStringLookup.java b/src/main/java/org/apache/commons/text/lookup/MapStringLookup.java index 4c1a4aa..b85bcf5 100644 --- a/src/main/java/org/apache/commons/text/lookup/MapStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/MapStringLookup.java @@ -31,6 +31,8 @@ public final class MapStringLookup<V> implements StringLookup { /** * Creates a new instance backed by a Map. Used by the default lookup. * + * @param <T> + * the map's value type. * @param map * the map of keys to values, may be null. * @return a new instance backed by the given map. @@ -54,7 +56,12 @@ public final class MapStringLookup<V> implements StringLookup { this.map = map; } - protected Map<String, V> getMap() { + /** + * Gets the map used in lookups. + * + * @return the map used in lookups. + */ + Map<String, V> getMap() { return map; } http://git-wip-us.apache.org/repos/asf/commons-text/blob/388fa72c/src/main/java/org/apache/commons/text/lookup/NullStringLookup.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/lookup/NullStringLookup.java b/src/main/java/org/apache/commons/text/lookup/NullStringLookup.java index 7c97d1e..8190139 100644 --- a/src/main/java/org/apache/commons/text/lookup/NullStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/NullStringLookup.java @@ -27,7 +27,7 @@ final class NullStringLookup extends AbstractStringLookup { /** * Defines the singleton for this class. */ - public static final StringLookup INSTANCE = new NullStringLookup(); + static final NullStringLookup INSTANCE = new NullStringLookup(); /** * No need to build instances for now. http://git-wip-us.apache.org/repos/asf/commons-text/blob/388fa72c/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java b/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java index ead8e63..2ef3112 100644 --- a/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java @@ -30,7 +30,7 @@ final class ResourceBundleStringLookup extends AbstractStringLookup { /** * Defines the singleton for this class. */ - public static final StringLookup INSTANCE = new ResourceBundleStringLookup(); + static final ResourceBundleStringLookup INSTANCE = new ResourceBundleStringLookup(); /** * No need to build instances for now. http://git-wip-us.apache.org/repos/asf/commons-text/blob/388fa72c/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java ---------------------------------------------------------------------- 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 58b0f3b..b1f3700 100644 --- a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java +++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java @@ -24,7 +24,7 @@ import java.util.Map; * * @since 1.3 */ -public class StringLookupFactory { +public final class StringLookupFactory { /** * Defines the singleton for this class. http://git-wip-us.apache.org/repos/asf/commons-text/blob/388fa72c/src/main/java/org/apache/commons/text/lookup/SystemPropertyStringLookup.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/lookup/SystemPropertyStringLookup.java b/src/main/java/org/apache/commons/text/lookup/SystemPropertyStringLookup.java index 1ae627b..d0ed869 100644 --- a/src/main/java/org/apache/commons/text/lookup/SystemPropertyStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/SystemPropertyStringLookup.java @@ -26,7 +26,7 @@ final class SystemPropertyStringLookup extends AbstractStringLookup { /** * Defines the singleton for this class. */ - public static final StringLookup INSTANCE = new SystemPropertyStringLookup(); + static final SystemPropertyStringLookup INSTANCE = new SystemPropertyStringLookup(); /** * No need to build instances for now. http://git-wip-us.apache.org/repos/asf/commons-text/blob/388fa72c/src/main/java/org/apache/commons/text/lookup/package-info.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/lookup/package-info.java b/src/main/java/org/apache/commons/text/lookup/package-info.java new file mode 100644 index 0000000..b53399a --- /dev/null +++ b/src/main/java/org/apache/commons/text/lookup/package-info.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * <p> + * Provides algorithms for looking up strings for use with a {@link org.apache.commons.text.StrSubstitutor}. The main + * class here is {@link org.apache.commons.text.lookup.StringLookupFactory} + * </p> + * + * <p> + * The initial implementation was adapted from Apache Commons Log4j 2.10.1-SNAPSHOT. + * </p> + * + * @since 1.3 + */ +package org.apache.commons.text.lookup;