/** * Alias for {@link Lazy#of(Supplier)} * * @param <T> type of the lazy value * @param supplier A supplier * @return A new instance of {@link Lazy} */ public static <T> Lazy<T> Lazy(Supplier<? extends T> supplier) { return Lazy.of(supplier); }
Cons(T head, Supplier<Stream<T>> tail) { Objects.requireNonNull(tail, "tail is null"); this.head = head; this.tail = Lazy.of(tail); }
@Override public <U> Lazy<U> map(Function<? super T, ? extends U> mapper) { return Lazy.of(() -> mapper.apply(get())); }
/** * Returns a memoizing version of this function, which computes the return value for given arguments only one time. * On subsequent calls given the same arguments the memoized value is returned. * <p> * Please note that memoizing functions do not permit {@code null} as single argument or return value. * * @return a memoizing function equivalent to this. */ default Function0<R> memoized() { if (isMemoized()) { return this; } else { return (Function0<R> & Memoized) Lazy.of(this)::get; } }
/** * Creates a real _lazy value_ of type {@code T}, backed by a {@linkplain java.lang.reflect.Proxy} which delegates * to a {@code Lazy} instance. * * @param supplier A supplier * @param type An interface * @param <T> type of the lazy value * @return A new instance of T */ @GwtIncompatible("reflection is not supported") @SuppressWarnings("unchecked") public static <T> T val(Supplier<? extends T> supplier, Class<T> type) { Objects.requireNonNull(supplier, "supplier is null"); Objects.requireNonNull(type, "type is null"); if (!type.isInterface()) { throw new IllegalArgumentException("type has to be an interface"); } final Lazy<T> lazy = Lazy.of(supplier); final InvocationHandler handler = (proxy, method, args) -> method.invoke(lazy.get(), args); return (T) Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[] { type }, handler); }
/** * Reduces many {@code Lazy} values into a single {@code Lazy} by transforming an * {@code Iterable<Lazy<? extends T>>} into a {@code Lazy<Seq<T>>}. * * @param <T> Type of the lazy values. * @param values An iterable of lazy values. * @return A lazy sequence of values. * @throws NullPointerException if values is null */ @SuppressWarnings("Convert2MethodRef") // TODO should be fixed in JDK 9 and Idea public static <T> Lazy<Seq<T>> sequence(Iterable<? extends Lazy<? extends T>> values) { Objects.requireNonNull(values, "values is null"); return Lazy.of(() -> Vector.ofAll(values).map(lazy -> lazy.get())); }
/** * Returns a memoizing version of this function, which computes the return value for given arguments only one time. * On subsequent calls given the same arguments the memoized value is returned. * <p> * Please note that memoizing functions do not permit {@code null} as single argument or return value. * * @return a memoizing function equivalent to this. */ default CheckedFunction0<R> memoized() { if (isMemoized()) { return this; } else { final Lazy<R> lazy = Lazy.of(() -> { try { return apply(); } catch (Throwable x) { throw new RuntimeException(x); } }); return (CheckedFunction0<R> & Memoized) () -> { try { return lazy.get(); } catch(RuntimeException x) { throw x.getCause(); } }; } }
@Test public void testGetBadLazyArgumentShouldThrow() { Lazy<Object> badEvaluatingLazy = Lazy.of(() -> { throw new TestSpecificException(); }); assertThatThrownBy(() -> unit.build(new GenericType<Lazy<Integer>>() {}.getType(), badEvaluatingLazy, null)) .isInstanceOf(TestSpecificException.class); }
@Test public void testGetLazyArgumentInferredShouldNotBeEmpty() { Optional<Argument> arg = unit.build(new GenericType<Lazy<?>>() {}.getType(), Lazy.of(() -> 1), null); assertThat(arg).isNotEmpty(); }
@Test public void testGetLazyArgumentShouldNotBeEmpty() { Optional<Argument> arg = unit.build(new GenericType<Lazy<Integer>>() {}.getType(), Lazy.of(() -> 1), null); assertThat(arg).isNotEmpty(); }
@Test public void testGetLazyShouldReturnCorrectRow() { Something result = dbRule.getSharedHandle().createQuery(SELECT_BY_NAME) .bind("name", Lazy.of(() -> "brian")) .mapToBean(Something.class) .findOnly(); assertThat(result).isEqualTo(BRIANSOMETHING); }
/** * * @param participation * @param related Provided as a Supplier in order to allow the calling code to retrieve the (likely remote) entity * on-demand. */ public ParticipationDescriptor(final Participation participation, final Supplier<Loan> related) { this.participation = participation; this.related = Lazy.of(related); }
/** * * @param investment * @param related Provided as a Supplier in order to allow the calling code to retrieve the (likely remote) entity * on-demand. */ public InvestmentDescriptor(final Investment investment, final Supplier<Loan> related) { this.investment = investment; this.related = Lazy.of(related); }
/** * * @param participation * @param related Provided as a Supplier in order to allow the calling code to retrieve the (likely remote) entity * on-demand. */ public ParticipationDescriptor(final Participation participation, final Supplier<Loan> related) { this.participation = participation; this.related = Lazy.of(related); }
/** * @param serviceClass * @param <T> * @return This is lazy initialized, since if we place the result in a static final variable, and the service * loader initialization throws an exception, the parent class will fail to load and we will get a * NoClassDefFoundError which gives us no information as to why it happened. */ public <T> Lazy<ServiceLoader<T>> getServiceLoader(final Class<T> serviceClass) { return Lazy.of(() -> getServiceLoader(serviceClass, "extensions")); }
GoogleCredentialsFeature(final String username, final HttpTransport transport, final CredentialProvider credentialProvider) { this.username = username; this.transport = transport; this.credentialProvider = Lazy.of(() -> credentialProvider == null ? CredentialProvider.live(transport, host, port) : credentialProvider); }
GoogleCredentialsFeature(final String username, final HttpTransport transport, final CredentialProvider credentialProvider) { this.username = username; this.transport = transport; this.credentialProvider = Lazy.of(() -> credentialProvider == null ? CredentialProvider.live(transport, host, port) : credentialProvider); }
public LazyEventImpl(final Class<T> eventType, final Supplier<T> eventSupplier) { this.eventType = eventType; final OffsetDateTime conceivedOn = DateUtil.offsetNow(); this.supplier = Lazy.of(() -> { LOGGER.trace("Instantiating {}.", eventType); final T result = eventSupplier.get(); ((AbstractEventImpl)result).setConceivedOn(conceivedOn); // make the event aware of when it was requested LOGGER.trace("Instantiated to {}.", result); return result; }); }
@Test public void test2() throws IOException { Lazy<?> src = Lazy.of(() -> null); String json = mapper().writer().writeValueAsString(src); Assert.assertEquals("null", json); Lazy<?> restored = mapper().readValue(json, Lazy.class); Assert.assertEquals(src, restored); }
@Test public void testLazyOfString() throws Exception { String src0 = "A"; Lazy<String> src = Lazy.of(() -> src0); String json = MAPPER.writeValueAsString(new ParameterizedLazyPojo<>(src)); Assert.assertEquals(json, "{\"value\":\"A\"}"); ParameterizedLazyPojo<java.lang.String> restored = MAPPER.readValue(json, new TypeReference<ParameterizedLazyPojo<java.lang.String>>(){}); Assert.assertEquals(src, restored.getValue()); }