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 2018/09/27 22:32:53 UTC
svn commit: r1842197 -
/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/interpol/ConfigurationInterpolator.java
Author: ggregory
Date: Thu Sep 27 22:32:53 2018
New Revision: 1842197
URL: http://svn.apache.org/viewvc?rev=1842197&view=rev
Log:
Sort methods. A private static method does not need to be static.
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/interpol/ConfigurationInterpolator.java
Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/interpol/ConfigurationInterpolator.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/interpol/ConfigurationInterpolator.java?rev=1842197&r1=1842196&r2=1842197&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/interpol/ConfigurationInterpolator.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/interpol/ConfigurationInterpolator.java Thu Sep 27 22:32:53 2018
@@ -106,26 +106,44 @@ public class ConfigurationInterpolator
/** A map containing the default prefix lookups. */
private static final Map<String, Lookup> DEFAULT_PREFIX_LOOKUPS;
- /** A map with the currently registered lookup objects. */
- private final Map<String, Lookup> prefixLookups;
-
- /** Stores the default lookup objects. */
- private final List<Lookup> defaultLookups;
-
- /** The helper object performing variable substitution. */
- private final StringSubstitutor substitutor;
+ static
+ {
+ final Map<String, Lookup> lookups = new HashMap<>();
+ for (final DefaultLookups l : DefaultLookups.values())
+ {
+ lookups.put(l.getPrefix(), l.getLookup());
+ }
+ DEFAULT_PREFIX_LOOKUPS = Collections.unmodifiableMap(lookups);
+ }
- /** Stores a parent interpolator objects if the interpolator is nested hierarchically. */
- private volatile ConfigurationInterpolator parentInterpolator;
+ /**
+ * Creates a new instance based on the properties in the given specification
+ * object.
+ *
+ * @param spec the {@code InterpolatorSpecification}
+ * @return the newly created instance
+ */
+ private static ConfigurationInterpolator createInterpolator(
+ final InterpolatorSpecification spec)
+ {
+ final ConfigurationInterpolator ci = new ConfigurationInterpolator();
+ ci.addDefaultLookups(spec.getDefaultLookups());
+ ci.registerLookups(spec.getPrefixLookups());
+ ci.setParentInterpolator(spec.getParentInterpolator());
+ return ci;
+ }
/**
- * Creates a new instance of {@code ConfigurationInterpolator}.
+ * Extracts the variable name from a value that consists of a single
+ * variable.
+ *
+ * @param strValue the value
+ * @return the extracted variable name
*/
- public ConfigurationInterpolator()
+ private static String extractVariableName(final String strValue)
{
- prefixLookups = new ConcurrentHashMap<>();
- defaultLookups = new CopyOnWriteArrayList<>();
- substitutor = initSubstitutor();
+ return strValue.substring(VAR_START_LENGTH,
+ strValue.length() - VAR_END_LENGTH);
}
/**
@@ -189,59 +207,58 @@ public class ConfigurationInterpolator
return lookup;
}
+ /** A map with the currently registered lookup objects. */
+ private final Map<String, Lookup> prefixLookups;
+
+ /** Stores the default lookup objects. */
+ private final List<Lookup> defaultLookups;
+
+ /** The helper object performing variable substitution. */
+ private final StringSubstitutor substitutor;
+
+ /** Stores a parent interpolator objects if the interpolator is nested hierarchically. */
+ private volatile ConfigurationInterpolator parentInterpolator;
+
/**
- * Returns a map with the currently registered {@code Lookup} objects and
- * their prefixes. This is a snapshot copy of the internally used map. So
- * modifications of this map do not effect this instance.
- *
- * @return a copy of the map with the currently registered {@code Lookup}
- * objects
+ * Creates a new instance of {@code ConfigurationInterpolator}.
*/
- public Map<String, Lookup> getLookups()
+ public ConfigurationInterpolator()
{
- return new HashMap<>(prefixLookups);
+ prefixLookups = new ConcurrentHashMap<>();
+ defaultLookups = new CopyOnWriteArrayList<>();
+ substitutor = initSubstitutor();
}
/**
- * Registers the given {@code Lookup} object for the specified prefix at
- * this instance. From now on this lookup object will be used for variables
- * that have the specified prefix.
+ * Adds a default {@code Lookup} object. Default {@code Lookup} objects are
+ * queried (in the order they were added) for all variables without a
+ * special prefix. If no default {@code Lookup} objects are present, such
+ * variables won't be processed.
*
- * @param prefix the variable prefix (must not be <b>null</b>)
- * @param lookup the {@code Lookup} object to be used for this prefix (must
+ * @param defaultLookup the default {@code Lookup} object to be added (must
* not be <b>null</b>)
- * @throws IllegalArgumentException if either the prefix or the
- * {@code Lookup} object is <b>null</b>
+ * @throws IllegalArgumentException if the {@code Lookup} object is
+ * <b>null</b>
*/
- public void registerLookup(final String prefix, final Lookup lookup)
+ public void addDefaultLookup(final Lookup defaultLookup)
{
- if (prefix == null)
- {
- throw new IllegalArgumentException(
- "Prefix for lookup object must not be null!");
- }
- if (lookup == null)
- {
- throw new IllegalArgumentException(
- "Lookup object must not be null!");
- }
- prefixLookups.put(prefix, lookup);
+ defaultLookups.add(defaultLookup);
}
/**
- * Registers all {@code Lookup} objects in the given map with their prefixes
- * at this {@code ConfigurationInterpolator}. Using this method multiple
- * {@code Lookup} objects can be registered at once. If the passed in map is
- * <b>null</b>, this method does not have any effect.
+ * Adds all {@code Lookup} objects in the given collection as default
+ * lookups. The collection can be <b>null</b>, then this method has no
+ * effect. It must not contain <b>null</b> entries.
*
- * @param lookups the map with lookups to register (may be <b>null</b>)
- * @throws IllegalArgumentException if the map contains <b>entries</b>
+ * @param lookups the {@code Lookup} objects to be added as default lookups
+ * @throws IllegalArgumentException if the collection contains a <b>null</b>
+ * entry
*/
- public void registerLookups(final Map<String, ? extends Lookup> lookups)
+ public void addDefaultLookups(final Collection<? extends Lookup> lookups)
{
if (lookups != null)
{
- prefixLookups.putAll(lookups);
+ defaultLookups.addAll(lookups);
}
}
@@ -259,15 +276,17 @@ public class ConfigurationInterpolator
}
/**
- * Returns an unmodifiable set with the prefixes, for which {@code Lookup}
- * objects are registered at this instance. This means that variables with
- * these prefixes can be processed.
+ * Obtains the lookup object for the specified prefix. This method is called
+ * by the {@code lookup()} method. This implementation will check
+ * whether a lookup object is registered for the given prefix. If not, a
+ * <b>null</b> lookup object will be returned (never <b>null</b>).
*
- * @return a set with the registered variable prefixes
+ * @param prefix the prefix
+ * @return the lookup object to be used for this prefix
*/
- public Set<String> prefixSet()
+ protected Lookup fetchLookupForPrefix(final String prefix)
{
- return Collections.unmodifiableSet(prefixLookups.keySet());
+ return nullSafeLookup(prefixLookups.get(prefix));
}
/**
@@ -285,63 +304,16 @@ public class ConfigurationInterpolator
}
/**
- * Adds a default {@code Lookup} object. Default {@code Lookup} objects are
- * queried (in the order they were added) for all variables without a
- * special prefix. If no default {@code Lookup} objects are present, such
- * variables won't be processed.
- *
- * @param defaultLookup the default {@code Lookup} object to be added (must
- * not be <b>null</b>)
- * @throws IllegalArgumentException if the {@code Lookup} object is
- * <b>null</b>
- */
- public void addDefaultLookup(final Lookup defaultLookup)
- {
- defaultLookups.add(defaultLookup);
- }
-
- /**
- * Adds all {@code Lookup} objects in the given collection as default
- * lookups. The collection can be <b>null</b>, then this method has no
- * effect. It must not contain <b>null</b> entries.
- *
- * @param lookups the {@code Lookup} objects to be added as default lookups
- * @throws IllegalArgumentException if the collection contains a <b>null</b>
- * entry
- */
- public void addDefaultLookups(final Collection<? extends Lookup> lookups)
- {
- if (lookups != null)
- {
- defaultLookups.addAll(lookups);
- }
- }
-
- /**
- * Removes the specified {@code Lookup} object from the list of default
- * {@code Lookup}s.
- *
- * @param lookup the {@code Lookup} object to be removed
- * @return a flag whether this {@code Lookup} object actually existed and
- * was removed
- */
- public boolean removeDefaultLookup(final Lookup lookup)
- {
- return defaultLookups.remove(lookup);
- }
-
- /**
- * Sets the parent {@code ConfigurationInterpolator}. This object is used if
- * the {@code Lookup} objects registered at this object cannot resolve a
- * variable.
+ * Returns a map with the currently registered {@code Lookup} objects and
+ * their prefixes. This is a snapshot copy of the internally used map. So
+ * modifications of this map do not effect this instance.
*
- * @param parentInterpolator the parent {@code ConfigurationInterpolator}
- * object (can be <b>null</b>)
+ * @return a copy of the map with the currently registered {@code Lookup}
+ * objects
*/
- public void setParentInterpolator(
- final ConfigurationInterpolator parentInterpolator)
+ public Map<String, Lookup> getLookups()
{
- this.parentInterpolator = parentInterpolator;
+ return new HashMap<>(prefixLookups);
}
/**
@@ -355,26 +327,24 @@ public class ConfigurationInterpolator
}
/**
- * Sets a flag that variable names can contain other variables. If enabled,
- * variable substitution is also done in variable names.
- *
- * @return the substitution in variables flag
- */
- public boolean isEnableSubstitutionInVariables()
- {
- return substitutor.isEnableSubstitutionInVariables();
- }
-
- /**
- * Sets the flag whether variable names can contain other variables. This
- * flag corresponds to the {@code enableSubstitutionInVariables} property of
- * the underlying {@code StringSubstitutor} object.
+ * Creates and initializes a {@code StringSubstitutor} object which is used for
+ * variable substitution. This {@code StringSubstitutor} is assigned a
+ * specialized lookup object implementing the correct variable resolving
+ * algorithm.
*
- * @param f the new value of the flag
+ * @return the {@code StringSubstitutor} used by this object
*/
- public void setEnableSubstitutionInVariables(final boolean f)
+ private StringSubstitutor initSubstitutor()
{
- substitutor.setEnableSubstitutionInVariables(f);
+ return new StringSubstitutor(new StringLookup()
+ {
+ @Override
+ public String lookup(final String key)
+ {
+ final Object result = resolve(key);
+ return result != null ? result.toString() : null;
+ }
+ });
}
/**
@@ -409,6 +379,100 @@ public class ConfigurationInterpolator
}
/**
+ * Sets a flag that variable names can contain other variables. If enabled,
+ * variable substitution is also done in variable names.
+ *
+ * @return the substitution in variables flag
+ */
+ public boolean isEnableSubstitutionInVariables()
+ {
+ return substitutor.isEnableSubstitutionInVariables();
+ }
+
+ /**
+ * Checks whether a value to be interpolated seems to be a single variable.
+ * In this case, it is resolved directly without using the
+ * {@code StringSubstitutor}. Note that it is okay if this method returns a
+ * false positive: In this case, resolving is going to fail, and standard
+ * mechanism is used.
+ *
+ * @param strValue the value to be interpolated
+ * @return a flag whether this value seems to be a single variable
+ */
+ private boolean looksLikeSingleVariable(final String strValue)
+ {
+ return strValue.startsWith(VAR_START) && strValue.endsWith(VAR_END);
+ }
+
+ /**
+ * Returns an unmodifiable set with the prefixes, for which {@code Lookup}
+ * objects are registered at this instance. This means that variables with
+ * these prefixes can be processed.
+ *
+ * @return a set with the registered variable prefixes
+ */
+ public Set<String> prefixSet()
+ {
+ return Collections.unmodifiableSet(prefixLookups.keySet());
+ }
+
+ /**
+ * Registers the given {@code Lookup} object for the specified prefix at
+ * this instance. From now on this lookup object will be used for variables
+ * that have the specified prefix.
+ *
+ * @param prefix the variable prefix (must not be <b>null</b>)
+ * @param lookup the {@code Lookup} object to be used for this prefix (must
+ * not be <b>null</b>)
+ * @throws IllegalArgumentException if either the prefix or the
+ * {@code Lookup} object is <b>null</b>
+ */
+ public void registerLookup(final String prefix, final Lookup lookup)
+ {
+ if (prefix == null)
+ {
+ throw new IllegalArgumentException(
+ "Prefix for lookup object must not be null!");
+ }
+ if (lookup == null)
+ {
+ throw new IllegalArgumentException(
+ "Lookup object must not be null!");
+ }
+ prefixLookups.put(prefix, lookup);
+ }
+
+ /**
+ * Registers all {@code Lookup} objects in the given map with their prefixes
+ * at this {@code ConfigurationInterpolator}. Using this method multiple
+ * {@code Lookup} objects can be registered at once. If the passed in map is
+ * <b>null</b>, this method does not have any effect.
+ *
+ * @param lookups the map with lookups to register (may be <b>null</b>)
+ * @throws IllegalArgumentException if the map contains <b>entries</b>
+ */
+ public void registerLookups(final Map<String, ? extends Lookup> lookups)
+ {
+ if (lookups != null)
+ {
+ prefixLookups.putAll(lookups);
+ }
+ }
+
+ /**
+ * Removes the specified {@code Lookup} object from the list of default
+ * {@code Lookup}s.
+ *
+ * @param lookup the {@code Lookup} object to be removed
+ * @return a flag whether this {@code Lookup} object actually existed and
+ * was removed
+ */
+ public boolean removeDefaultLookup(final Lookup lookup)
+ {
+ return defaultLookups.remove(lookup);
+ }
+
+ /**
* Resolves the specified variable. This implementation tries to extract
* a variable prefix from the given variable name (the first colon (':') is
* used as prefix separator). It then passes the name of the variable with
@@ -459,41 +523,6 @@ public class ConfigurationInterpolator
}
/**
- * Obtains the lookup object for the specified prefix. This method is called
- * by the {@code lookup()} method. This implementation will check
- * whether a lookup object is registered for the given prefix. If not, a
- * <b>null</b> lookup object will be returned (never <b>null</b>).
- *
- * @param prefix the prefix
- * @return the lookup object to be used for this prefix
- */
- protected Lookup fetchLookupForPrefix(final String prefix)
- {
- return nullSafeLookup(prefixLookups.get(prefix));
- }
-
- /**
- * Creates and initializes a {@code StringSubstitutor} object which is used for
- * variable substitution. This {@code StringSubstitutor} is assigned a
- * specialized lookup object implementing the correct variable resolving
- * algorithm.
- *
- * @return the {@code StringSubstitutor} used by this object
- */
- private StringSubstitutor initSubstitutor()
- {
- return new StringSubstitutor(new StringLookup()
- {
- @Override
- public String lookup(final String key)
- {
- final Object result = resolve(key);
- return result != null ? result.toString() : null;
- }
- });
- }
-
- /**
* Interpolates a string value that seems to be a single variable.
*
* @param strValue the string to be interpolated
@@ -505,57 +534,28 @@ public class ConfigurationInterpolator
}
/**
- * Checks whether a value to be interpolated seems to be a single variable.
- * In this case, it is resolved directly without using the
- * {@code StringSubstitutor}. Note that it is okay if this method returns a
- * false positive: In this case, resolving is going to fail, and standard
- * mechanism is used.
+ * Sets the flag whether variable names can contain other variables. This
+ * flag corresponds to the {@code enableSubstitutionInVariables} property of
+ * the underlying {@code StringSubstitutor} object.
*
- * @param strValue the value to be interpolated
- * @return a flag whether this value seems to be a single variable
+ * @param f the new value of the flag
*/
- private static boolean looksLikeSingleVariable(final String strValue)
+ public void setEnableSubstitutionInVariables(final boolean f)
{
- return strValue.startsWith(VAR_START) && strValue.endsWith(VAR_END);
+ substitutor.setEnableSubstitutionInVariables(f);
}
/**
- * Extracts the variable name from a value that consists of a single
+ * Sets the parent {@code ConfigurationInterpolator}. This object is used if
+ * the {@code Lookup} objects registered at this object cannot resolve a
* variable.
*
- * @param strValue the value
- * @return the extracted variable name
- */
- private static String extractVariableName(final String strValue)
- {
- return strValue.substring(VAR_START_LENGTH,
- strValue.length() - VAR_END_LENGTH);
- }
-
- /**
- * Creates a new instance based on the properties in the given specification
- * object.
- *
- * @param spec the {@code InterpolatorSpecification}
- * @return the newly created instance
+ * @param parentInterpolator the parent {@code ConfigurationInterpolator}
+ * object (can be <b>null</b>)
*/
- private static ConfigurationInterpolator createInterpolator(
- final InterpolatorSpecification spec)
- {
- final ConfigurationInterpolator ci = new ConfigurationInterpolator();
- ci.addDefaultLookups(spec.getDefaultLookups());
- ci.registerLookups(spec.getPrefixLookups());
- ci.setParentInterpolator(spec.getParentInterpolator());
- return ci;
- }
-
- static
+ public void setParentInterpolator(
+ final ConfigurationInterpolator parentInterpolator)
{
- final Map<String, Lookup> lookups = new HashMap<>();
- for (final DefaultLookups l : DefaultLookups.values())
- {
- lookups.put(l.getPrefix(), l.getLookup());
- }
- DEFAULT_PREFIX_LOOKUPS = Collections.unmodifiableMap(lookups);
+ this.parentInterpolator = parentInterpolator;
}
}