Widening.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@2971 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
samskivert
2010-12-19 18:58:04 +00:00
parent cb82f080d6
commit df7df145e6
@@ -36,12 +36,11 @@ import com.samskivert.io.StreamUtil;
public class PropertiesUtil public class PropertiesUtil
{ {
/** /**
* Extracts all properties from the supplied properties object with * Extracts all properties from the supplied properties object with the specified prefix,
* the specified prefix, removes the prefix from the key for those * removes the prefix from the key for those properties and inserts them into a new properties
* properties and inserts them into a new properties object which is * object which is then returned. This is useful for extracting properties from a global
* then returned. This is useful for extracting properties from a * configuration object that must be passed to a service that expects it's own private
* global configuration object that must be passed to a service that * properties (JDBC for example).
* expects it's own private properties (JDBC for example).
* *
* The property file might look like so: * The property file might look like so:
* *
@@ -56,9 +55,8 @@ public class PropertiesUtil
* my_happy_other_param=my_happy_other_value * my_happy_other_param=my_happy_other_value
* </pre> * </pre>
* *
* This can be supplied to <code>getSubProperties()</code> with a * This can be supplied to <code>getSubProperties()</code> with a prefix of <code>"jdbc"</code>
* prefix of <code>"jdbc"</code> and the following properties would be * and the following properties would be returned:
* returned:
* *
* <pre> * <pre>
* driver=foo.bar.Driver * driver=foo.bar.Driver
@@ -67,8 +65,7 @@ public class PropertiesUtil
* password=is your uncle * password=is your uncle
* </pre> * </pre>
*/ */
public static Properties getSubProperties ( public static Properties getSubProperties (Properties source, String prefix)
Properties source, String prefix)
{ {
Properties dest = new Properties(); Properties dest = new Properties();
extractSubProperties(source, dest, prefix); extractSubProperties(source, dest, prefix);
@@ -76,9 +73,8 @@ public class PropertiesUtil
} }
/** /**
* Like {@link #getSubProperties(Properties,String)} with the * Like {@link #getSubProperties(Properties,String)} with the additional functionality of
* additional functionality of loading up defaults for the * loading up defaults for the sub-properties which are identified by the
* sub-properties which are identified by the
* <code>defaultsPrefix</code> string. * <code>defaultsPrefix</code> string.
*/ */
public static Properties getSubProperties ( public static Properties getSubProperties (
@@ -96,24 +92,21 @@ public class PropertiesUtil
} }
/** /**
* Returns a filtered version of the specified properties that first * Returns a filtered version of the specified properties that first looks for a property
* looks for a property starting with the given prefix, then looks for * starting with the given prefix, then looks for a property without the prefix. For example,
* a property without the prefix. For example, passing the prefix "alt" * passing the prefix "alt" and using the following properties:
* and using the following properties:
* *
* <pre> * <pre>
* alt.texture = sand.png * alt.texture = sand.png
* lighting = off * lighting = off
* </pre> * </pre>
* *
* ...would return "sand.png" for the property "texture" and "off" for * ...would return "sand.png" for the property "texture" and "off" for the property "lighting".
* the property "lighting". Unlike {@link #getSubProperties}, the object * Unlike {@link #getSubProperties}, the object returned by this method references, rather than
* returned by this method references, rather than copies, the underlying * copies, the underlying properties. Only the {@link Properties#getProperty} methods are
* properties. Only the {@link Properties#getProperty} methods are
* guaranteed to work correctly on the returned object. * guaranteed to work correctly on the returned object.
*/ */
public static Properties getFilteredProperties ( public static Properties getFilteredProperties (final Properties source, String prefix)
final Properties source, String prefix)
{ {
final String dprefix = prefix + "."; final String dprefix = prefix + ".";
return new Properties() { return new Properties() {
@@ -121,8 +114,7 @@ public class PropertiesUtil
return getProperty(key, null); return getProperty(key, null);
} }
@Override public String getProperty (String key, String defaultValue) { @Override public String getProperty (String key, String defaultValue) {
return source.getProperty(dprefix + key, return source.getProperty(dprefix + key, source.getProperty(key, defaultValue));
source.getProperty(key, defaultValue));
} }
@Override public Enumeration<?> propertyNames () { @Override public Enumeration<?> propertyNames () {
return new Enumeration<Object>() { return new Enumeration<Object>() {
@@ -157,8 +149,7 @@ public class PropertiesUtil
/** /**
* A helper function used by the {@link #getSubProperties} methods. * A helper function used by the {@link #getSubProperties} methods.
*/ */
protected static void extractSubProperties ( protected static void extractSubProperties (Properties source, Properties dest, String prefix)
Properties source, Properties dest, String prefix)
{ {
// extend the prefix to contain a dot // extend the prefix to contain a dot
prefix = prefix + "."; prefix = prefix + ".";