OptionalUtils { public static <T> void ifPresentOrElse(Optional<T> value, Consumer<T> action, Runnable emptyAction) { if (value.isPresent()) { action.accept(value.get()); } else { emptyAction.run(); } } private  OptionalUtils(); @SuppressWarnings("unchecked") static Optional<T> or(Optional<T> value, Supplier<Optional<? extends T>> supplier); static Stream<T> stream(Optional<T> value); static void ifPresentOrElse(Optional<T> value, Consumer<T> action, Runnable emptyAction);  }
@Test public void invokesPresentAction() { OptionalUtils.ifPresentOrElse(Optional.of("Kevin"), (i) -> {}, () -> new RuntimeException("Failure")); }
@Test public void invokesEmptyAction() { OptionalUtils.ifPresentOrElse(Optional.empty(), (i) -> new RuntimeException("Failure"), () -> {}); }