/** * Converts this to a Java array having an accurate component type. * * <pre>{@code * // = [] of type String[] * Future.<String> of(() -> { throw new Error(); }) * .toJavaArray(String[]::new) * * // = [ok] of type String[] * Try.of(() -> "ok") * .toJavaArray(String[]::new) * * // = [1, 2, 3] of type Integer[] * List.of(1, 2, 3) * .toJavaArray(Integer[]::new) * }</pre> * * @param arrayFactory an <code>int</code> argument function that * creates an array of the correct component * type with the specified size * @return The array provided by the factory filled with the values from this <code>Value</code>. * @throws NullPointerException if componentType is null */ default T[] toJavaArray(IntFunction<T[]> arrayFactory) { java.util.List<T> javaList = toJavaList(); return javaList.toArray(arrayFactory.apply(javaList.size())); }
} else if (o instanceof Value) { final Value<?> that = (Value<?>) o; return this.iterator().corresponds(that.iterator(), (o1, o2) -> { if (o1 instanceof Value) { return ((Value<?>) o1).eq(o2); } else if (o2 instanceof Value) { return ((Value<?>) o2).eq(o1); } else { return Objects.equals(o1, o2); } else if (o instanceof Iterable) { final Value<?> that = Iterator.ofAll((Iterable<?>) o); return this.eq(that); } else { return false;
static <T, R extends Traversable<T>> R toTraversable( Value<T> value, R empty, Function<T, R> ofElement, Function<Iterable<T>, R> ofAll) { if (value.isEmpty()) { return empty; } else if (value.isSingleValued()) { return ofElement.apply(value.get()); } else { return ofAll.apply(value); } }
/** * Converts this to a {@link Try}. * <p> * If this value is undefined, i.e. empty, then a new {@code Failure(ifEmpty.get())} is returned, * otherwise a new {@code Success(value)} is returned. * * @param ifEmpty an exception supplier * @return A new {@link Try}. */ default Try<T> toTry(Supplier<? extends Throwable> ifEmpty) { Objects.requireNonNull(ifEmpty, "ifEmpty is null"); return isEmpty() ? Try.failure(ifEmpty.get()) : toTry(); }
/** * Converts this to a Java array with component type {@code Object} * * <pre>{@code * // = [] of type Object[] * Future.<String> of(() -> { throw new Error(); }) * .toJavaArray() * * // = [ok] of type Object[] * Try.of(() -> "ok") * .toJavaArray() * * // = [1, 2, 3] of type Object[] * List.of(1, 2, 3) * .toJavaArray() * }</pre> * * @return A new Java array. */ default Object[] toJavaArray() { if ((this instanceof Traversable<?>) && ((Traversable<?>) this).isTraversableAgain()) { final Object[] results = new Object[((Traversable<T>) this).size()]; final Iterator<T> iter = iterator(); Arrays.setAll(results, i -> iter.next()); return results; } else { return toJavaList().toArray(); } }
@Override public boolean isDefinedAt(V v) { return !v.isEmpty(); }
static <T, R extends java.util.Collection<T>> R toJavaCollection( Value<T> value, Function<Integer, R> containerSupplier, int defaultInitialCapacity) { final int size; if (value instanceof Traversable && ((Traversable) value).isTraversableAgain() && !value.isLazy()) { size = ((Traversable) value).size(); } else { size = defaultInitialCapacity; } final R container = containerSupplier.apply(size); value.forEach(container::add); return container; } }
/** * Shortcut for {@code exists(e -> Objects.equals(e, element))}, tests if the given {@code element} is contained. * * @param element An Object of type A, may be null. * @return true, if element is contained, false otherwise. */ default boolean contains(T element) { return exists(e -> Objects.equals(e, element)); }
@Override public T apply(V v) { return v.get(); }
private Optional<Argument> buildValueArgument(Type type, ConfigRegistry config, Value<?> value) { Type nestedType = findGenericParameter(type, Value.class).orElseGet(() -> extractTypeOfValue(value)); Object nestedValue = value.getOrNull(); return resolveNestedFromConfigured(config, nestedType, nestedValue); }
static <T, K, V, E extends Tuple2<? extends K, ? extends V>, R extends Map<K, V>> R toMap( Value<T> value, R empty, Function<E, R> ofElement, Function<Iterable<E>, R> ofAll, Function<? super T, ? extends E> f) { if (value.isEmpty()) { return empty; } else if (value.isSingleValued()) { return ofElement.apply(f.apply(value.get())); } else { return ofAll.apply(Iterator.ofAll(value).map(f)); } }
/** * Converts this to a {@link Try}. * <p> * If this value is undefined, i.e. empty, then a new {@code Failure(ifEmpty.get())} is returned, * otherwise a new {@code Success(value)} is returned. * * @param ifEmpty an exception supplier * @return A new {@link Try}. */ default Try<T> toTry(Supplier<? extends Throwable> ifEmpty) { Objects.requireNonNull(ifEmpty, "ifEmpty is null"); return isEmpty() ? Try.failure(ifEmpty.get()) : toTry(); }
/** * Converts this to a Java array with component type {@code Object} * * <pre>{@code * // = [] of type Object[] * Future.<String> of(() -> { throw new Error(); }) * .toJavaArray() * * // = [ok] of type Object[] * Try.of(() -> "ok") * .toJavaArray() * * // = [1, 2, 3] of type Object[] * List.of(1, 2, 3) * .toJavaArray() * }</pre> * * @return A new Java array. */ default Object[] toJavaArray() { if ((this instanceof Traversable<?>) && ((Traversable<?>) this).isTraversableAgain()) { final Object[] results = new Object[((Traversable<T>) this).size()]; final Iterator<T> iter = iterator(); Arrays.setAll(results, i -> iter.next()); return results; } else { return toJavaList().toArray(); } }
@Override public boolean isEmpty(SerializerProvider provider, T value) { return value.isEmpty(); } }
static <T, R extends java.util.Collection<T>> R toJavaCollection( Value<T> value, Function<Integer, R> containerSupplier, int defaultInitialCapacity) { final int size; if (value instanceof Traversable && ((Traversable) value).isTraversableAgain() && !value.isLazy()) { size = ((Traversable) value).size(); } else { size = defaultInitialCapacity; } final R container = containerSupplier.apply(size); value.forEach(container::add); return container; } }
/** * Checks, if the given predicate holds for all elements. * * @param predicate A Predicate * @return true, if the predicate holds for all elements, false otherwise * @throws NullPointerException if {@code predicate} is null */ default boolean forAll(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); return !exists(predicate.negate()); }