@Override public Stream<T> tail() { return tail.get(); }
@Override public Lazy<T> peek(Consumer<? super T> action) { action.accept(get()); return this; }
@Override public int hashCode() { return Objects.hashCode(get()); }
public static void scheduleAutoTransition(Runnable transition, Duration waitDurationInOpenState) { executorService.get().schedule( transition, waitDurationInOpenState.toMillis(), TimeUnit.MILLISECONDS); } }
/** * Ensures that the value is evaluated before serialization. * * @param s An object serialization stream. * @throws java.io.IOException If an error occurs writing to the stream. */ @GwtIncompatible("The Java serialization protocol is explicitly not supported") private void writeObject(ObjectOutputStream s) throws IOException { get(); // evaluates the lazy value if it isn't evaluated yet! s.defaultWriteObject(); } }
@Override public Iterator<T> iterator() { return Iterator.of(get()); }
@Override public <U> Lazy<U> map(Function<? super T, ? extends U> mapper) { return Lazy.of(() -> mapper.apply(get())); }
@GwtIncompatible("Math::nextUp is not implemented") static BigDecimal asDecimal(double number) { if (number == NEGATIVE_INFINITY) { final BigDecimal result = BigDecimal.valueOf(Math.nextUp(NEGATIVE_INFINITY)); return result.subtract(INFINITY_DISTANCE.get()); } else if (number == POSITIVE_INFINITY) { final BigDecimal result = BigDecimal.valueOf(Math.nextDown(POSITIVE_INFINITY)); return result.add(INFINITY_DISTANCE.get()); } else { return BigDecimal.valueOf(number); } } }
public Option<T> filter(Predicate<? super T> predicate) { final T v = get(); return predicate.test(v) ? Option.some(v) : Option.none(); }
/** * 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(); } }; } }
@Override public Stream<T> tail() { final Stream<T> t = tail.get(); if (t.isEmpty()) { return Stream.ofAll(queue); } else { if (t instanceof ConsImpl) { final ConsImpl<T> c = (ConsImpl<T>) t; return new AppendElements<>(c.head(), queue, c.tail); } else { final AppendElements<T> a = (AppendElements<T>) t; return new AppendElements<>(a.head(), a.queue.appendAll(queue), a.tail); } } }
@Override public Lazy<T> peek(Consumer<? super T> action) { action.accept(get()); return this; }
public void cancel() { ensureNotOver(); blocksUntilAllUnblock.get().cancel(true); isOver.set(true); LOGGER.debug("Cancelled."); }
public void cancel() { ensureNotOver(); blocksUntilAllUnblock.get().cancel(true); isOver.set(true); LOGGER.debug("Cancelled."); }
public static Optional<PurchaseStrategy> toPurchase(final String strategy) { LOGGER.debug("Reading purchasing strategy."); return load(strategy, LOADER.get(), StrategyService::toPurchase); } }
void ensureLiveness() { if (!lifecycle.get().waitUntilOnline()) { exit(ReturnCode.ERROR_DOWN); } }
@Test public void testLazy() throws Exception { Lazy<A> src = Lazy.of(() -> new B("a", "b")); String json = MAPPER.writeValueAsString(new LazyPojo().setValue(src)); Assert.assertEquals(json, "{\"value\":{\"ExtFieldsPojoTest$B\":{\"a\":\"a\",\"b\":\"b\"}}}"); LazyPojo pojo = MAPPER.readValue(json, LazyPojo.class); Lazy<A> restored = pojo.getValue(); Assert.assertTrue(restored.get() instanceof B); Assert.assertEquals(restored.get().a, "a"); Assert.assertEquals(((B) restored.get()).b, "b"); }