Class Cast


  • public final class Cast
    extends Object

    A collection of static methods making casting from one type to another a bit easier. Note that the standard use case for the collection based conversions are for use in for loops or method calls where the collection is read only.

    Note that these methods only cast and do not convert, i.e. an Integer object cannot be cast to a Double object. If conversion is needed then transformations should be done using Guava or the Converter class.

    Author:
    David B. Bracewell
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T as​(Object o)
      Casts an object to the desired return throwing a java.lang.ClassCastException if the given object cannot be cast as the desired type.
      static <T> T as​(Object o, @NonNull Class<T> clazz)
      Casts an object to a given type throwing a java.lang.ClassCastException if the given object cannot be cast as the desired type.
      static <T> Iterable<T> cast​(Iterable<?> iterable)
      Casts the elements in an iterable in a lazy fashion.
      static <T> Collection<T> cast​(Collection<?> collection)
      Casts the elements in a collection in a lazy fashion.
      static <T> Iterator<T> cast​(Iterator<?> iterator)
      Casts the elements in an iterator in a lazy fashion.
      static <T> List<T> cast​(List<?> list)
      Casts the elements in a list in a lazy fashion.
      static <K,​V>
      Map<K,​V>
      cast​(Map<?,​?> map)
      Casts a map in a lazy fashion.
      static <T> Set<T> cast​(Set<?> set)
      Casts the elements in a set in a lazy fashion.
    • Method Detail

      • as

        public static <T> T as​(Object o)
        Casts an object to the desired return throwing a java.lang.ClassCastException if the given object cannot be cast as the desired type.
        Type Parameters:
        T - the type parameter
        Parameters:
        o - the object
        Returns:
        the casted object or null if the object was null
        Throws:
        ClassCastException - If the given object cannot be cast as the return type
      • as

        public static <T> T as​(Object o,
                               @NonNull
                               @NonNull Class<T> clazz)
        Casts an object to a given type throwing a java.lang.ClassCastException if the given object cannot be cast as the desired type.
        Type Parameters:
        T - the type parameter
        Parameters:
        o - the object
        clazz - The class to cast to
        Returns:
        the casted object or null if the object was null or the object was not of the desired type
      • cast

        public static <T> Iterable<T> cast​(Iterable<?> iterable)

        Casts the elements in an iterable in a lazy fashion. Calls to remove on the iterator will be reflected in iterable

        Type Parameters:
        T - the type parameter
        Parameters:
        iterable - the iterable
        Returns:
        the casted iterable or null if the given iterable is null
      • cast

        public static <T> Set<T> cast​(Set<?> set)

        Casts the elements in a set in a lazy fashion. Changes to the returned set are reflected in the the given set.

        Type Parameters:
        T - the type parameter
        Parameters:
        set - the set
        Returns:
        the casted set or null if the given set is null
      • cast

        public static <T> List<T> cast​(List<?> list)

        Casts the elements in a list in a lazy fashion. Changes to the returned list are reflected in the the given list.

        Type Parameters:
        T - the type parameter
        Parameters:
        list - the list to cast
        Returns:
        the casted list or null if the given list is null
      • cast

        public static <T> Iterator<T> cast​(Iterator<?> iterator)

        Casts the elements in an iterator in a lazy fashion. Changes to the returned iterator are reflected in the the given iterator.

        Type Parameters:
        T - the type parameter
        Parameters:
        iterator - the iterator to cast
        Returns:
        the casted iterator or null if the given iterator was null
      • cast

        public static <T> Collection<T> cast​(Collection<?> collection)

        Casts the elements in a collection in a lazy fashion. Changes to the returned collection are reflected in the the given collection.

        Type Parameters:
        T - the type parameter
        Parameters:
        collection - the collection to cast
        Returns:
        the casted collection or null if the collection is null
      • cast

        public static <K,​V> Map<K,​V> cast​(Map<?,​?> map)

        Casts a map in a lazy fashion. Changes to the returned map are reflected in the the given map.

        Type Parameters:
        K - the type parameter
        V - the type parameter
        Parameters:
        map - the map to cast
        Returns:
        the casted map or null if the map is null