Cast the content of optional or streams

michaeak :

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?

Didier L :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=103065&siteId=1