com.sun.appserv.management.util.misc
Class TypeCast

java.lang.Object
  extended by com.sun.appserv.management.util.misc.TypeCast

public final class TypeCast
extends Object

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, 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
<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) 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, java.lang.Class) 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) 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, java.lang.Class) 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 call checkCollection(java.util.Collection, java.lang.Class) 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) 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 call checkSerializable(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 call checkSet(java.util.Set, java.lang.Class) 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) 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) 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 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TypeCast

public TypeCast()
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 call checkCollection(java.util.Collection, java.lang.Class) instead if there is any doubt.

Parameters:
o - the Object, which must be a Collection
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 call checkCollection(java.util.Collection, java.lang.Class) 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 call checkMap(java.util.Map, java.lang.Class, java.lang.Class) 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 call checkSerializable(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 call checkMap(java.util.Map, java.lang.Class, java.lang.Class) 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 call checkList(java.util.List, java.lang.Class) 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) 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 call checkSet(java.util.Set, java.lang.Class) 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 call checkSet(java.util.Set, java.lang.Class) 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 call checkSet(java.util.Set, java.lang.Class) 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 Collection
collectionItself - 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 extend
c -
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 list
theClass - 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 extend
valueClass - 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 Class
o - 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 extend
a - 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