You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2013/10/27 11:00:33 UTC
svn commit: r1536079 - in /commons/proper/beanutils/branches/java5/src:
main/java/org/apache/commons/beanutils/LazyDynaList.java
main/java/org/apache/commons/beanutils/LazyDynaMap.java
test/java/org/apache/commons/beanutils/LazyDynaMapTestCase.java
Author: oheger
Date: Sun Oct 27 10:00:32 2013
New Revision: 1536079
URL: http://svn.apache.org/r1536079
Log:
Generified LazyDynaMap.
Modified:
commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/LazyDynaList.java
commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/LazyDynaMap.java
commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/LazyDynaMapTestCase.java
Modified: commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/LazyDynaList.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/LazyDynaList.java?rev=1536079&r1=1536078&r2=1536079&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/LazyDynaList.java (original)
+++ commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/LazyDynaList.java Sun Oct 27 10:00:32 2013
@@ -531,7 +531,7 @@ public class LazyDynaList extends ArrayL
// Create a DynaBean
DynaBean dynaBean = null;
if (Map.class.isAssignableFrom(elementType)) {
- dynaBean = new LazyDynaMap((Map<?, ?>)object);
+ dynaBean = createDynaBeanForMapProperty(object);
this.elementDynaClass = dynaBean.getDynaClass();
} else if (DynaBean.class.isAssignableFrom(elementType)) {
dynaBean = (DynaBean)object;
@@ -663,7 +663,7 @@ public class LazyDynaList extends ArrayL
// Transform Object to a DynaBean
newElementType = element.getClass();
if (Map.class.isAssignableFrom(element.getClass())) {
- dynaBean = new LazyDynaMap((Map<?, ?>)element);
+ dynaBean = createDynaBeanForMapProperty(element);
} else if (DynaBean.class.isAssignableFrom(element.getClass())) {
dynaBean = (DynaBean)element;
} else {
@@ -694,6 +694,19 @@ public class LazyDynaList extends ArrayL
}
/**
+ * Creates a new {@code LazyDynaMap} object for the given property value.
+ *
+ * @param value the property value
+ * @return the newly created {@code LazyDynaMap}
+ */
+ private LazyDynaMap createDynaBeanForMapProperty(Object value) {
+ @SuppressWarnings("unchecked")
+ // map properties are always stored as Map<String, Object>
+ Map<String, Object> valueMap = (Map<String, Object>) value;
+ return new LazyDynaMap(valueMap);
+ }
+
+ /**
* Return the DynaClass.
*/
private DynaClass getDynaClass() {
Modified: commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/LazyDynaMap.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/LazyDynaMap.java?rev=1536079&r1=1536078&r2=1536079&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/LazyDynaMap.java (original)
+++ commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/LazyDynaMap.java Sun Oct 27 10:00:32 2013
@@ -17,7 +17,6 @@
package org.apache.commons.beanutils;
import java.util.Map;
-import java.util.Iterator;
/**
* <p>Provides a <i>light weight</i> <code>DynaBean</code> facade to a <code>Map</code>
@@ -74,7 +73,7 @@ public class LazyDynaMap extends LazyDyn
* Default Constructor.
*/
public LazyDynaMap() {
- this(null, (Map)null);
+ this(null, (Map<String, Object>)null);
}
/**
@@ -83,7 +82,7 @@ public class LazyDynaMap extends LazyDyn
* @param name Name of this DynaBean class
*/
public LazyDynaMap(String name) {
- this(name, (Map)null);
+ this(name, (Map<String, Object>)null);
}
/**
@@ -91,7 +90,7 @@ public class LazyDynaMap extends LazyDyn
*
* @param values The Map backing this <code>LazyDynaMap</code>
*/
- public LazyDynaMap(Map values) {
+ public LazyDynaMap(Map<String, Object> values) {
this(null, values);
}
@@ -101,7 +100,7 @@ public class LazyDynaMap extends LazyDyn
* @param name Name of this DynaBean class
* @param values The Map backing this <code>LazyDynaMap</code>
*/
- public LazyDynaMap(String name, Map values) {
+ public LazyDynaMap(String name, Map<String, Object> values) {
this.name = name == null ? "LazyDynaMap" : name;
this.values = values == null ? newMap() : values;
this.dynaClass = this;
@@ -123,7 +122,7 @@ public class LazyDynaMap extends LazyDyn
* @param properties Property descriptors for the supported properties
*/
public LazyDynaMap(String name, DynaProperty[] properties) {
- this(name, (Map)null);
+ this(name, (Map<String, Object>)null);
if (properties != null) {
for (int i = 0; i < properties.length; i++) {
add(properties[i]);
@@ -147,7 +146,7 @@ public class LazyDynaMap extends LazyDyn
*
* @param values The new Map of values
*/
- public void setMap(Map values) {
+ public void setMap(Map<String, Object> values) {
this.values = values;
}
@@ -157,7 +156,7 @@ public class LazyDynaMap extends LazyDyn
* @since 1.8.0
*/
@Override
- public Map getMap() {
+ public Map<String, Object> getMap() {
return values;
}
@@ -252,12 +251,11 @@ public class LazyDynaMap extends LazyDyn
int i = 0;
DynaProperty[] properties = new DynaProperty[values.size()];
- Iterator iterator = values.keySet().iterator();
-
- while (iterator.hasNext()) {
- String name = (String)iterator.next();
+ for (Map.Entry<String, Object> e : values.entrySet()) {
+ String name = e.getKey();
Object value = values.get(name);
- properties[i++] = new DynaProperty(name, value == null ? null : value.getClass());
+ properties[i++] = new DynaProperty(name, value == null ? null
+ : value.getClass());
}
return properties;
@@ -272,9 +270,12 @@ public class LazyDynaMap extends LazyDyn
public DynaBean newInstance() {
// Create a new instance of the Map
- Map newMap = null;
+ Map<String, Object> newMap = null;
try {
- newMap = getMap().getClass().newInstance();
+ @SuppressWarnings("unchecked")
+ // The new map is used as properties map
+ Map<String, Object> temp = getMap().getClass().newInstance();
+ newMap = temp;
} catch(Exception ex) {
newMap = newMap();
}
@@ -340,7 +341,7 @@ public class LazyDynaMap extends LazyDyn
* @exception IllegalStateException if this DynaClass is currently
* restricted, so no new properties can be added
*/
- public void add(String name, Class type) {
+ public void add(String name, Class<?> type) {
if (name == null) {
throw new IllegalArgumentException("Property name is missing.");
@@ -380,7 +381,7 @@ public class LazyDynaMap extends LazyDyn
*
* @exception UnsupportedOperationException anytime this method is called
*/
- public void add(String name, Class type, boolean readable, boolean writeable) {
+ public void add(String name, Class<?> type, boolean readable, boolean writeable) {
throw new java.lang.UnsupportedOperationException("readable/writable properties not supported");
}
Modified: commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/LazyDynaMapTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/LazyDynaMapTestCase.java?rev=1536079&r1=1536078&r2=1536079&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/LazyDynaMapTestCase.java (original)
+++ commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/LazyDynaMapTestCase.java Sun Oct 27 10:00:32 2013
@@ -16,15 +16,16 @@
*/
package org.apache.commons.beanutils;
-import java.util.Map;
-import java.util.HashMap;
-import java.util.TreeMap;
-import java.util.List;
+import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.LinkedList;
-import java.lang.reflect.InvocationTargetException;
-import junit.framework.TestCase;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
import junit.framework.Test;
+import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
@@ -160,12 +161,12 @@ public class LazyDynaMapTestCase extends
dynaMap.set(testProperty, testKey, testInteger1);
assertEquals("Check Mapped Property exists", HashMap.class, dynaMap.get(testProperty).getClass());
assertEquals("Check First Mapped Value is correct(a)", testInteger1, dynaMap.get(testProperty, testKey));
- assertEquals("Check First Mapped Value is correct(b)", testInteger1, ((HashMap)dynaMap.get(testProperty)).get(testKey));
+ assertEquals("Check First Mapped Value is correct(b)", testInteger1, ((HashMap<?, ?>)dynaMap.get(testProperty)).get(testKey));
// Set the property again - should set the new value
dynaMap.set(testProperty, testKey, testInteger2);
assertEquals("Check Second Mapped Value is correct(a)", testInteger2, dynaMap.get(testProperty, testKey));
- assertEquals("Check Second Mapped Value is correct(b)", testInteger2, ((HashMap)dynaMap.get(testProperty)).get(testKey));
+ assertEquals("Check Second Mapped Value is correct(b)", testInteger2, ((HashMap<?, ?>)dynaMap.get(testProperty)).get(testKey));
}
/**
@@ -187,12 +188,12 @@ public class LazyDynaMapTestCase extends
dynaMap.set(testProperty, testKey, testInteger1);
assertEquals("Check Mapped Property exists", TreeMap.class, dynaMap.get(testProperty).getClass());
assertEquals("Check First Mapped Value is correct(a)", testInteger1, dynaMap.get(testProperty, testKey));
- assertEquals("Check First Mapped Value is correct(b)", testInteger1, ((TreeMap)dynaMap.get(testProperty)).get(testKey));
+ assertEquals("Check First Mapped Value is correct(b)", testInteger1, ((TreeMap<?, ?>)dynaMap.get(testProperty)).get(testKey));
// Set the property again - should set the new value
dynaMap.set(testProperty, testKey, testInteger2);
assertEquals("Check Second Mapped Value is correct(a)", testInteger2, dynaMap.get(testProperty, testKey));
- assertEquals("Check Second Mapped Value is correct(b)", testInteger2, ((TreeMap)dynaMap.get(testProperty)).get(testKey));
+ assertEquals("Check Second Mapped Value is correct(b)", testInteger2, ((TreeMap<?, ?>)dynaMap.get(testProperty)).get(testKey));
}
/**
@@ -280,13 +281,13 @@ public class LazyDynaMapTestCase extends
assertNotNull("Check Indexed Property is not null", dynaMap.get(testProperty));
assertEquals("Check Indexed Property is correct type", ArrayList.class, dynaMap.get(testProperty).getClass());
assertEquals("Check First Indexed Value is correct", testInteger1, dynaMap.get(testProperty, index));
- assertEquals("Check First Array length is correct", new Integer(index+1), new Integer(((ArrayList)dynaMap.get(testProperty)).size()));
+ assertEquals("Check First Array length is correct", new Integer(index+1), new Integer(((ArrayList<?>)dynaMap.get(testProperty)).size()));
// Set a second indexed value, should automatically grow the ArrayList and set appropriate indexed value
index = index + 2;
dynaMap.set(testProperty, index, testString1);
assertEquals("Check Second Indexed Value is correct", testString1, dynaMap.get(testProperty, index));
- assertEquals("Check Second Array length is correct", new Integer(index+1), new Integer(((ArrayList)dynaMap.get(testProperty)).size()));
+ assertEquals("Check Second Array length is correct", new Integer(index+1), new Integer(((ArrayList<?>)dynaMap.get(testProperty)).size()));
}
/**
@@ -310,13 +311,13 @@ public class LazyDynaMapTestCase extends
dynaMap.set(testProperty, index, testString1);
assertEquals("Check Property type is correct", LinkedList.class, dynaMap.get(testProperty).getClass());
assertEquals("Check First Indexed Value is correct", testString1, dynaMap.get(testProperty, index));
- assertEquals("Check First Array length is correct", new Integer(index+1), new Integer(((LinkedList)dynaMap.get(testProperty)).size()));
+ assertEquals("Check First Array length is correct", new Integer(index+1), new Integer(((LinkedList<?>)dynaMap.get(testProperty)).size()));
// Set a second indexed value, should automatically grow the LinkedList and set appropriate indexed value
index = index + 2;
dynaMap.set(testProperty, index, testInteger1);
assertEquals("Check Second Indexed Value is correct", testInteger1, dynaMap.get(testProperty, index));
- assertEquals("Check Second Array length is correct", new Integer(index+1), new Integer(((LinkedList)dynaMap.get(testProperty)).size()));
+ assertEquals("Check Second Array length is correct", new Integer(index+1), new Integer(((LinkedList<?>)dynaMap.get(testProperty)).size()));
}
/**
@@ -495,16 +496,16 @@ public class LazyDynaMapTestCase extends
// Create LazyDynaMap using TreeMap
// containing some properties
- LazyDynaMap orig = new LazyDynaMap(new TreeMap());
+ LazyDynaMap orig = new LazyDynaMap(new TreeMap<String, Object>());
orig.set("indexProp", 0, "indexVal0");
orig.set("indexProp", 1, "indexVal1");
- assertEquals("Index prop size", 2, ((List)orig.get("indexProp")).size());
+ assertEquals("Index prop size", 2, ((List<?>)orig.get("indexProp")).size());
LazyDynaMap newOne = (LazyDynaMap)orig.newInstance();
- Map newMap = newOne.getMap();
+ Map<String, Object> newMap = newOne.getMap();
assertEquals("Check Map type", TreeMap.class, newMap.getClass());
- ArrayList indexProp = (ArrayList)newMap.get("indexProp");
+ ArrayList<?> indexProp = (ArrayList<?>)newMap.get("indexProp");
assertNotNull("Indexed Prop missing", indexProp);
assertEquals("Index prop size", 0, indexProp.size());
}