class Cast {

    private Cast() {}

    /**
     * Casts and returns the provided object if it is assignable from the given
     * class, otherwise returns an Optional.empty().
     *
     * @param <T> the type to return
     * @param object to cast
     * @param clazz to cast to
     * @return An Optional of the casted element or Optional.empty()
     */
    public static <T> Optional<T> cast(Object object, Class<T> clazz) {
        requireNonNull(clazz);
        if (object == null) {
            return Optional.empty();
        }
        if (clazz.isAssignableFrom(object.getClass())) {
            final T result = clazz.cast(object);
            return Optional.of(result);
        }
        return Optional.empty();
    }

    /**
     * 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
     */
    public static <T> T castOrFail(Object object, Class<T> clazz) {<FILL_FUNCTION_BODY>}

}

        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);
    
<no_super_class>