Class Cast


  • public class Cast
    extends Object
    Utilities for casting objects.
    • Method Detail

      • as

        public static <T> Optional<T> as​(Class<T> type,
                                         @NullOr Object object)
        Attempts to cast an object into the specified type.
        Type Parameters:
        T - the type
        Parameters:
        type - type to cast into
        object - object to cast
        Returns:
        the successfully cast object, otherwise empty
      • as

        public static <T> Function<@NullOr Object,​Optional<T>> as​(Class<T> type)
        Creates a function that attempts to cast objects into the specified type.
        Type Parameters:
        T - the type
        Parameters:
        type - type to cast into
        Returns:
        a function that casts objects into the specified type
        See Also:
        as(Class, Object)
      • optional

        public static <T> Optional<T> optional​(Class<T> type,
                                               Optional<?> optional)
        Attempts to cast an Optional's contents into the specified type.
        Type Parameters:
        T - the type
        Parameters:
        type - type to cast into
        optional - optional to cast
        Returns:
        a present optional if its contents were successfully cast, otherwise empty
      • unsafe

        public static Cast.Unsafe unsafe()
        Gets the unsafe casting utilities instance.

        Warning!

        The unsafe instance cannot guarantee accurate casts.

        Returns:
        the unsafe casting utilities instance