Class 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 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 Detail

      • TypeCast

        public TypeCast()
    • Method Detail

      • 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,
                                                                 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
      • 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