@Test public void invalid_cast_gives_empty_optional() { Object objectToCast = "string"; Optional<Integer> value = cast(Integer.class, objectToCast); assertTrue("Value is present", !value.isPresent()); }
public static <T> Optional<T> cast(Class<T> targetClass, Object instance) { return targetClass.isInstance(instance)? Optional.of(targetClass.cast(instance)): Optional.empty(); }
Casting { public static <T> Optional<T> cast(Class<T> targetClass, Object instance) { return targetClass.isInstance(instance)? Optional.of(targetClass.cast(instance)): Optional.empty(); } }
Casting { public static <T> Optional<T> cast(Class<T> targetClass, Object instance) { return targetClass.isInstance(instance)? Optional.of(targetClass.cast(instance)): Optional.empty(); }  }
Casting { public static <T> Optional<T> cast(Class<T> targetClass, Object instance) { return targetClass.isInstance(instance)? Optional.of(targetClass.cast(instance)): Optional.empty(); }  static Optional<T> cast(Class<T> targetClass, Object instance); }
Casting { public static <T> Optional<T> cast(Class<T> targetClass, Object instance) { return targetClass.isInstance(instance)? Optional.of(targetClass.cast(instance)): Optional.empty(); }  static Optional<T> cast(Class<T> targetClass, Object instance);  }