本文整理了Java中io.vavr.control.Option
类的一些代码示例,展示了Option
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Option
类的具体详情如下:
包路径:io.vavr.control.Option
类名称:Option
[英]Replacement for java.util.Optional.
Option is a monadic container type which represents an optional value. Instances of Option are either an instance of Some or the singleton None.
Most of the API is taken from java.util.Optional. A similar type can be found in Haskell and Scala.
[中]java的替代品。util。可选择的
选项是一个{$0$}容器类型,它表示一个可选值。Option的实例可以是Some的实例,也可以是singleton None的实例。
大部分API都来自java。util。可选择的类似的类型可以在Haskell和{$2$}中找到。
代码示例来源:origin: vavr-io/vavr
@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);
}
}
代码示例来源:origin: vavr-io/vavr
/**
* 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);
}
代码示例来源:origin: vavr-io/vavr
@Deprecated
@Override
default V apply(K key) {
return get(key).getOrElseThrow(() -> new NoSuchElementException(String.valueOf(key)));
}
代码示例来源:origin: vavr-io/vavr
/**
* 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);
}
代码示例来源:origin: vavr-io/vavr
/**
* 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();
}
代码示例来源:origin: vavr-io/vavr
/**
* 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()));
}
代码示例来源:origin: vavr-io/vavr
/**
* 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);
}
代码示例来源:origin: vavr-io/vavr
/**
* 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();
}
代码示例来源:origin: resilience4j/resilience4j
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();
}
}
代码示例来源:origin: vavr-io/vavr
/**
* 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;
}
代码示例来源:origin: vavr-io/vavr
/**
* 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);
}
代码示例来源:origin: com.pragmaticobjects.oo.atom/atom-basis
@Override
public final boolean matches(final TypeDescription target) {
boolean truth = matcher.matches(target);
if(!truth) {
truth = Option.of(target.getSuperClass())
.map(TypeDescription.Generic::asErasure)
.map(this::matches)
.getOrElse(false);
}
return truth;
}
}
代码示例来源:origin: vavr-io/vavr
/**
* 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)));
}
代码示例来源:origin: vavr-io/vavr
@Override
public T getNext() {
final T result = next.get();
next = Option.none();
return result;
}
};
代码示例来源:origin: resilience4j/resilience4j
/**
* 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();
}
代码示例来源:origin: jdbi/jdbi
@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"));
}
代码示例来源:origin: vavr-io/vavr
@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();
}
代码示例来源:origin: vavr-io/vavr
@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);
}
}
代码示例来源:origin: vavr-io/vavr
@Override
public T head() {
if (isEmpty()) {
throw new NoSuchElementException("head of empty TreeSet");
} else {
return tree.min().get();
}
}
代码示例来源:origin: vavr-io/vavr
@Override
public boolean hasNext() {
return nextVal.isDefined();
}
内容来源于网络,如有侵权,请联系作者删除!