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