public final class TypeCast extends Object
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 appropriate
checkCollection(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 over asMap(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 and Description |
|---|
TypeCast() |
| Modifier and Type | Method and 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 call
checkCollection(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 call
checkMap(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 call
checkList(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 call
checkMap(java.util.Map<?, ?>, java.lang.Class<K>, java.lang.Class<V>) instead if there is
any doubt. |
static <T extends Serializable> |
asSerializableCollection(Object c)
The caller should take appropriate care that the type of element
is correct, and may want to call
checkCollection(java.util.Collection<?>, java.lang.Class<T>) instead if there is
any doubt. |
static <T extends Serializable> |
asSerializableList(Object list)
The caller should take appropriate care that the type of element
is correct, and may want to call
checkList(java.util.List<?>, java.lang.Class<T>) instead if there is
any doubt. |
static <K extends Serializable,V extends Serializable> |
asSerializableMap(Object m)
The caller should take appropriate care that the type of keys/values
is correct, and may want to call
checkSerializable(java.lang.Object[]) instead if there is
any doubt. |
static <T extends Serializable> |
asSerializableSet(Object s)
The caller should take appropriate care that the type of element
is correct, and may want to call
checkSet(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 call
checkSet(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 call
checkSet(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
|
static <T> List<T> |
checkedList(List<?> l,
Class<T> theClass)
Create a checked List
|
static <K,V> Map<K,V> |
checkedMap(Map<?,?> m,
Class<K> keyClass,
Class<V> valueClass)
Create a checked Map
|
static <T> Set<T> |
checkedSet(Set<?> s,
Class<T> theClass)
Create a checked Set
|
static Collection<String> |
checkedStringCollection(Collection<?> c)
Create a checked Collection
|
static List<String> |
checkedStringList(List<?> l)
Create a checked List
|
static Map<String,String> |
checkedStringMap(Map<?,?> m)
Create a checked Map
|
static Set<String> |
checkedStringSet(Set<?> s)
Create a checked Set
|
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 Collection<Serializable> |
checkSerializable(Collection<?> l)
Verify that all elements implement java.io.Serializable
|
static Collection<Serializable> |
checkSerializable(Collection<?> l,
boolean collectionItself)
Verify that all elements implement java.io.Serializable
|
static Map<Serializable,Serializable> |
checkSerializable(Map<?,?> m)
Verify that the Map itself, and all keys and values
implement java.io.Serializable
|
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.Serializable
|
static void |
checkSerializableElements(Collection<?> l)
Verify that all elements implement java.io.Serializable
|
static <T> Set<T> |
checkSet(Set<?> s,
Class<T> theClass)
Verify that the elements are all assignable to an object of the
specified class.
|
public static <T> Collection<T> asCollection(Object o)
checkCollection(java.util.Collection<?>, java.lang.Class<T>) instead
if there is any doubt.o - the Object, which must be a Collectionpublic static <T extends Serializable> Collection<T> asSerializableCollection(Object c)
checkCollection(java.util.Collection<?>, java.lang.Class<T>) instead if there is
any doubt.public static <K,V> Map<K,V> asMap(Object m)
checkMap(java.util.Map<?, ?>, java.lang.Class<K>, java.lang.Class<V>) instead if there is
any doubt.public static <K extends Serializable,V extends Serializable> Map<K,V> asSerializableMap(Object m)
checkSerializable(java.lang.Object[]) instead if there is
any doubt.public static <K,V> Hashtable<K,V> asHashtable(Object o)
checkMap(java.util.Map<?, ?>, java.lang.Class<K>, java.lang.Class<V>) instead if there is
any doubt.public static <T> List<T> asList(Object list)
checkList(java.util.List<?>, java.lang.Class<T>) instead if there is
any doubt.public static <T extends Serializable> List<T> asSerializableList(Object list)
checkList(java.util.List<?>, java.lang.Class<T>) instead if there is
any doubt.public static <T> Set<T> asSet(Object s)
checkSet(java.util.Set<?>, java.lang.Class<T>) instead if there is
any doubt.public static <T> SortedSet<T> asSortedSet(Object s)
checkSet(java.util.Set<?>, java.lang.Class<T>) instead if there is
any doubt.public static <T extends Serializable> Set<T> asSerializableSet(Object s)
checkSet(java.util.Set<?>, java.lang.Class<T>) instead if there is
any doubt.public static <T> Class<T> asClass(Class<?> c)
public static <T> T[] asArray(Object o)
public static void checkSerializable(Object[] a)
ClassCastExceptionpublic static void checkSerializableElements(Collection<?> l)
ClassCastExceptionpublic static Collection<Serializable> checkSerializable(Collection<?> l)
ClassCastExceptionpublic static Collection<Serializable> checkSerializable(Collection<?> l, boolean collectionItself)
l - the CollectioncollectionItself - if true, the Collection itself is additionally checked,
if false only the elements are checked.ClassCastExceptionpublic static Map<Serializable,Serializable> checkSerializable(Map<?,?> m)
ClassCastExceptionpublic static Serializable checkSerializable(Object o)
ClassCastExceptionpublic static <T> Collection<T> checkCollection(Collection<?> c, Class<T> theClass)
theClass - the Class which the element must extendc - ClassCastExceptionpublic static <T> List<T> checkList(List<?> l, Class<T> theClass)
l - the listtheClass - the Class which the element must extendClassCastExceptionpublic static <T> Set<T> checkSet(Set<?> s, Class<T> theClass)
s - theClass - the Class which the element must extendClassCastExceptionpublic static <K,V> Map<K,V> checkMap(Map<?,?> m, Class<K> keyClass, Class<V> valueClass)
m - keyClass - the Class which keys must extendvalueClass - the Class which values must extendClassCastExceptionpublic static <T> T checkObject(Object o, Class<T> theClass)
theClass - the Classo - the ObjectClassCastExceptionpublic static <T> void checkArray(Object[] a, Class<T> theClass)
theClass - the Class which the element must extenda - the Array of elementsClassCastExceptionpublic static Collection<String> checkedStringCollection(Collection<?> c)
c - the CollectionClassCastExceptionpublic static Set<String> checkedStringSet(Set<?> s)
s - the SetClassCastExceptionpublic static List<String> checkedStringList(List<?> l)
l - the ListClassCastExceptionpublic static Map<String,String> checkedStringMap(Map<?,?> m)
m - the MapClassCastExceptionpublic static <T> Collection<T> checkedCollection(Collection<?> c, Class<T> theClass)
c - the CollectionClassCastExceptionpublic static <T> Set<T> checkedSet(Set<?> s, Class<T> theClass)
s - the SetClassCastExceptionpublic static <T> List<T> checkedList(List<?> l, Class<T> theClass)
l - the ListClassCastExceptionpublic static <K,V> Map<K,V> checkedMap(Map<?,?> m, Class<K> keyClass, Class<V> valueClass)
m - the MapClassCastExceptionCopyright © 2017. All rights reserved.