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();
}

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 if it is assignable from the given class , otherwise returns an optional . empty ( ) .
