@Test(expected = IllegalArgumentException.class) public void cast_sample_usage() { Object objectToCast = "illegal"; int result = cast(Integer.class, objectToCast). filter(i -> !i.equals(0)). orElseThrow(() -> new IllegalArgumentException("Expected non-zero integer, got " + quote(objectToCast))); }
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);  }