@Override public boolean isDefinedAt(V v) { return !v.isEmpty(); }
/** * Returns the underlying value if present, otherwise {@code other}. * * @param other An alternative value. * @return A value of type {@code T} */ default T getOrElse(T other) { return isEmpty() ? other : get(); }
/** * Returns the underlying value if present, otherwise {@code null}. * * @return A value of type {@code T} or {@code null}. */ default T getOrNull() { return isEmpty() ? null : get(); }
/** * Returns the underlying value if present, otherwise {@code other}. * * @param supplier An alternative value supplier. * @return A value of type {@code T} * @throws NullPointerException if supplier is null */ default T getOrElse(Supplier<? extends T> supplier) { Objects.requireNonNull(supplier, "supplier is null"); return isEmpty() ? supplier.get() : get(); }
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 {@link Validation}. * * @param <U> value type of a {@code Valid} * @param valueSupplier A supplier of a {@code Valid} value * @return A new {@link Validation.Valid} containing the result of {@code valueSupplier} if this is empty, * otherwise a new {@link Validation.Invalid} containing this value. * @throws NullPointerException if {@code valueSupplier} is null * @deprecated Use {@link #toValidation(Supplier)} instead. */ @Deprecated default <U> Validation<T, U> toInvalid(Supplier<? extends U> valueSupplier) { Objects.requireNonNull(valueSupplier, "valueSupplier is null"); return isEmpty() ? Validation.valid(valueSupplier.get()) : Validation.invalid(get()); }
@Override default Spliterator<T> spliterator() { return Spliterators.spliterator(iterator(), isEmpty() ? 0 : 1, Spliterator.IMMUTABLE | Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED); }
/** * Converts this to a {@link Either}. * * @param <R> right type * @param right A supplier of a right value * @return A new {@link Either.Right} containing the result of {@code right} if this is empty, otherwise * a new {@link Either.Left} containing this value. * @throws NullPointerException if {@code right} is null * @deprecated Use {@link #toEither(Supplier)} instead. */ @Deprecated default <R> Either<T, R> toLeft(Supplier<? extends R> right) { Objects.requireNonNull(right, "right is null"); return isEmpty() ? Either.right(right.get()) : Either.left(get()); }
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 Validation}. * * @param <U> value type of a {@code Valid} * @param value An instance of a {@code Valid} value * @return A new {@link Validation.Valid} containing the given {@code value} if this is empty, otherwise * a new {@link Validation.Invalid} containing this value. * @deprecated Use {@link #toValidation(Object)} instead. */ @Deprecated default <U> Validation<T, U> toInvalid(U value) { return isEmpty() ? Validation.valid(value) : Validation.invalid(get()); }
/** * Returns the underlying value if present, otherwise returns the result of {@code Try.of(supplier).get()}. * * @param supplier An alternative value supplier. * @return A value of type {@code T}. * @throws NullPointerException if supplier is null */ default T getOrElseTry(CheckedFunction0<? extends T> supplier) { Objects.requireNonNull(supplier, "supplier is null"); return isEmpty() ? Try.of(supplier).get() : get(); }
/** * Converts this to a {@link Either}. * * @param <R> right type * @param right An instance of a right value * @return A new {@link Either.Right} containing the value of {@code right} if this is empty, otherwise * a new {@link Either.Left} containing this value. * @deprecated Use {@link #toEither(Object)} instead. */ @Deprecated default <R> Either<T, R> toLeft(R right) { return isEmpty() ? Either.right(right) : Either.left(get()); }
/** * Converts this to a {@link Either}. * * @param <L> left type * @param left An instance of a left value * @return A new {@link Either.Left} containing the value of {@code left} if this is empty, otherwise * a new {@link Either.Right} containing this value. * @deprecated Use {@link #toEither(Object)} instead. */ @Deprecated default <L> Either<L, T> toRight(L left) { return isEmpty() ? Either.left(left) : Either.right(get()); }
/** * Converts this to a {@link Validation}. * * @param <E> error type of an {@code Invalid} * @param error An error * @return A new {@link Validation.Invalid} containing the given {@code error} if this is empty, otherwise * a new {@link Validation.Valid} containing this value. * @deprecated Use {@link #toValidation(Object)} instead. */ @Deprecated default <E> Validation<E, T> toValid(E error) { return isEmpty() ? Validation.invalid(error) : Validation.valid(get()); }
/** * Converts this to an {@link Validation}. * * @param invalidSupplier A {@link Supplier} for the invalid value for the {@link Validation} * @param <E> Validation error component type * @return A new {@link Validation}. */ default <E> Validation<E, T> toValidation(Supplier<? extends E> invalidSupplier) { Objects.requireNonNull(invalidSupplier, "invalidSupplier is null"); if (this instanceof Validation) { return ((Validation<?, T>) this).mapError(ignored -> invalidSupplier.get()); } else { return isEmpty() ? Invalid(invalidSupplier.get()) : Valid(get()); } }
/** * Converts this to an {@link Option}. * * @return A new {@link Option}. */ default Option<T> toOption() { if (this instanceof Option) { return (Option<T>) this; } else { return isEmpty() ? Option.none() : Option.some(get()); } }
/** * Converts this to a {@link CharSeq}. * * @return A new {@link CharSeq}. */ default CharSeq toCharSeq() { if (this instanceof CharSeq) { return (CharSeq) this; } else if (isEmpty()) { return CharSeq.empty(); } else { return CharSeq.of(iterator().mkString()); } }
/** * Converts this to an {@link Either}. * * @param left A left value for the {@link Either} * @param <L> Either left component type * @return A new {@link Either}. */ default <L> Either<L, T> toEither(L left) { if (this instanceof Either) { return ((Either<?, T>) this).mapLeft(ignored -> left); } else { return isEmpty() ? Left(left) : Right(get()); } }
/** * Converts this to an {@link Validation}. * * @param invalid An invalid value for the {@link Validation} * @param <E> Validation error component type * @return A new {@link Validation}. */ default <E> Validation<E, T> toValidation(E invalid) { if (this instanceof Validation) { return ((Validation<?, T>) this).mapError(ignored -> invalid); } else { return isEmpty() ? Invalid(invalid) : Valid(get()); } }