Author: adrianc
Date: Mon Jun 2 15:20:54 2008
New Revision: 662589
URL:
http://svn.apache.org/viewvc?rev=662589&view=revLog:
Small enhancement to the UtilGenerics class. Added two helper methods to make it easier to write type-safe code.
Now you can do things like
Map<String, Object> params = UtilGenerics.toMap(context.get("parameters"));
without causing compiler errors or throwing ClassCast exceptions.
Modified:
ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilGenerics.java
Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilGenerics.java
URL:
http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilGenerics.java?rev=662589&r1=662588&r2=662589&view=diff==============================================================================
--- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilGenerics.java (original)
+++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilGenerics.java Mon Jun 2 15:20:54 2008
@@ -46,6 +46,7 @@
}
}
+ @SuppressWarnings("unchecked")
public static <T> Collection<T> checkCollection(Object object) {
return (Collection<T>) checkCollectionCast(object, Collection.class);
}
@@ -55,6 +56,7 @@
return checkCollection(object);
}
+ @SuppressWarnings("unchecked")
public static <T> List<T> checkList(Object object) {
return (List<T>) checkCollectionCast(object, List.class);
}
@@ -64,6 +66,7 @@
return checkList(object);
}
+ @SuppressWarnings("unchecked")
public static <K, V> Map<K, V> checkMap(Object object) {
if (object != null && !(object instanceof Map)) throw new ClassCastException("Not a map");
return (Map<K, V>) object;
@@ -87,6 +90,7 @@
return checkMap(object);
}
+ @SuppressWarnings("unchecked")
public static <T> Set<T> checkSet(Object object) {
return (Set<T>) checkCollectionCast(object, Set.class);
}
@@ -96,6 +100,24 @@
return checkSet(object);
}
+ /** Returns the Object argument as a parameterized List if the Object argument
+ * is an instance of List. Otherwise returns null.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> List<T> toList(Object object) {
+ if (object != null && !(object instanceof List)) return null;
+ return (List<T>) object;
+ }
+
+ /** Returns the Object argument as a parameterized Map if the Object argument
+ * is an instance of Map. Otherwise returns null.
+ */
+ @SuppressWarnings("unchecked")
+ public static <K, V> Map<K, V> toMap(Object object) {
+ if (object != null && !(object instanceof Map)) return null;
+ return (Map<K, V>) object;
+ }
+
public static <K, V> Map<K, V> toMap(Class<K> keyType, Class<V> valueType, Object... data) {
if (data == null) {
return null;
@@ -116,6 +138,7 @@
return map;
}
+ @SuppressWarnings("hiding")
public static <K, Object> Map<K, Object> toMap(Class<K> keyType, Object... data) {
if (data == null) {
return null;