FileDocCategorySizeDatePackage
TypeCast.javaAPI DocGlassfish v2 API17714Fri May 04 22:31:06 BST 2007com.sun.appserv.management.util.misc

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 appropriate {@link #checkCollection}, {@link #checkMap}, {@link #checkList} 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, {@link #checkMap} is strongly advised over {@link #asMap}. 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.

Fields Summary
Constructors Summary
Methods Summary
public static T[]asArray(java.lang.Object o)
The caller should take appropriate care that the type is correct.

return
Class

	    return (T[])Object[].class.cast( o );
	
public static java.lang.ClassasClass(java.lang.Class c)
The caller should take appropriate care that the type is correct.

return
Class

	    if ( ! ( c instanceof Class ) )
	    {
	        throw new IllegalArgumentException( "" + c );
	    }
	    
	    return (Class<T>)Class.class.cast( c );
	
public static java.util.CollectionasCollection(java.lang.Object o)
The caller should take appropriate care that the type of element is correct, and may want to call {@link #checkCollection} instead if there is any doubt.

param
o the Object, which must be a {@link Collection}
return
Collection

	    return (Collection<T>)Collection.class.cast( o );
	
public static java.util.HashtableasHashtable(java.lang.Object o)
The caller should take appropriate care that the type of element is correct, and may want to call {@link #checkMap} instead if there is any doubt.

return
Hashtable

	    return (Hashtable<K,V>)Hashtable.class.cast( o );
	
public static java.util.ListasList(java.lang.Object list)
The caller should take appropriate care that the type of element is correct, and may want to call {@link #checkList} instead if there is any doubt.

return
List

	    return (List<T>)List.class.cast( list );
	
public static java.util.MapasMap(java.lang.Object m)
The caller should take appropriate care that the type of keys/values is correct, and may want to call {@link #checkMap} instead if there is any doubt.

return
Map
return
Map

	    return (Map<K,V>)Map.class.cast( m );
	
public static java.util.CollectionasSerializableCollection(java.lang.Object c)
The caller should take appropriate care that the type of element is correct, and may want to call {@link #checkCollection} instead if there is any doubt.

return
Collection

	    final Collection<T>   result  = asCollection( c );
	    checkSerializable( result );
	    return result;
	
public static java.util.ListasSerializableList(java.lang.Object list)
The caller should take appropriate care that the type of element is correct, and may want to call{@link #checkList} instead if there is any doubt.

return
List

	    final List<T>   result  = asList( list );
	    checkSerializable( result );
	    return result;
	
public static java.util.MapasSerializableMap(java.lang.Object m)
The caller should take appropriate care that the type of keys/values is correct, and may want to call {@link #checkSerializable} instead if there is any doubt.

return
Map

	    final Map<K,V> result   = asMap( m );
	    checkSerializable( result );
	    
	    return result;
	
public static java.util.SetasSerializableSet(java.lang.Object s)
The caller should take appropriate care that the type of element is correct, and may want to call {@link #checkSet} instead if there is any doubt.

return
Set

	    final Set<T>   result  = asSet( s );
	    checkSerializable( result );
	    return result;
	
public static java.util.SetasSet(java.lang.Object s)
The caller should take appropriate care that the type of element is correct, and may want to call {@link #checkSet} instead if there is any doubt.

return
Set

	    return (Set<T>)Set.class.cast( s );
	
public static java.util.SortedSetasSortedSet(java.lang.Object s)
The caller should take appropriate care that the type of element is correct, and may want to call {@link #checkSet} instead if there is any doubt.

return
Set

	    return (SortedSet<T>)Set.class.cast( s );
	
public static voidcheckArray(java.lang.Object[] a, java.lang.Class theClass)
Verify that the elements are all assignable to an object of the specified class.

param
theClass the Class which the element must extend
param
a the Array of elements
throws
ClassCastException

	    for( final Object o : a )
	    {
	        checkObject( o, theClass );
	    }
	
public static java.util.CollectioncheckCollection(java.util.Collection c, java.lang.Class theClass)
Verify that the elements are all assignable to an object of the specified class.

param
theClass the Class which the element must extend
param
c
throws
ClassCastException

	    if ( c != null )
	    {
    	    for( final Object o : c )
    	    {
    	        checkObject( o, theClass );
    	    }
	    }
	    
	    return asCollection(c);
	
public static java.util.ListcheckList(java.util.List l, java.lang.Class theClass)
Verify that the elements are all assignable to an object of the specified class.

param
l the list
param
theClass the Class which the element must extend
throws
ClassCastException

	    if ( l != null )
	    {
    	    for( final Object o : l )
    	    {
    	        checkObject( o, theClass );
    	    }
	    }
	    
	    return asList(l);
	
public static java.util.MapcheckMap(java.util.Map m, java.lang.Class keyClass, java.lang.Class valueClass)
Verify that the elements are all assignable to an object of the specified class.

param
m
param
keyClass the Class which keys must extend
param
valueClass the Class which values must extend
throws
ClassCastException

	    if ( m != null )
	    {
	        checkSet( m.keySet(), keyClass  );
	        checkCollection( m.values(), valueClass );
	    }
	    
	    return asMap( m );
	
public static TcheckObject(java.lang.Object o, java.lang.Class theClass)
Verify that the Object is assignable to an object of the specified class.

param
theClass the Class
param
o the Object
throws
ClassCastException

	    if ( o != null && ! theClass.isAssignableFrom( o.getClass() ) )
	    {
	        throw new ClassCastException( "Object of class " + o.getClass().getName() +
	            " not assignment compatible with: " + theClass.getName() );
	    }
	    return (T)o;
	
public static voidcheckSerializable(java.lang.Object[] a)
Verify that all elements implement java.io.Serializable

throws
ClassCastException

	    for( final Object o : a )
	    {
	        checkSerializable( o );
	    }
	
public static java.util.CollectioncheckSerializable(java.util.Collection l)
Verify that all elements implement java.io.Serializable

throws
ClassCastException

	    checkSerializable( l, true );
	    return asCollection( l );
	
public static java.util.CollectioncheckSerializable(java.util.Collection l, boolean collectionItself)
Verify that all elements implement java.io.Serializable

param
l the Collection
param
collectionItself if true, the Collection itself is additionally checked, if false only the elements are checked.
throws
ClassCastException

	    if ( collectionItself )
	    {
	        checkSerializable( (Object)l );
	    }
	    
	    checkSerializableElements( l );
	    
	    return asCollection( l );
	
public static java.util.MapcheckSerializable(java.util.Map m)
Verify that the Map itself, and all keys and values implement java.io.Serializable

throws
ClassCastException

	    checkSerializable( (Object)m );
	    
	    // the key set used by HashMap isn't Serializable; apparently
	    // HashMap serializes itself properly, but not by serializing
	    // the key set directly. So if the Map is Serializable, we
	    // can't necessarily constrain the key set and value set to be so,
	    // since it's up to the Map to serialize properly.
	    checkSerializable( m.keySet(), false);
	    checkSerializable( m.values(), false);
	    
	    return asMap( m );
	
public static java.io.SerializablecheckSerializable(java.lang.Object o)
Verify that the Object implements java.io.Serializable.

throws
ClassCastException

	    if ( (o != null) && ! (o instanceof Serializable) )
	    {
	        throw new ClassCastException( "Object not Serializable, class = " + o.getClass().getName() );
	    }
	    
	    return Serializable.class.cast( o );
	
public static voidcheckSerializableElements(java.util.Collection l)
Verify that all elements implement java.io.Serializable

throws
ClassCastException

	    for( final Object o : l )
	    {
	        checkSerializable( o );
	    }
	
public static java.util.SetcheckSet(java.util.Set s, java.lang.Class theClass)
Verify that the elements are all assignable to an object of the specified class.

param
s
param
theClass the Class which the element must extend
throws
ClassCastException

	    if ( s != null )
	    {
    	    for( final Object o : s )
    	    {
    	        checkObject( o, theClass );
    	    }
    	}
	    
	    return asSet(s);
	
public static java.util.CollectioncheckedCollection(java.util.Collection c, java.lang.Class theClass)
Create a checked Collection, first verifying that all elements are in fact String.

param
c the Collection
throws
ClassCastException

	    final Collection<T> cc = checkCollection( c, theClass );
	    return Collections.checkedCollection( cc, theClass );
	
public static java.util.ListcheckedList(java.util.List l, java.lang.Class theClass)
Create a checked List, first verifying that all elements are in fact String.

param
l the List
throws
ClassCastException

	    final List<T> cl = checkList( l, theClass );
	    return Collections.checkedList( cl, theClass );
	
public static java.util.MapcheckedMap(java.util.Map m, java.lang.Class keyClass, java.lang.Class valueClass)
Create a checked Map, first verifying that all keys and values are in fact String.

param
m the Map
throws
ClassCastException

	    final Map<K,V> cm   = checkMap( m, keyClass, valueClass );
	    return Collections.checkedMap( cm, keyClass, valueClass );
	
public static java.util.SetcheckedSet(java.util.Set s, java.lang.Class theClass)
Create a checked Set, first verifying that all elements are in fact String.

param
s the Set
throws
ClassCastException

	    final Set<T> cs = checkSet( s, theClass );
	    return Collections.checkedSet( cs, theClass );
	
public static java.util.CollectioncheckedStringCollection(java.util.Collection c)
Create a checked Collection, first verifying that all elements are in fact String.

param
c the Collection
throws
ClassCastException

	    return checkedCollection( c, String.class );
	
public static java.util.ListcheckedStringList(java.util.List l)
Create a checked List, first verifying that all elements are in fact String.

param
l the List
throws
ClassCastException

	    return checkedList( l, String.class );
	
public static java.util.MapcheckedStringMap(java.util.Map m)
Create a checked Map, first verifying that all keys and values are in fact String.

param
m the Map
throws
ClassCastException

	    return checkedMap( m, String.class, String.class);
	
public static java.util.SetcheckedStringSet(java.util.Set s)
Create a checked Set, first verifying that all elements are in fact String.

param
s the Set
throws
ClassCastException

	    return checkedSet( s, String.class );