You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2008/03/16 00:23:50 UTC
svn commit: r637489 - in /commons/proper/collections/trunk/src:
java/org/apache/commons/collections/ExtendedProperties.java
test/org/apache/commons/collections/TestExtendedProperties.java
Author: bayard
Date: Sat Mar 15 16:23:48 2008
New Revision: 637489
URL: http://svn.apache.org/viewvc?rev=637489&view=rev
Log:
Applying the latest patch from Henning's report in COLLECTIONS-278 that put() and putAll() don't update the getKeys() map on ExtendedProperties
Modified:
commons/proper/collections/trunk/src/java/org/apache/commons/collections/ExtendedProperties.java
commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestExtendedProperties.java
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/ExtendedProperties.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/ExtendedProperties.java?rev=637489&r1=637488&r2=637489&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/ExtendedProperties.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/ExtendedProperties.java Sat Mar 15 16:23:48 2008
@@ -31,6 +31,7 @@
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
+import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.StringTokenizer;
@@ -639,7 +640,7 @@
*/
public Object getProperty(String key) {
// first, try to get from the 'user value' store
- Object obj = this.get(key);
+ Object obj = super.get(key);
if (obj == null) {
// if there isn't a value there, get it from the
@@ -705,7 +706,7 @@
if (!containsKey(key)) {
keysAsListed.add(key);
}
- put(key, value);
+ super.put(key, value);
}
/**
@@ -727,7 +728,7 @@
List values = new Vector(2);
values.add(current);
values.add(value);
- put(key, values);
+ super.put(key, values);
} else if (current instanceof List) {
// already a list - just add the new token
@@ -738,7 +739,7 @@
if (!containsKey(key)) {
keysAsListed.add(key);
}
- put(key, value);
+ super.put(key, value);
}
}
@@ -831,7 +832,7 @@
break;
}
}
- remove(key);
+ super.remove(key);
}
}
@@ -1090,7 +1091,7 @@
} else if (value instanceof String) {
Vector values = new Vector(1);
values.add(value);
- put(key, values);
+ super.put(key, values);
return values;
} else if (value == null) {
@@ -1142,7 +1143,7 @@
} else if (value instanceof String) {
List values = new ArrayList(1);
values.add(value);
- put(key, values);
+ super.put(key, values);
return values;
} else if (value == null) {
@@ -1208,7 +1209,7 @@
} else if (value instanceof String) {
String s = testBoolean((String) value);
Boolean b = new Boolean(s);
- put(key, b);
+ super.put(key, b);
return b;
} else if (value == null) {
@@ -1302,7 +1303,7 @@
} else if (value instanceof String) {
Byte b = new Byte((String) value);
- put(key, b);
+ super.put(key, b);
return b;
} else if (value == null) {
@@ -1372,7 +1373,7 @@
} else if (value instanceof String) {
Short s = new Short((String) value);
- put(key, s);
+ super.put(key, s);
return s;
} else if (value == null) {
@@ -1470,7 +1471,7 @@
} else if (value instanceof String) {
Integer i = new Integer((String) value);
- put(key, i);
+ super.put(key, i);
return i;
} else if (value == null) {
@@ -1540,7 +1541,7 @@
} else if (value instanceof String) {
Long l = new Long((String) value);
- put(key, l);
+ super.put(key, l);
return l;
} else if (value == null) {
@@ -1610,7 +1611,7 @@
} else if (value instanceof String) {
Float f = new Float((String) value);
- put(key, f);
+ super.put(key, f);
return f;
} else if (value == null) {
@@ -1680,7 +1681,7 @@
} else if (value instanceof String) {
Double d = new Double((String) value);
- put(key, d);
+ super.put(key, d);
return d;
} else if (value == null) {
@@ -1712,6 +1713,57 @@
}
return c;
+ }
+
+ /**
+ * Add a new property specified by the key to the
+ * ExtendedProperties.
+ *
+ * @param key specifying the property
+ * @param value for the property
+ * @return old value of the property
+ */
+ public Object put(Object key, Object value) {
+ String strKey = String.valueOf(key);
+ Object ret = getProperty(strKey);
+ addProperty(strKey, value);
+ return ret;
+ }
+
+ /**
+ * Add a map full of key/value pairs to the ExtendedProperties.
+ * If the added map is an ExtendedProperties class, then the
+ * order of the added properties is maintained.
+ *
+ * @param map full of key/value pair data
+ */
+ public void putAll(Map map) {
+ if (map instanceof ExtendedProperties) {
+ for (Iterator it = ((ExtendedProperties) map).getKeys(); it.hasNext(); ) {
+ Object key = it.next();
+ put(key, map.get(key));
+ }
+ } else {
+ for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) {
+ Map.Entry entry = (Map.Entry) it.next();
+ put(entry.getKey(), entry.getValue());
+ }
+ }
+ }
+
+
+ /**
+ * Remove the property specified by the key from the
+ * ExtendedProperties.
+ *
+ * @param key specifying the property
+ * @return old value of the property
+ */
+ public Object remove(Object key) {
+ String strKey = String.valueOf(key);
+ Object ret = getProperty(strKey);
+ clearProperty(strKey);
+ return ret;
}
}
Modified: commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestExtendedProperties.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestExtendedProperties.java?rev=637489&r1=637488&r2=637489&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestExtendedProperties.java (original)
+++ commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestExtendedProperties.java Sat Mar 15 16:23:48 2008
@@ -19,6 +19,7 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.util.Iterator;
import java.util.Properties;
import junit.framework.Test;
@@ -336,6 +337,63 @@
a.setInclude(null);
assertEquals(null, a.getInclude());
assertEquals("include", b.getInclude());
+ }
+
+ public void testKeySet1() {
+ ExtendedProperties p = new ExtendedProperties();
+ p.addProperty("a", "foo");
+ p.addProperty("b", "bar");
+ p.addProperty("c", "bar");
+
+ Iterator it = p.getKeys();
+ assertEquals("a", (String) it.next());
+ assertEquals("b", (String) it.next());
+ assertEquals("c", (String) it.next());
+ assertFalse(it.hasNext());
+ }
+
+ public void testKeySet2() {
+ ExtendedProperties p = new ExtendedProperties();
+ p.put("a", "foo");
+ p.put("b", "bar");
+ p.put("c", "bar");
+
+ Iterator it = p.getKeys();
+ assertEquals("a", (String) it.next());
+ assertEquals("b", (String) it.next());
+ assertEquals("c", (String) it.next());
+ assertFalse(it.hasNext());
+ }
+
+
+ public void testKeySet3() {
+ ExtendedProperties q = new ExtendedProperties();
+ q.addProperty("a", "foo");
+ q.addProperty("b", "bar");
+ q.addProperty("c", "bar");
+
+ ExtendedProperties p = new ExtendedProperties();
+ p.putAll(q);
+
+ Iterator it = p.getKeys();
+ assertEquals("a", (String) it.next());
+ assertEquals("b", (String) it.next());
+ assertEquals("c", (String) it.next());
+ assertFalse(it.hasNext());
+ }
+
+ public void testKeySet4() {
+ ExtendedProperties q = new ExtendedProperties();
+ q.addProperty("a", "foo");
+ q.addProperty("b", "bar");
+ q.addProperty("c", "bar");
+
+ q.remove("b");
+
+ Iterator it = q.getKeys();
+ assertEquals("a", (String) it.next());
+ assertEquals("c", (String) it.next());
+ assertFalse(it.hasNext());
}
}