@Override public T getNext() { final T result = next.get(); next = Option.none(); return result; } };
/** * 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); }
/** * 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()); }
/** * 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(); }
/** * 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()); }
/** * 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 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()); }
@SuppressWarnings("unchecked") @Override Option<V> lookup(int shift, int keyHash, K key) { final int frag = hashFragment(shift, keyHash); final int bit = toBitmap(frag); if ((bitmap & bit) != 0) { final AbstractNode<K, V> n = (AbstractNode<K, V>) subNodes[fromBitmap(bitmap, bit)]; return n.lookup(shift + SIZE, keyHash, key); } else { return Option.none(); } }