From 5ad5f22e7c9991be7a20a33a51948e3ffd2a38c5 Mon Sep 17 00:00:00 2001 From: andrzej Date: Wed, 11 Oct 2006 18:41:08 +0000 Subject: [PATCH] Added a method to create a Properties object that accesses subproperties by filtering a referenced base Properties, rather than creating an entirely new set of Properties, to avoid creating loads of garbage when filtering through multiple levels of subproperties. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1943 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/util/PropertiesUtil.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/java/com/samskivert/util/PropertiesUtil.java b/src/java/com/samskivert/util/PropertiesUtil.java index 672d5db9..0a3abfad 100644 --- a/src/java/com/samskivert/util/PropertiesUtil.java +++ b/src/java/com/samskivert/util/PropertiesUtil.java @@ -92,6 +92,38 @@ public class PropertiesUtil return dest; } + /** + * Returns a filtered version of the specified properties that first + * looks for a property starting with the given prefix, then looks for + * a property without the prefix. For example, passing the prefix "alt" + * and using the following properties: + * + *
+     * alt.texture = sand.png
+     * lighting = off
+     * 
+ * + * ...would return "sand.png" for the property "texture" and "off" for + * the property "lighting". Unlike {@link #getSubProperties}, the object + * returned by this method references, rather than copies, the underlying + * properties. Only the {@link Properties#getProperty} methods are + * guaranteed to work correctly on the returned object. + */ + public static Properties getFilteredProperties ( + final Properties source, String prefix) + { + final String dprefix = prefix + "."; + return new Properties() { + public String getProperty (String key) { + return getProperty(key, null); + } + public String getProperty (String key, String defaultValue) { + return source.getProperty(dprefix + key, + source.getProperty(key, defaultValue)); + } + }; + } + /** * A helper function used by the {@link #getSubProperties} methods. */