@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); } }
/** * Folds either the {@code None} or the {@code Some} side of the Option value. * * @param ifNone maps the left value if this is a None * @param f maps the value if this is a Some * @param <U> type of the folded value * @return A value of type U */ default <U> U fold(Supplier<? extends U> ifNone, Function<? super T, ? extends U> f) { return this.<U>map(f).getOrElse(ifNone); }
@Deprecated @Override default V apply(K key) { return get(key).getOrElseThrow(() -> new NoSuchElementException(String.valueOf(key))); }
/** * 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(); }
/** * 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())); }
/** * Alias for {@link Option#of(Object)} * * @param <T> type of the value * @param value A value * @return {@link Option.Some} if value is not {@code null}, {@link Option.None} otherwise */ public static <T> Option<T> Option(T value) { return Option.of(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(); }
private Option<V> getValueFromCache(K cacheKey){ try { Option<V> result = Option.of(cache.get(cacheKey)); if (result.isDefined()) { onCacheHit(cacheKey); return result; } else { onCacheMiss(cacheKey); return result; } }catch (Exception exception){ LOG.warn("Failed to get a value from Cache {}", getName(), exception); onError(exception); return Option.none(); } }
/** * Applies an action to this value, if this option is defined, otherwise does nothing. * * @param action An action which can be applied to an optional value * @return this {@code Option} */ @Override default Option<T> peek(Consumer<? super T> action) { Objects.requireNonNull(action, "action is null"); if (isDefined()) { action.accept(get()); } return this; }
/** * Turns this map from a partial function into a total function that * returns defaultValue for all keys absent from the map. * * @param defaultValue default value to return for all keys not present in the map * @return a total function from K to T * @deprecated Will be removed */ @Deprecated default Function1<K, V> withDefaultValue(V defaultValue) { return k -> get(k).getOrElse(defaultValue); }
/** * 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))); }
/** * Creates a RateLimiter. * * @param name the name of the RateLimiter * @param rateLimiterConfig The RateLimiter configuration. * @param scheduler executor that will refresh permissions */ public SemaphoreBasedRateLimiter(String name, RateLimiterConfig rateLimiterConfig, ScheduledExecutorService scheduler) { this.name = requireNonNull(name, NAME_MUST_NOT_BE_NULL); this.rateLimiterConfig = new AtomicReference<>(requireNonNull(rateLimiterConfig, CONFIG_MUST_NOT_BE_NULL)); this.scheduler = Option.of(scheduler).getOrElse(this::configureScheduler); this.semaphore = new Semaphore(this.rateLimiterConfig.get().getLimitForPeriod(), true); this.metrics = this.new SemaphoreBasedRateLimiterMetrics(); this.eventProcessor = new RateLimiterEventProcessor(); scheduleLimitRefresh(); }
@Test public void testOptionMappedShouldSucceed() { final Set<Option<String>> result = dbRule.getSharedHandle() .createQuery("select name from something") .collectInto(new GenericType<Set<Option<String>>>() {}); assertThat(result).hasSize(2); assertThat(result).contains(Option.none(), Option.of("eric")); }
@SuppressWarnings("unchecked") @Override public <K2 extends K, V2 extends V> M merge(Multimap<K2, V2> that, BiFunction<Traversable<V>, Traversable<V2>, Traversable<V>> collisionResolution) { Objects.requireNonNull(that, "that is null"); Objects.requireNonNull(collisionResolution, "collisionResolution is null"); if (isEmpty()) { return (M) createFromEntries(that); } else if (that.isEmpty()) { return (M) this; } else { final Map<K, Traversable<V>> result = that.keySet().foldLeft(this.back, (map, key) -> { final Traversable<V> thisValues = map.get(key).getOrElse((Traversable<V>) emptyContainer.get()); final Traversable<V2> thatValues = that.get(key).get(); final Traversable<V> newValues = collisionResolution.apply(thisValues, thatValues); return map.put(key, newValues); }); return (M) createFromMap(result); } }
@Override public T head() { if (isEmpty()) { throw new NoSuchElementException("head of empty TreeSet"); } else { return tree.min().get(); } }
@Override public boolean hasNext() { return nextVal.isDefined(); }