@SuppressWarnings("unchecked") @Test void getOnceIfSuccessful() { ThrowingSupplier<String,RuntimeException> supplier = Mockito.mock(ThrowingSupplier.class); when(supplier.get()).thenReturn("Listen very carefully, I shall say this only once."); Lazy<String> sut = new Lazy<>(supplier); String result1 = sut.get(); String result2 = sut.get(); assertThat(result1, is("Listen very carefully, I shall say this only once.")); assertThat(result2, is("Listen very carefully, I shall say this only once.")); verify(supplier, times(1)).get(); }
public T get() { return tryGet().orElseThrow(); }
Lazy { public T get() { return tryGet().orElseThrow(); } }
Lazy { public T get() { return tryGet().orElseThrow(); } <E extends Exception> Lazy(ThrowingSupplier<T,E> supplier); }
Lazy { public T get() { return tryGet().orElseThrow(); } <E extends Exception> Lazy(ThrowingSupplier<T,E> supplier); Try<T> tryGet(); T get(); Optional<T> optional(); Stream<T> stream(); boolean isKnown(); boolean isKnownSuccess(); boolean isKnownFailure(); Lazy<U> map(ThrowingFunction<? super T, ? extends U, E> function); Lazy<U> flatMap(ThrowingFunction<? super T, Lazy<U>, E> function); }
Lazy { public T get() { return tryGet().orElseThrow(); } <E extends Exception> Lazy(ThrowingSupplier<T,E> supplier); Try<T> tryGet(); T get(); Optional<T> optional(); Stream<T> stream(); boolean isKnown(); boolean isKnownSuccess(); boolean isKnownFailure(); Lazy<U> map(ThrowingFunction<? super T, ? extends U, E> function); Lazy<U> flatMap(ThrowingFunction<? super T, Lazy<U>, E> function);  }