/** * Creates a new {@code Option} of a given value. * * @param value A value * @param <T> type of the value * @return {@code Some(value)} if value is not {@code null}, {@code None} otherwise */ static <T> Option<T> of(T value) { return (value == null) ? none() : some(value); }
/** * Filters this right-biased {@code Either} by testing a predicate. * <p> * * @param predicate A predicate * @return a new {@code Option} instance * @throws NullPointerException if {@code predicate} is null */ default Option<Either<L, R>> filter(Predicate<? super R> predicate) { Objects.requireNonNull(predicate, "predicate is null"); return isLeft() || predicate.test(get()) ? Option.some(this) : Option.none(); }
@SuppressWarnings({ "unchecked", "varargs" }) @SafeVarargs public final <R> Option<R> option(Case<? extends T, ? extends R>... cases) { Objects.requireNonNull(cases, "cases is null"); for (Case<? extends T, ? extends R> _case : cases) { final Case<T, R> __case = (Case<T, R>) _case; if (__case.isDefinedAt(value)) { return Option.some(__case.apply(value)); } } return Option.none(); }
/** * Creates a {@code FutureImpl} that is immediately completed with the given value. No task will be started. * * @param executor An {@link Executor} to run and control the computation and to perform the actions. * @param value the result of this Future * @param <T> value type of the Future * @return a new {@code FutureImpl} instance */ static <T> FutureImpl<T> of(Executor executor, Try<? extends T> value) { return new FutureImpl<>(executor, Option.some(Try.narrow(value)), null, null, (complete, updateThread) -> {}); }
/** * Turns this sequence into a plain function returning an Option result. * * @return a function that takes an index i and returns the value of * this sequence in a Some if the index is within bounds, otherwise a None. * @deprecated Will be removed */ @Deprecated default Function1<Integer, Option<T>> lift() { return i -> (i >= 0 && i < length()) ? Option.some(apply(i)) : Option.none(); }
/** * Shortcut for {@code isEmpty() ? Option.none() : Option.some(reduceLeft(op))}. * * @param op A BiFunction of type T * @return a reduced value * @throws NullPointerException if {@code op} is null */ @Override default Option<T> reduceLeftOption(BiFunction<? super T, ? super T, ? extends T> op) { Objects.requireNonNull(op, "op is null"); return isEmpty() ? Option.none() : Option.some(reduceLeft(op)); }
/** * Shortcut for {@code isEmpty() ? Option.none() : Option.some(reduceRight(op))}. * * @param op An operation of type T * @return a reduced value * @throws NullPointerException if {@code op} is null */ @Override default Option<T> reduceRightOption(BiFunction<? super T, ? super T, ? extends T> op) { Objects.requireNonNull(op, "op is null"); return isEmpty() ? Option.none() : Option.some(reduceRight(op)); }
@SuppressWarnings("unchecked") @Override public Option<M> initOption() { return isEmpty() ? Option.none() : Option.some(init()); }
@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(); }
/** * Returns the head element without modifying the List. * * @return {@code None} if this List is empty, otherwise a {@code Some} containing the head element */ default Option<T> peekOption() { return isEmpty() ? Option.none() : Option.some(head()); }
@Override public Option<HashSet<T>> tailOption() { if (tree.isEmpty()) { return Option.none(); } else { return Option.some(tail()); } }
/** * Removes an element from this Queue. * * @return {@code None} if this Queue is empty, otherwise {@code Some} {@code Tuple} containing the first element and the remaining elements of this Queue */ public Option<Tuple2<T, Q>> dequeueOption() { return isEmpty() ? Option.none() : Option.some(dequeue()); }
/** * Dual of {@linkplain #headOption()}, returning the last element as {@code Option}. * * @return {@code Some(element)} or {@code None} if this is empty. */ default Option<T> lastOption() { return isEmpty() ? Option.none() : Option.some(last()); }
/** * Returns the minimum element of this tree according to the underlying comparator. * * @return Some element, if this is not empty, otherwise None */ default Option<T> min() { return isEmpty() ? Option.none() : Option.some(Node.minimum((Node<T>) this)); }
/** * Removes the head element from this List. * * @return {@code None} if this List is empty, otherwise a {@code Some} containing the elements of this List without the head element */ default Option<List<T>> popOption() { return isEmpty() ? Option.none() : Option.some(pop()); }
/** * 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()); } }
@Override default Option<Iterator<T>> tailOption() { if (hasNext()) { next(); return Option.some(this); } else { return Option.none(); } }
/** * Returns the first element of a non-empty Traversable as {@code Option}. * * @return {@code Some(element)} or {@code None} if this is empty. */ default Option<T> headOption() { return isEmpty() ? Option.none() : Option.some(head()); }
/** * Returns the first element without modifying the Queue. * * @return {@code None} if this Queue is empty, otherwise a {@code Some} containing the first element */ public Option<T> peekOption() { return isEmpty() ? Option.none() : Option.some(peek()); }
/** * Removes the head element from this List. * * @return {@code None} if this List is empty, otherwise {@code Some} {@code Tuple} containing the head element and the remaining elements of this List */ default Option<Tuple2<T, List<T>>> pop2Option() { return isEmpty() ? Option.none() : Option.some(Tuple.of(head(), pop())); }