Cast the content of optional or streams

Viewed 5337

I wish instead of writing my own method or class I could just check and cast the content of an optional to another class or have an empty object.

For this sub-problem in the application I want to have the custom user object of an instance of TreeNode casted to CustomUserObject, provided that the TreeNode is an instance of DefaultMutableTreeNode.

private Optional<CustomUserObject> getCustomUserObject(TreeNode node) {
    Optional<DefaultMutableTreeNode> optDefaultMutableTreeNode = OptionalUtil.cast(Optional.ofNullable(node), DefaultMutableTreeNode.class);
    Optional<Object> optUserObject = optDefaultMutableTreeNode.map(DefaultMutableTreeNode::getUserObject); //
    return OptionalUtil.cast(optUserObject, CustomUserObject.class);
}


/**
 * Maps the given optional, if the containing element is an instance of the given class.
 * Returns empty if the containing object is not an instance of the given class.
 * 
 * @param orgOptional
 *        given optional
 * @param clazz
 *        given class.
 * @return the resulting {@link Optional}.
 */
public static <T, X> Optional<T> cast(Optional<X> orgOptional, Class<T> clazz) {
    return orgOptional //
        .filter(clazz::isInstance) // check instance
        .map(clazz::cast); // cast
}

/**
 * Maps the given stream, if the containing element is an instance of the given class.
 * Returns empty if the containing object is not an instance of the given class.
 * 
 * @param orgStream
 *        given optional
 * @param clazz
 *        given class.
 * @return the resulting {@link Optional}.
 */
public static <T, X> Stream<T> cast(Stream<X> orgStream, Class<T> clazz) {
    return orgStream //
        .filter(clazz::isInstance) // check instance
        .map(clazz::cast); // cast
}

I remember I need to cast optionals or streams in this way quite often. It is not fluent. Actually I wish java Optional or Stream would have a cast method which does the above step. I don't want to write my own fluent CustomOptional. Did I miss anything? Is there any way to do this in a simpler way?

2 Answers

You can easily make this more fluent by relying on map()/flatMap() and cast methods that return functions instead.

For Optional, this is very easy since map() can act as a filter by returning null. So just define:

public static <U> Function<Object, U> filterAndCast(Class<? extends U> clazz) {
    return t -> clazz.isInstance(t) ? clazz.cast(t) : null;
}

and use it as:

Optional<Number> number = Optional.of(42L);
System.out.println(number.map(filterAndCast(Integer.class)));
System.out.println(number.map(filterAndCast(Long.class)));

Output:

Optional.empty
Optional[42]

For streams you can apply more or less the same trick by relying on flatMap() with a function that returns an empty Stream:

public static <U> Function<Object, Stream<U>> streamFilterAndCast(Class<? extends U> clazz) {
    return t -> clazz.isInstance(t) ? Stream.of(clazz.cast(t)) : Stream.empty();
    // or alternatively
    return t -> Stream.of(t).filter(clazz::isInstance).map(clazz::cast);
}

and use it as:

Stream.of(42L, "Hello world", 1024, 3.14)
        .flatMap(streamFilterAndCast(Number.class))
        .forEach(System.out::println);

Output:

42
1024
3.14

I came up with this for Optional

/**
 * Creates a function that tries to cast to the given class.
 * If the given argument is not an instance of the given class <code>Optional.empty()</code> is returned.
 * This function can be used in an <code>optionalObject.flatMap(Functional.cast(SomeClass.class))</code> context.
 * 
 * @param clazz
 *        given Clazz
 * @return Returns an optional of the given class.
 * @param <X>
 *        generic input argument for the function
 * @param <T>
 *        type of the output of the function
 */
public static <X, T> Function<X, Optional<T>> cast(Class<? extends T> clazz) {
    return x -> {
        if (clazz.isInstance(x)) {
            return Optional.of(clazz.cast(x));
        }
        return Optional.empty();
    };
}

and use it like this:

Optional<DefaultMutableTreeNode> optParent = Optional.of(node) // optional of original node
                    .map(ProfileTreeNode::getParent) // parent of this node.
                    .flatMap(FunctionalUtil.cast(DefaultMutableTreeNode.class)); // cast to DefaultMutableTreeNode
                optParent.ifPresent(y -> y.remove(node));

For streams I wrote

/**
 * Creates a function that tries to cast to the given class.
 * If the given argument is not an instance of the given class an empty stream is returned.
 * This function can be used in an <code>stream.flatMap(Functional.castStream(SomeClass.class))</code> context.
 * 
 * @param clazz
 *        given Clazz
 * @return Returns an optional of the given class.
 * @param <X>
 *        generic input argument for the function
 * @param <T>
 *        type of the output of the function
 */
public static <X, T> Function<X, Stream<T>> castStream(Class<? extends T> clazz) {
    return x -> Stream.of(x) // stream of x.
        .filter(clazz::isInstance) // check if instance
        .map(clazz::cast);
}
Related