Class TypeCast
- java.lang.Object
-
- org.glassfish.admin.amx.util.TypeCast
-
public final class TypeCast extends Object
This utility class contains two types of methods:- Methods to cast Collection/List/Set/Map/etc to a more strongly-typed generic type;
- Methods to verify the types of elements within the above.
Due to the way in which generic types are implemented in JDK 1.5, coupled with the fact that both generic and non-generic code need to coexist, there exist a variety of cases in which casts cannot be avoided. However, performing such cast generates compiler warnings which cannot be eliminated, and which thus produce clutter which makes it hard to recognize other warnings during compilation.
The casting methods here localize the aforementioned compiler warnings to this file thus allowing code elsewhere to compile "cleanly" (eg without warnings).
Clients should use the casting routines only when there is no other appropriate solution. For example, consider a caller of non-generic code method getStuff():
Map getStuff()
The javadoc for getStuff() specifies that the keys and values of the Map are java.lang.String. The caller would like to declare:final Map<String,String> m = getStuff();
But this will generate a compiler warning. To avoid this compiler warning, the code should be written as follows:final Map<String,String> m = TypeCast.asMap( getStuff() );
If there is any doubt as to the correct contents of a Collection/List/Set/Map, use the appropriatecheckCollection(java.util.Collection<?>, java.lang.Class<T>)
,checkMap(java.util.Map<?, ?>, java.lang.Class<K>, java.lang.Class<V>)
,checkList(java.util.List<?>, java.lang.Class<T>)
method.Due to the way generics are implemented, an explicit call is needed with a specific class in order to do so; this is why the as() methods do not already perform that check. Following the above example, we would write:
TypeCast.checkCompatible(m, String.class, String.class)
Naturally checking the keys and values of the Map is far more expensive than a simple cast, but if the contents are unclear,
checkMap(java.util.Map<?, ?>, java.lang.Class<K>, java.lang.Class<V>)
is strongly advised overasMap(java.lang.Object)
. The same holds true for the Collection, Set, and List variants of these methods. Most casts can be handled appropriately through the appropriate use of generic types.
-
-
Constructor Summary
Constructors Constructor Description TypeCast()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T[]
asArray(Object o)
The caller should take appropriate care that the type is correct.static <T> Class<T>
asClass(Class<?> c)
The caller should take appropriate care that the type is correct.static <T> Collection<T>
asCollection(Object o)
The caller should take appropriate care that the type of element is correct, and may want to callcheckCollection(java.util.Collection<?>, java.lang.Class<T>)
instead if there is any doubt.static <K,V>
Hashtable<K,V>asHashtable(Object o)
The caller should take appropriate care that the type of element is correct, and may want to callcheckMap(java.util.Map<?, ?>, java.lang.Class<K>, java.lang.Class<V>)
instead if there is any doubt.static <T> List<T>
asList(Object list)
The caller should take appropriate care that the type of element is correct, and may want to callcheckList(java.util.List<?>, java.lang.Class<T>)
instead if there is any doubt.static <K,V>
Map<K,V>asMap(Object m)
The caller should take appropriate care that the type of keys/values is correct, and may want to callcheckMap(java.util.Map<?, ?>, java.lang.Class<K>, java.lang.Class<V>)
instead if there is any doubt.static <T extends Serializable>
Collection<T>asSerializableCollection(Object c)
The caller should take appropriate care that the type of element is correct, and may want to callcheckCollection(java.util.Collection<?>, java.lang.Class<T>)
instead if there is any doubt.static <T extends Serializable>
List<T>asSerializableList(Object list)
The caller should take appropriate care that the type of element is correct, and may want to callcheckList(java.util.List<?>, java.lang.Class<T>)
instead if there is any doubt.static <K extends Serializable,V extends Serializable>
Map<K,V>asSerializableMap(Object m)
The caller should take appropriate care that the type of keys/values is correct, and may want to callcheckSerializable(java.lang.Object[])
instead if there is any doubt.static <T extends Serializable>
Set<T>asSerializableSet(Object s)
The caller should take appropriate care that the type of element is correct, and may want to callcheckSet(java.util.Set<?>, java.lang.Class<T>)
instead if there is any doubt.static <T> Set<T>
asSet(Object s)
The caller should take appropriate care that the type of element is correct, and may want to callcheckSet(java.util.Set<?>, java.lang.Class<T>)
instead if there is any doubt.static <T> SortedSet<T>
asSortedSet(Object s)
The caller should take appropriate care that the type of element is correct, and may want to callcheckSet(java.util.Set<?>, java.lang.Class<T>)
instead if there is any doubt.static <T> void
checkArray(Object[] a, Class<T> theClass)
Verify that the elements are all assignable to an object of the specified class.static <T> Collection<T>
checkCollection(Collection<?> c, Class<T> theClass)
Verify that the elements are all assignable to an object of the specified class.static <T> Collection<T>
checkedCollection(Collection<?> c, Class<T> theClass)
Create a checked Collection, first verifying that all elements are in fact String. static <T> List<T>
checkedList(List<?> l, Class<T> theClass)
Create a checked List, first verifying that all elements are in fact String. static <K,V>
Map<K,V>checkedMap(Map<?,?> m, Class<K> keyClass, Class<V> valueClass)
Create a checked Map, first verifying that all keys and values are in fact String. static <T> Set<T>
checkedSet(Set<?> s, Class<T> theClass)
Create a checked Set, first verifying that all elements are in fact String. static Collection<String>
checkedStringCollection(Collection<?> c)
Create a checked Collection, first verifying that all elements are in fact String. static List<String>
checkedStringList(List<?> l)
Create a checked List, first verifying that all elements are in fact String. static Map<String,String>
checkedStringMap(Map<?,?> m)
Create a checked Map, first verifying that all keys and values are in fact String. static Set<String>
checkedStringSet(Set<?> s)
Create a checked Set, first verifying that all elements are in fact String. static <T> List<T>
checkList(List<?> l, Class<T> theClass)
Verify that the elements are all assignable to an object of the specified class.static <K,V>
Map<K,V>checkMap(Map<?,?> m, Class<K> keyClass, Class<V> valueClass)
Verify that the elements are all assignable to an object of the specified class.static <T> T
checkObject(Object o, Class<T> theClass)
Verify that the Object is assignable to an object of the specified class.static Serializable
checkSerializable(Object o)
Verify that the Object implements java.io.Serializable.static void
checkSerializable(Object[] a)
Verify that all elements implement java.io.Serializablestatic Collection<Serializable>
checkSerializable(Collection<?> l)
Verify that all elements implement java.io.Serializablestatic Collection<Serializable>
checkSerializable(Collection<?> l, boolean collectionItself)
Verify that all elements implement java.io.Serializablestatic Map<Serializable,Serializable>
checkSerializable(Map<?,?> m)
Verify that the Map itself, and all keys and values implement java.io.Serializablestatic void
checkSerializableElements(Collection<?> l)
Verify that all elements implement java.io.Serializablestatic <T> Set<T>
checkSet(Set<?> s, Class<T> theClass)
Verify that the elements are all assignable to an object of the specified class.
-
-
-
Method Detail
-
asCollection
public static <T> Collection<T> asCollection(Object o)
The caller should take appropriate care that the type of element is correct, and may want to callcheckCollection(java.util.Collection<?>, java.lang.Class<T>)
instead if there is any doubt.- Parameters:
o
- the Object, which must be aCollection
- Returns:
- Collection
-
asSerializableCollection
public static <T extends Serializable> Collection<T> asSerializableCollection(Object c)
The caller should take appropriate care that the type of element is correct, and may want to callcheckCollection(java.util.Collection<?>, java.lang.Class<T>)
instead if there is any doubt.- Returns:
- Collection
-
asMap
public static <K,V> Map<K,V> asMap(Object m)
The caller should take appropriate care that the type of keys/values is correct, and may want to callcheckMap(java.util.Map<?, ?>, java.lang.Class<K>, java.lang.Class<V>)
instead if there is any doubt.- Returns:
- Map
-
asSerializableMap
public static <K extends Serializable,V extends Serializable> Map<K,V> asSerializableMap(Object m)
The caller should take appropriate care that the type of keys/values is correct, and may want to callcheckSerializable(java.lang.Object[])
instead if there is any doubt.- Returns:
- Map
-
asHashtable
public static <K,V> Hashtable<K,V> asHashtable(Object o)
The caller should take appropriate care that the type of element is correct, and may want to callcheckMap(java.util.Map<?, ?>, java.lang.Class<K>, java.lang.Class<V>)
instead if there is any doubt.- Returns:
- Hashtable
-
asList
public static <T> List<T> asList(Object list)
The caller should take appropriate care that the type of element is correct, and may want to callcheckList(java.util.List<?>, java.lang.Class<T>)
instead if there is any doubt.- Returns:
- List
-
asSerializableList
public static <T extends Serializable> List<T> asSerializableList(Object list)
The caller should take appropriate care that the type of element is correct, and may want to callcheckList(java.util.List<?>, java.lang.Class<T>)
instead if there is any doubt.- Returns:
- List
-
asSet
public static <T> Set<T> asSet(Object s)
The caller should take appropriate care that the type of element is correct, and may want to callcheckSet(java.util.Set<?>, java.lang.Class<T>)
instead if there is any doubt.- Returns:
- Set
-
asSortedSet
public static <T> SortedSet<T> asSortedSet(Object s)
The caller should take appropriate care that the type of element is correct, and may want to callcheckSet(java.util.Set<?>, java.lang.Class<T>)
instead if there is any doubt.- Returns:
- Set
-
asSerializableSet
public static <T extends Serializable> Set<T> asSerializableSet(Object s)
The caller should take appropriate care that the type of element is correct, and may want to callcheckSet(java.util.Set<?>, java.lang.Class<T>)
instead if there is any doubt.- Returns:
- Set
-
asClass
public static <T> Class<T> asClass(Class<?> c)
The caller should take appropriate care that the type is correct.- Returns:
- Class
-
asArray
public static <T> T[] asArray(Object o)
The caller should take appropriate care that the type is correct.- Returns:
- Class
-
checkSerializable
public static void checkSerializable(Object[] a)
Verify that all elements implement java.io.Serializable- Throws:
ClassCastException
-
checkSerializableElements
public static void checkSerializableElements(Collection<?> l)
Verify that all elements implement java.io.Serializable- Throws:
ClassCastException
-
checkSerializable
public static Collection<Serializable> checkSerializable(Collection<?> l)
Verify that all elements implement java.io.Serializable- Throws:
ClassCastException
-
checkSerializable
public static Collection<Serializable> checkSerializable(Collection<?> l, boolean collectionItself)
Verify that all elements implement java.io.Serializable- Parameters:
l
- the CollectioncollectionItself
- if true, the Collection itself is additionally checked, if false only the elements are checked.- Throws:
ClassCastException
-
checkSerializable
public static Map<Serializable,Serializable> checkSerializable(Map<?,?> m)
Verify that the Map itself, and all keys and values implement java.io.Serializable- Throws:
ClassCastException
-
checkSerializable
public static Serializable checkSerializable(Object o)
Verify that the Object implements java.io.Serializable.- Throws:
ClassCastException
-
checkCollection
public static <T> Collection<T> checkCollection(Collection<?> c, Class<T> theClass)
Verify that the elements are all assignable to an object of the specified class.- Parameters:
theClass
- the Class which the element must extendc
-- Throws:
ClassCastException
-
checkList
public static <T> List<T> checkList(List<?> l, Class<T> theClass)
Verify that the elements are all assignable to an object of the specified class.- Parameters:
l
- the listtheClass
- the Class which the element must extend- Throws:
ClassCastException
-
checkSet
public static <T> Set<T> checkSet(Set<?> s, Class<T> theClass)
Verify that the elements are all assignable to an object of the specified class.- Parameters:
s
-theClass
- the Class which the element must extend- Throws:
ClassCastException
-
checkMap
public static <K,V> Map<K,V> checkMap(Map<?,?> m, Class<K> keyClass, Class<V> valueClass)
Verify that the elements are all assignable to an object of the specified class.- Parameters:
m
-keyClass
- the Class which keys must extendvalueClass
- the Class which values must extend- Throws:
ClassCastException
-
checkObject
public static <T> T checkObject(Object o, Class<T> theClass)
Verify that the Object is assignable to an object of the specified class.- Parameters:
theClass
- the Classo
- the Object- Throws:
ClassCastException
-
checkArray
public static <T> void checkArray(Object[] a, Class<T> theClass)
Verify that the elements are all assignable to an object of the specified class.- Parameters:
theClass
- the Class which the element must extenda
- the Array of elements- Throws:
ClassCastException
-
checkedStringCollection
public static Collection<String> checkedStringCollection(Collection<?> c)
Create a checked Collection, first verifying that all elements are in fact String. - Parameters:
c
- the Collection- Throws:
ClassCastException
-
checkedStringSet
public static Set<String> checkedStringSet(Set<?> s)
Create a checked Set, first verifying that all elements are in fact String. - Parameters:
s
- the Set- Throws:
ClassCastException
-
checkedStringList
public static List<String> checkedStringList(List<?> l)
Create a checked List, first verifying that all elements are in fact String. - Parameters:
l
- the List- Throws:
ClassCastException
-
checkedStringMap
public static Map<String,String> checkedStringMap(Map<?,?> m)
Create a checked Map, first verifying that all keys and values are in fact String. - Parameters:
m
- the Map- Throws:
ClassCastException
-
checkedCollection
public static <T> Collection<T> checkedCollection(Collection<?> c, Class<T> theClass)
Create a checked Collection, first verifying that all elements are in fact String. - Parameters:
c
- the Collection- Throws:
ClassCastException
-
checkedSet
public static <T> Set<T> checkedSet(Set<?> s, Class<T> theClass)
Create a checked Set, first verifying that all elements are in fact String. - Parameters:
s
- the Set- Throws:
ClassCastException
-
checkedList
public static <T> List<T> checkedList(List<?> l, Class<T> theClass)
Create a checked List, first verifying that all elements are in fact String. - Parameters:
l
- the List- Throws:
ClassCastException
-
checkedMap
public static <K,V> Map<K,V> checkedMap(Map<?,?> m, Class<K> keyClass, Class<V> valueClass)
Create a checked Map, first verifying that all keys and values are in fact String. - Parameters:
m
- the Map- Throws:
ClassCastException
-
-