Class Cast.Unsafe

  • Enclosing class:
    Cast

    public static final class Cast.Unsafe
    extends Object
    Unsafe utilities for casting objects. This class cannot guarantee accurate casts.
    • Method Detail

      • generic

        public <T> Optional<T> generic​(TypeCompatible<T> type,
                                       @NullOr Object object)
        Attempts to cast an object into the specified generic type.

        Warning: since generic type information is erased, casting will "succeed" for any instance of a generic class.

        Type Parameters:
        T - the generic type
        Parameters:
        type - generic type to cast into
        object - object to cast
        Returns:
        the potentially cast object, otherwise empty
      • generic

        public <T> Function<@NullOr Object,​Optional<T>> generic​(TypeCompatible<T> type)
        Creates a function that attempts to cast objects into the specified generic type.

        Warning: since generic type information is erased, casting will "succeed" for any instance of a generic class.

        Type Parameters:
        T - the generic type
        Parameters:
        type - generic type to cast into
        Returns:
        a function that casts objects into the specified generic type
        See Also:
        generic(TypeCompatible, Object)
      • genericOptional

        public <T> Optional<T> genericOptional​(TypeCompatible<T> type,
                                               Optional<?> optional)
        Attempts to cast an Optional's contents into the specified generic type.

        Warning: since generic type information is erased, casting will "succeed" for any instance of a generic class.

        Type Parameters:
        T - the generic type
        Parameters:
        type - generic type to cast into
        optional - optional to cast
        Returns:
        a present Optional if its contents were successfully cast, otherwise empty