/** * Returns true, if this is {@code Some}, otherwise false, if this is {@code None}. * <p> * Please note that it is possible to create {@code new Some(null)}, which is defined. * * @return true, if this {@code Option} has a defined value, false otherwise */ default boolean isDefined() { return !isEmpty(); }
/** * Returns this {@code Option} if it is nonempty, otherwise return the alternative. * * @param other An alternative {@code Option} * @return this {@code Option} if it is nonempty, otherwise return the alternative. */ @SuppressWarnings("unchecked") default Option<T> orElse(Option<? extends T> other) { Objects.requireNonNull(other, "other is null"); return isEmpty() ? (Option<T>) other : this; }
/** * Returns this {@code Option} if it is nonempty, otherwise return the result of evaluating supplier. * * @param supplier An alternative {@code Option} supplier * @return this {@code Option} if it is nonempty, otherwise return the result of evaluating supplier. */ @SuppressWarnings("unchecked") default Option<T> orElse(Supplier<? extends Option<? extends T>> supplier) { Objects.requireNonNull(supplier, "supplier is null"); return isEmpty() ? (Option<T>) supplier.get() : this; }
/** * Runs a Java Runnable passed as parameter if this {@code Option} is empty. * * @param action a given Runnable to be run * @return this {@code Option} */ default Option<T> onEmpty(Runnable action) { Objects.requireNonNull(action, "action is null"); if (isEmpty()) { action.run(); } return this; }
/** * Returns the value if this is a {@code Some} or the {@code other} value if this is a {@code None}. * <p> * Please note, that the other value is eagerly evaluated. * * @param other An alternative value * @return This value, if this Option is defined or the {@code other} value, if this Option is empty. */ @Override default T getOrElse(T other) { return isEmpty() ? other : get(); }
/** * Returns the value if this is a {@code Some}, otherwise the {@code other} value is returned, * if this is a {@code None}. * <p> * Please note, that the other value is lazily evaluated. * * @param supplier An alternative value supplier * @return This value, if this Option is defined or the {@code other} value, if this Option is empty. */ @Override default T getOrElse(Supplier<? extends T> supplier) { Objects.requireNonNull(supplier, "supplier is null"); return isEmpty() ? supplier.get() : get(); }
/** * Returns the value if this is a {@code Some}, otherwise throws an exception. * * @param exceptionSupplier An exception supplier * @param <X> A throwable * @return This value, if this Option is defined, otherwise throws X * @throws X a throwable */ @Override default <X extends Throwable> T getOrElseThrow(Supplier<X> exceptionSupplier) throws X { Objects.requireNonNull(exceptionSupplier, "exceptionSupplier is null"); if (isEmpty()) { throw exceptionSupplier.get(); } else { return get(); } }
/** * Returns {@code Some(value)} if this is a {@code Some} and the value satisfies the given predicate. * Otherwise {@code None} is returned. * * @param predicate A predicate which is used to test an optional value * @return {@code Some(value)} or {@code None} as specified */ default Option<T> filter(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); return isEmpty() || predicate.test(get()) ? this : none(); }
/** * Maps the value to a new {@code Option} if this is a {@code Some}, otherwise returns {@code None}. * * @param mapper A mapper * @param <U> Component type of the resulting Option * @return a new {@code Option} */ @SuppressWarnings("unchecked") default <U> Option<U> flatMap(Function<? super T, ? extends Option<? extends U>> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return isEmpty() ? none() : (Option<U>) mapper.apply(get()); }
/** * Maps the value and wraps it in a new {@code Some} if this is a {@code Some}, returns {@code None}. * * @param mapper A value mapper * @param <U> The new value type * @return a new {@code Some} containing the mapped value if this Option is defined, otherwise {@code None}, if this is empty. */ @Override default <U> Option<U> map(Function<? super T, ? extends U> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return isEmpty() ? none() : some(mapper.apply(get())); }
@SuppressWarnings("unchecked") static <K, V, U extends V, M extends Map<K, V>> M put(M map, K key, U value, BiFunction<? super V, ? super U, ? extends V> merge) { Objects.requireNonNull(merge, "the merge function is null"); final Option<V> currentValue = map.get(key); if (currentValue.isEmpty()) { return (M) map.put(key, value); } else { return (M) map.put(key, merge.apply(currentValue.get(), value)); } }
static <K, V, U extends V, M extends Map<K, V>> M put(M map, Tuple2<? extends K, U> entry, BiFunction<? super V, ? super U, ? extends V> merge) { Objects.requireNonNull(merge, "the merge function is null"); final Option<V> currentValue = map.get(entry._1); if (currentValue.isEmpty()) { return put(map, entry); } else { return put(map, entry.map2(value -> merge.apply(currentValue.get(), value))); } }
@Override public boolean hasNext() { while (next.isEmpty() && that.hasNext()) { final T candidate = that.next(); if (predicate.test(candidate)) { next = Option.some(candidate); } } return next.isDefined(); }
@Override public String toString() { final Option<Try<T>> value = this.value; final String s = (value == null || value.isEmpty()) ? "?" : value.get().toString(); return stringPrefix() + "(" + s + ")"; }
@Override default Iterator<T> iterator() { return isEmpty() ? Iterator.empty() : Iterator.of(get()); }
/** * Reduces many {@code Option}s into a single {@code Option} by transforming an * {@code Iterable<Option<? extends T>>} into a {@code Option<Seq<T>>}. If any of * the Options are {@link Option.None}, then this returns {@link Option.None}. * * @param values An {@code Iterable} of {@code Option}s * @param <T> type of the Options * @return An {@code Option} of a {@link Seq} of results * @throws NullPointerException if {@code values} is null */ static <T> Option<Seq<T>> sequence(Iterable<? extends Option<? extends T>> values) { Objects.requireNonNull(values, "values is null"); Vector<T> vector = Vector.empty(); for (Option<? extends T> value : values) { if (value.isEmpty()) { return Option.none(); } vector = vector.append(value.get()); } return Option.some(vector); }
/** * Maps the cause to a new exception if this is a {@code Failure} or returns this instance if this is a {@code Success}. * <p> * If none of the given cases matches the cause, the same {@code Failure} is returned. * * @param cases A not necessarily exhaustive sequence of cases that will be matched against a cause. * @return A new {@code Try} if this is a {@code Failure}, otherwise this. */ @GwtIncompatible @SuppressWarnings({ "unchecked", "varargs" }) default Try<T> mapFailure(Match.Case<? extends Throwable, ? extends Throwable>... cases) { if (isSuccess()) { return this; } else { final Option<Throwable> x = Match(getCause()).option(cases); return x.isEmpty() ? this : failure(x.get()); } }
/** * Matches each element with a unique key that you extract from it. * If the same key is present twice, the function will return {@code None}. * * @param getKey A function which extracts a key from elements * @param <K> key class type * @return A Map containing the elements arranged by their keys. * @throws NullPointerException if {@code getKey} is null. * @see #groupBy(Function) */ default <K> Option<Map<K, T>> arrangeBy(Function<? super T, ? extends K> getKey) { return Option.of(groupBy(getKey).mapValues(Traversable<T>::singleOption)) .filter(map -> !map.exists(kv -> kv._2.isEmpty())) .map(map -> Map.narrow(map.mapValues(Option::get))); }
assertThat(exceptionBuffer.take().get()).isInstanceOf(IOException.class); assertThat(exceptionBuffer.take().get()).isInstanceOf(IOException.class); assertThat(exceptionBuffer.take().isEmpty()).isTrue();
/** * Returns this {@code Option} if it is nonempty, otherwise return the alternative. * * @param other An alternative {@code Option} * @return this {@code Option} if it is nonempty, otherwise return the alternative. */ @SuppressWarnings("unchecked") default Option<T> orElse(Option<? extends T> other) { Objects.requireNonNull(other, "other is null"); return isEmpty() ? (Option<T>) other : this; }