felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r1076377 - in /felix/trunk/utils/src: main/java/org/apache/felix/utils/properties/Properties.java test/java/org/apache/felix/utils/properties/PropertiesTest.java
Date Wed, 02 Mar 2011 21:13:05 GMT
Author: gnodet
Date: Wed Mar  2 21:13:05 2011
New Revision: 1076377

URL: http://svn.apache.org/viewvc?rev=1076377&view=rev
Log:
[FELIX-2866] Add java.util.Properties backward compatibility methods

Modified:
    felix/trunk/utils/src/main/java/org/apache/felix/utils/properties/Properties.java
    felix/trunk/utils/src/test/java/org/apache/felix/utils/properties/PropertiesTest.java

Modified: felix/trunk/utils/src/main/java/org/apache/felix/utils/properties/Properties.java
URL: http://svn.apache.org/viewvc/felix/trunk/utils/src/main/java/org/apache/felix/utils/properties/Properties.java?rev=1076377&r1=1076376&r2=1076377&view=diff
==============================================================================
--- felix/trunk/utils/src/main/java/org/apache/felix/utils/properties/Properties.java (original)
+++ felix/trunk/utils/src/main/java/org/apache/felix/utils/properties/Properties.java Wed
Mar  2 21:13:05 2011
@@ -29,13 +29,7 @@ import java.io.OutputStreamWriter;
 import java.io.Reader;
 import java.io.Writer;
 import java.net.URL;
-import java.util.AbstractMap;
-import java.util.ArrayList;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 
 /**
  * <p>
@@ -133,11 +127,70 @@ public class Properties extends Abstract
         saveLayout(writer);
     }
 
+    /**
+     * Store a properties into a output stream, preserving comments, special character, etc.
+     * This method is mainly to be compatible with the java.util.Properties class.
+     *
+     * @param os an output stream.
+     * @param comment this parameter is ignored as this Properties
+     * @throws IOException
+     */
+    public void store(OutputStream os, String comment) throws IOException {
+        this.save(os);
+    }
+
+    /**
+     * Searches for the property with the specified key in this property list.
+     *
+     * @param key the property key.
+     * @return the value in this property list with the specified key value.
+     */
+    public String getProperty(String key) {
+        return this.get(key);
+    }
+
+    /**
+     * Searches for the property with the specified key in this property list. If the key
is not found in this property
+     * list, the default property list, and its defaults, recursively, are then checked.
The method returns the default
+     * value argument if the property is not found.
+     *
+     * @param key the property key.
+     * @param defaultValue a default value.
+     * @return
+     */
+    public String getProperty(String key, String defaultValue) {
+        if (this.get(key) != null)
+            return this.get(key);
+        return defaultValue;
+    }
+
     @Override
     public Set<Entry<String, String>> entrySet() {
         return storage.entrySet();
     }
 
+    /**
+     * Returns an enumeration of all the keys in this property list, including distinct keys
in the default property
+     * list if a key of the same name has not already been found from the main properties
list.
+     *
+     * @return an enumeration of all the keys in this property list, including the keys in
the default property list.
+     */
+    public Enumeration<?> propertyNames() {
+        return Collections.enumeration(storage.keySet());
+    }
+
+    /**
+     * Calls the map method put. Provided for parallelism with the getProperty method.
+     * Enforces use of strings for property keys and values. The value returned is the result
of the map call to put.
+     *
+     * @param key the key to be placed into this property list.
+     * @param value the value corresponding to the key.
+     * @return the previous value of the specified key in this property list, or null if
it did not have one.
+     */
+    public Object setProperty(String key, String value) {
+        return this.put(key, value);
+    }
+
     @Override
     public String put(String key, String value) {
         String old = storage.put(key, value);

Modified: felix/trunk/utils/src/test/java/org/apache/felix/utils/properties/PropertiesTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/utils/src/test/java/org/apache/felix/utils/properties/PropertiesTest.java?rev=1076377&r1=1076376&r2=1076377&view=diff
==============================================================================
--- felix/trunk/utils/src/test/java/org/apache/felix/utils/properties/PropertiesTest.java
(original)
+++ felix/trunk/utils/src/test/java/org/apache/felix/utils/properties/PropertiesTest.java
Wed Mar  2 21:13:05 2011
@@ -88,4 +88,21 @@ public class PropertiesTest extends Test
         props.save(System.err);
         System.err.println("=====");
     }
+
+    public void testJavaUtilPropertiesCompatibility() throws Exception {
+        Properties properties = new Properties();
+        properties.load(this.getClass().getClassLoader().getResourceAsStream(TEST_PROPERTIES_FILE));
+
+        String test = properties.getProperty("test");
+        assertEquals(test, "test");
+
+        String defaultValue = properties.getProperty("notfound", "default");
+        assertEquals(defaultValue, "default");
+
+        properties.setProperty("another", "another");
+        assertEquals(properties.getProperty("another"), "another");
+
+        properties.store(System.err, null);
+        System.err.println("====");
+    }
 }



Mime
View raw message