[FELIX-2866] Add java.util.Properties backward compatibility methods
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1076377 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/utils/src/main/java/org/apache/felix/utils/properties/Properties.java b/utils/src/main/java/org/apache/felix/utils/properties/Properties.java
index 4edce27..6c6743c 100644
--- a/utils/src/main/java/org/apache/felix/utils/properties/Properties.java
+++ b/utils/src/main/java/org/apache/felix/utils/properties/Properties.java
@@ -29,13 +29,7 @@
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 @@
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);
diff --git a/utils/src/test/java/org/apache/felix/utils/properties/PropertiesTest.java b/utils/src/test/java/org/apache/felix/utils/properties/PropertiesTest.java
index 7809943..3f06738 100644
--- a/utils/src/test/java/org/apache/felix/utils/properties/PropertiesTest.java
+++ b/utils/src/test/java/org/apache/felix/utils/properties/PropertiesTest.java
@@ -88,4 +88,21 @@
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("====");
+ }
}