@Override public Stream<T> tail() { return tail.get(); }
public Supplier<List<String>> createLazyIdSupplier(Supplier<List<String>> idSupplier) { return Lazy.val(() -> { List<String> ids = idSupplier.get(); return (Supplier) () -> ids; }, Supplier.class); }
/** * 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); }
/** * Creates a {@code Lazy} that requests its value from a given {@code Supplier}. The supplier is asked only once, * the value is memoized. * * @param <T> type of the lazy value * @param supplier A supplier * @return A new instance of Lazy */ @SuppressWarnings("unchecked") public static <T> Lazy<T> of(Supplier<? extends T> supplier) { Objects.requireNonNull(supplier, "supplier is null"); if (supplier instanceof Lazy) { return (Lazy<T>) supplier; } else { return new Lazy<>(supplier); } }
Cons(T head, Supplier<Stream<T>> tail) { Objects.requireNonNull(tail, "tail is null"); this.head = head; this.tail = Lazy.of(tail); }
/** * Creates a {@code Lazy} that requests its value from a given {@code Supplier}. The supplier is asked only once, * the value is memoized. * * @param <T> type of the lazy value * @param supplier A supplier * @return A new instance of Lazy */ @SuppressWarnings("unchecked") public static <T> Lazy<T> of(Supplier<? extends T> supplier) { Objects.requireNonNull(supplier, "supplier is null"); if (supplier instanceof Lazy) { return (Lazy<T>) supplier; } else { return new Lazy<>(supplier); } }
/** * 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 Function0<R> memoized() { if (isMemoized()) { return this; } else { return (Function0<R> & Memoized) Lazy.of(this)::get; } }
private CandidateDimension parse(DimensionContext context, JSONArray jsonConfig) { List<CandidateDimensionParserStrategy.StrategyConfig> configList = jsonConfig.stream() .filter(json -> json instanceof JSONObject) .map(JSONObject.class::cast) .filter(json -> json.get("dimension") != null && CollectionUtils.isNotEmpty(json.getJSONArray("idList"))) .map(json -> { CandidateDimensionParserStrategy.StrategyConfig config = json.toJavaObject(CandidateDimensionParserStrategy.StrategyConfig.class); if (config.getConfig() == null) { config.setConfig(json); } return config; }).collect(Collectors.toList()); if (configList.isEmpty()) { return CandidateDimension.empty; } return Lazy.val(() -> { List<String> list = configList.stream() .flatMap(config -> strategies .stream() .filter(strategy -> strategy.support(config.getDimension())) .map(strategy -> strategy.parse(context, config)) .filter(CollectionUtils::isNotEmpty) .flatMap(Collection::stream) .filter(StringUtils::hasText) ).collect(Collectors.toList()); return (CandidateDimension) () -> list; }, CandidateDimension.class); }
@Override public Lazy<T> peek(Consumer<? super T> action) { action.accept(get()); return this; }
/** * 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); }
@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); }
@Override @SuppressWarnings("all") public List<CandidateInfo> getCandidateInfo(Task task) { return Lazy.val(() -> { DimensionContext context = new DimensionContext(); context.setCreatorId(doingUser); context.setActivityId(activityId); context.setProcessDefineId(processDefineId); context.setTask(task); CandidateDimension dimension = candidateDimensionParser .parse(context, configEntity.getCandidateDimension()); return dimension.getCandidateUserIdList() .stream() .distinct() .map(userId -> Lazy.val(() -> new CandidateInfo() { @Override public Authentication user() { return AuthenticationHolder.get(userId); } @Override public PersonnelAuthentication person() { return PersonnelAuthenticationHolder.getByUserId(userId); } }, CandidateInfo.class)) .collect(Collectors.toList()); }, List.class); }
@Override public int hashCode() { return Objects.hashCode(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 testGetLazyArgumentInferredShouldNotBeEmpty() { Optional<Argument> arg = unit.build(new GenericType<Lazy<?>>() {}.getType(), Lazy.of(() -> 1), null); assertThat(arg).isNotEmpty(); }
@Override public Collector<?, ?, ?> build(Type containerType) { Class<?> erasedType = getCollectionType(containerType); return collectors.getOrElse(erasedType, Lazy.val(() -> resolveDefaultCollector(erasedType).get(), Collector.class)); } }
public static void scheduleAutoTransition(Runnable transition, Duration waitDurationInOpenState) { executorService.get().schedule( transition, waitDurationInOpenState.toMillis(), TimeUnit.MILLISECONDS); } }