public static <T> T castOrFail(Object object, Class<T> clazz) {
        requireNonNull(clazz);
        if (object == null) {
            throw new NoSuchElementException("null is not an instance of " + clazz.getName());
        }
        return Optional.of(object)
            .filter(o -> clazz.isAssignableFrom(o.getClass()))
            .map(clazz::cast)
            .orElseThrow(NoSuchElementException::new);
    }
Casts and returns the provided object to the provided class.

@param <T> the type to return
@param object to cast
@param clazz to cast to
@return the casted element
@throws NoSuchElementException if the object could not be casted to the
provided class
runtime-parent/runtime-core/src/main/java/com/speedment/runtime/core/internal/util/Cast.java