@Override public T getNext() { final T next = nextOption.get(); nextOption = null; return next; } };
/** * 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(); }
@Override public T head() { if (isEmpty()) { throw new NoSuchElementException("head of empty TreeSet"); } else { return tree.min().get(); } }
@Override public T last() { if (isEmpty()) { throw new NoSuchElementException("last of empty TreeSet"); } else { return tree.max().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)); } }
@Override public TreeMap<K, V> init() { if (isEmpty()) { throw new UnsupportedOperationException("init of empty TreeMap"); } else { final Tuple2<K, V> max = entries.max().get(); return new TreeMap<>(entries.delete(max)); } }
@SuppressWarnings("unchecked") static <K, V, M extends Map<K, V>> Tuple2<V, M> computeIfAbsent(M map, K key, Function<? super K, ? extends V> mappingFunction) { Objects.requireNonNull(mappingFunction, "mappingFunction is null"); final Option<V> value = map.get(key); if (value.isDefined()) { return Tuple.of(value.get(), map); } else { final V newValue = mappingFunction.apply(key); final M newMap = (M) map.put(key, newValue); return Tuple.of(newValue, newMap); } }
/** * Gets the value if the computation result is a {@code Success} or throws if it was a {@code Failure}. * Waits for the result if necessary by blocking the current thread. * <p> * <strong>IMPORTANT! If the computation result is a {@link Try.Failure}, the underlying {@code cause} of type {@link Throwable} is thrown.</strong> * * @return The value of this {@code Future}. */ @Override default T get() { return await().getValue().get().get(); }
/** * Checks, if this future has a value. * * @return true, if this future succeeded with a value, false otherwise. */ @Override default boolean isEmpty() { return await().getValue().get().isEmpty(); }
/** * Checks if this Future completed with a success. * * @return true, if this Future completed and is a Success, false otherwise. */ default boolean isSuccess() { return isCompleted() && getValue().get().isSuccess(); }
@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 public TreeSet<T> init() { if (isEmpty()) { throw new UnsupportedOperationException("init of empty TreeSet"); } else { return new TreeSet<>(tree.delete(tree.max().get())); } }
@Override public TreeSet<T> tail() { if (isEmpty()) { throw new UnsupportedOperationException("tail of empty TreeSet"); } else { return new TreeSet<>(tree.delete(tree.min().get())); } }
/** * Checks if this Future completed with a failure. * * @return true, if this Future completed and is a Failure, false otherwise. */ default boolean isFailure() { return isCompleted() && getValue().get().isFailure(); }
@Override public TreeMap<K, V> tail() { if (isEmpty()) { throw new UnsupportedOperationException("tail of empty TreeMap"); } else { final Tuple2<K, V> min = entries.min().get(); return new TreeMap<>(entries.delete(min)); } }
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))); } }
@SuppressWarnings("unchecked") static <K, V, M extends Map<K, V>> Tuple2<Option<V>, M> computeIfPresent(M map, K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { final Option<V> value = map.get(key); if (value.isDefined()) { final V newValue = remappingFunction.apply(key, value.get()); final M newMap = (M) map.put(key, newValue); return Tuple.of(Option.of(newValue), newMap); } else { return Tuple.of(Option.none(), map); } }
@SuppressWarnings("unchecked") private <K2, V2> Multimap<K2, V2> createFromEntries(Iterable<? extends Tuple2<? extends K2, ? extends V2>> entries) { Map<K2, Traversable<V2>> back = emptyMapSupplier(); for (Tuple2<? extends K2, ? extends V2> entry : entries) { if (back.containsKey(entry._1)) { back = back.put(entry._1, containerType.add(back.get(entry._1).get(), entry._2)); } else { back = back.put(entry._1, containerType.add(emptyContainer.get(), entry._2)); } } return createFromMap(back); }