public static <T> @NonNull Optional<T> cast(final @NonNull Optional<?> optional, final @NonNull Class<T> type) {
    // not necessary to re-wrap, we can just cast
    return isInstance(optional, type) ? (Optional<T>) optional : Optional.empty();
  }
public static < T > @ NonNull Optional < T > cast ( final @ NonNull Optional < ? > optional , final @ NonNull Class < T > type ) { return isInstance ( optional , type ) ? ( Optional < T > ) optional : Optional . empty ( ) ; }
Casts {@code optional} to an optional of type {@code type} if the value held by {@code optional} is an instance of {@code type}

@param optional the optional
@param type the type
@param <T> the type
@return the optional