|
Java EE 5 SDK | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.sun.appserv.management.util.misc.TypeCast
public final class TypeCast
This utility class contains two types of methods:
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)
,
checkMap(java.util.Map, ?>, java.lang.Class, java.lang.Class)
, checkList(java.util.List>, java.lang.Class)
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
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 Summary | |
---|---|
TypeCast()
|
Method Summary | ||
---|---|---|
static
|
asArray(Object o)
The caller should take appropriate care that the type is correct. |
|
static
|
asClass(Class<?> c)
The caller should take appropriate care that the type is correct. |
|
static
|
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 instead
if there is any doubt. |
|
static
|
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 instead if there is
any doubt. |
|
static
|
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 instead if there is
any doubt. |
|
static
|
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 instead if there is
any doubt. |
|
static
|
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 instead if there is
any doubt. |
|
static
|
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 instead if there is
any doubt. |
|
static
|
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
|
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 instead if there is
any doubt. |
|
static
|
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 instead if there is
any doubt. |
|
static
|
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 instead if there is
any doubt. |
|
static
|
checkArray(Object[] a,
Class<T> theClass)
Verify that the elements are all assignable to an object of the specified class. |
|
static
|
checkCollection(Collection<?> c,
Class<T> theClass)
Verify that the elements are all assignable to an object of the specified class. |
|
static
|
checkedCollection(Collection<?> c,
Class<T> theClass)
Create a checked Collection |
|
static
|
checkedList(List<?> l,
Class<T> theClass)
Create a checked List |
|
static
|
checkedMap(Map<?,?> m,
Class<K> keyClass,
Class<V> valueClass)
Create a checked Map |
|
static
|
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
|
checkList(List<?> l,
Class<T> theClass)
Verify that the elements are all assignable to an object of the specified class. |
|
static
|
checkMap(Map<?,?> m,
Class<K> keyClass,
Class<V> valueClass)
Verify that the elements are all assignable to an object of the specified class. |
|
static
|
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
|
checkSet(Set<?> s,
Class<T> theClass)
Verify that the elements are all assignable to an object of the specified class. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public TypeCast()
Method Detail |
---|
public static <T> Collection<T> asCollection(Object o)
checkCollection(java.util.Collection>, java.lang.Class)
instead
if there is any doubt.
o
- the Object, which must be a Collection
public static <T extends Serializable> Collection<T> asSerializableCollection(Object c)
checkCollection(java.util.Collection>, java.lang.Class)
instead if there is
any doubt.
public static <K,V> Map<K,V> asMap(Object m)
checkMap(java.util.Map, ?>, java.lang.Class, java.lang.Class)
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, java.lang.Class)
instead if there is
any doubt.
public static <T> List<T> asList(Object list)
checkList(java.util.List>, java.lang.Class)
instead if there is
any doubt.
public static <T extends Serializable> List<T> asSerializableList(Object list)
checkList(java.util.List>, java.lang.Class)
instead if there is
any doubt.
public static <T> Set<T> asSet(Object s)
checkSet(java.util.Set>, java.lang.Class)
instead if there is
any doubt.
public static <T> SortedSet<T> asSortedSet(Object s)
checkSet(java.util.Set>, java.lang.Class)
instead if there is
any doubt.
public static <T extends Serializable> Set<T> asSerializableSet(Object s)
checkSet(java.util.Set>, java.lang.Class)
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)
ClassCastException
public static void checkSerializableElements(Collection<?> l)
ClassCastException
public static Collection<Serializable> checkSerializable(Collection<?> l)
ClassCastException
public 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.
ClassCastException
public static Map<Serializable,Serializable> checkSerializable(Map<?,?> m)
ClassCastException
public static Serializable checkSerializable(Object o)
ClassCastException
public static <T> Collection<T> checkCollection(Collection<?> c, Class<T> theClass)
theClass
- the Class which the element must extendc
-
ClassCastException
public static <T> List<T> checkList(List<?> l, Class<T> theClass)
l
- the listtheClass
- the Class which the element must extend
ClassCastException
public static <T> Set<T> checkSet(Set<?> s, Class<T> theClass)
s
- theClass
- the Class which the element must extend
ClassCastException
public 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 extend
ClassCastException
public static <T> T checkObject(Object o, Class<T> theClass)
theClass
- the Classo
- the Object
ClassCastException
public static <T> void checkArray(Object[] a, Class<T> theClass)
theClass
- the Class which the element must extenda
- the Array of elements
ClassCastException
public static Collection<String> checkedStringCollection(Collection<?> c)
c
- the Collection
ClassCastException
public static Set<String> checkedStringSet(Set<?> s)
s
- the Set
ClassCastException
public static List<String> checkedStringList(List<?> l)
l
- the List
ClassCastException
public static Map<String,String> checkedStringMap(Map<?,?> m)
m
- the Map
ClassCastException
public static <T> Collection<T> checkedCollection(Collection<?> c, Class<T> theClass)
c
- the Collection
ClassCastException
public static <T> Set<T> checkedSet(Set<?> s, Class<T> theClass)
s
- the Set
ClassCastException
public static <T> List<T> checkedList(List<?> l, Class<T> theClass)
l
- the List
ClassCastException
public static <K,V> Map<K,V> checkedMap(Map<?,?> m, Class<K> keyClass, Class<V> valueClass)
m
- the Map
ClassCastException
|
Java EE 5 SDK | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright 2006 Sun Microsystems, Inc. All rights reserved.