/** * 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); }
/** * {@inheritDoc} */ @Override public Option<T> take() { return Option.of(queue.poll()); } }
@Override default Option<T> findLast(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); T last = null; while (hasNext()) { final T elem = next(); if (predicate.test(elem)) { last = elem; } } return Option.of(last); }
Option<String> getConfiguredColumnName(int tupleIndex, ConfigRegistry config) { return Option.of(config.get(TupleMappers.class) .getColumn(tupleIndex)); }
@SuppressWarnings("unchecked") @Override public Option<T> map(ResultSet r, int columnNumber, StatementContext ctx) throws SQLException { final ColumnMapper<?> mapper = ctx.findColumnMapperFor(nestedType) .orElseThrow(() -> new NoSuchMapperException("No mapper for type " + nestedType + " nested in Option")); return (Option<T>) Option.of(mapper.map(r, columnNumber, ctx)); }
/** * 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(); }
@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); } }
Array<Tuple3<Type, Integer, Option<String>>> resolveKeyValueColumns(ConfigRegistry config, Array<Tuple2<Type, Integer>> tupleTypes) { Array<Tuple3<Type, Integer, Option<String>>> withConfiguredColumnName; Tuple2<Type, Integer> keyType = tupleTypes.get(0); Tuple2<Type, Integer> valueType = tupleTypes.get(1); withConfiguredColumnName = Array.of( Tuple.of(keyType._1, keyType._2, Option.of(config.get(TupleMappers.class).getKeyColumn())), Tuple.of(valueType._1, valueType._2, Option.of(config.get(TupleMappers.class).getValueColumn())) ); return withConfiguredColumnName; }
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(); } }
@Test public void testGetNonValueArgumentShouldNotBeEmpty() { Optional<Argument> arg = unit.build(new GenericType<Option<Integer>>() {}.getType(), Option.of(1), null); assertThat(arg).isNotEmpty(); }
@Test public void testGetOptionShouldReturnCorrectRow() { Something result = dbRule.getSharedHandle().createQuery(SELECT_BY_NAME) .bind("name", Option.of("eric")) .mapToBean(Something.class) .findOnly(); assertThat(result).isEqualTo(ERICSOMETHING); }
/** * 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))); }
@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")); }
@Test public void testOptionMappedWithinObjectIfPresentShouldContainValue() { final SomethingWithOption result = dbRule.getSharedHandle() .registerRowMapper(ConstructorMapper.factory(SomethingWithOption.class)) .createQuery("select id, name from something where id = 1") .mapTo(SomethingWithOption.class) .findOnly(); assertThat(result.getName()).isInstanceOf(Option.class); assertThat(result).isEqualTo(new SomethingWithOption(1, Option.of("eric"))); }
/** * 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); }
@Override default Option<T> findLast(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); T last = null; while (hasNext()) { final T elem = next(); if (predicate.test(elem)) { last = elem; } } return Option.of(last); }
Map<String, String> getCompressionOptions(final String name) throws BackendException { final KeyspaceMetadata keyspaceMetadata = Option.of(this.cluster.getMetadata().getKeyspace(this.keyspace)) .getOrElseThrow(() -> new PermanentBackendException(String.format("Unknown keyspace '%s'", this.keyspace))); return Option.of(keyspaceMetadata.getTable(name)) .map(tableMetadata -> tableMetadata.getOptions().getCompression()) .getOrElseThrow(() -> new PermanentBackendException(String.format("Unknown table '%s'", name))); }
public Option<EventStream> getEventStream(EventStreamId eventStreamId) { return Option.of(eventstreams.computeIfAbsent(eventStreamId, definition -> getEventStore(eventStreamId .eventStoreName()).map(eventStore -> new EventStream(eventStore, eventStreamId)).getOrNull())); }
@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; } }
@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); } }