/** * 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); }
/** * 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); }
@Override public V getOrElse(K key, V defaultValue) { return get(key).getOrElse(defaultValue); }
/** * Turns this map from a partial function into a total function that * returns a value computed by defaultFunction for all keys * absent from the map. * * @param defaultFunction function to evaluate 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> withDefault(Function<? super K, ? extends V> defaultFunction) { return k -> get(k).getOrElse(() -> defaultFunction.apply(k)); }
@Override default boolean contains(Tuple2<K, V> element) { return get(element._1).map(v -> Objects.equals(v, element._2)).getOrElse(false); }
@Override public V computeIfAbsent(K cacheKey, CheckedFunction0<V> supplier) { return getValueFromCache(cacheKey) .getOrElse(() -> computeAndPut(cacheKey, supplier)); }
@Override public String toString() { return "Promise(" + future.getValue().map(String::valueOf).getOrElse("?") + ")"; } }
/** * 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(); }
@Override public Vector<T> replace(T currentElement, T newElement) { return indexOfOption(currentElement) .map(i -> update(i, newElement)) .getOrElse(this); }
@Override default boolean contains(Tuple2<K, V> element) { return get(element._1).map(v -> v.contains(element._2)).getOrElse(false); }
@SuppressWarnings("unchecked") static <K, V, U extends V, M extends Map<K, V>> M merge( M map, OfEntries<K, V, M> ofEntries, Map<? extends K, U> that, BiFunction<? super V, ? super U, ? extends V> collisionResolution) { Objects.requireNonNull(that, "that is null"); Objects.requireNonNull(collisionResolution, "collisionResolution is null"); if (map.isEmpty()) { return ofEntries.apply(Map.narrow(that)); } else if (that.isEmpty()) { return map; } else { return that.foldLeft(map, (result, entry) -> { final K key = entry._1; final U value = entry._2; final V newValue = result.get(key).map(v -> (V) collisionResolution.apply(v, value)).getOrElse(value); return (M) result.put(key, newValue); }); } }
@SuppressWarnings("unchecked") @Override public M put(K key, V value) { final Traversable<V> values = back.get(key).getOrElse((Traversable<V>) emptyContainer.get()); final Traversable<V> newValues = containerType.add(values, value); return (M) (newValues == values ? this : createFromMap(back.put(key, newValues))); }
@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 Map<String, ?> apply(Map<String, ?> config, String keyPrefix) { // Generate keys for table types based on table.types // eg. table.types=[offline,realtime] -> table.type.realtime=realtime, table.type.offline=offline config = ((Map<String, Object>) config).merge(config.get("table.types").map(typeList -> { if (typeList instanceof String) { return List.of((String) typeList); } else if (typeList instanceof Collection) { return List.ofAll((Collection<String>) typeList); } else { return List.empty(); } }).map(typeList -> typeList.map(type -> Tuple.of("table.type." + type.toString().toLowerCase(), type)) .toMap(Function.identity())).getOrElse(HashMap::empty)); config = config.remove("table.types"); return config; }
@Override public Map<String, ?> apply(Map<String, ?> childKeys, String pathPrefix) { // Adjust the name to add the table suffix to table.name.realtime/table.name.offline List<String> tableTypes = childKeys.get("table.types").map(tableTypesListOrString -> { if (tableTypesListOrString instanceof String) { return List.of((String) tableTypesListOrString); } else if (tableTypesListOrString instanceof Collection) { return List.ofAll((Collection<String>) tableTypesListOrString); } else { return List.empty(); } }).getOrElse(List.empty()).map(Object::toString); String tableName = childKeys.get("table.name").map(Object::toString).getOrElse( () -> childKeys.get("table.name.realtime").map(Object::toString) .getOrElse(() -> childKeys.get("table.name.offline").map(Object::toString).getOrNull())); Map<String, Object> remappedConfig = (Map<String, Object>) childKeys; for (String tableType : tableTypes) { String tableNameKey = "table.name." + tableType.toLowerCase(); CommonConstants.Helix.TableType type = CommonConstants.Helix.TableType.valueOf(tableType.toUpperCase()); remappedConfig = remappedConfig.put(tableNameKey, TableNameBuilder.forType(type).tableNameWithType(tableName)); } remappedConfig = remappedConfig.remove("table.name"); return remappedConfig; }
@SuppressWarnings("unchecked") @Override public M remove(K key, V value) { final Traversable<V> values = back.get(key).getOrElse((Traversable<V>) emptyContainer.get()); final Traversable<V> newValues = containerType.remove(values, value); if (newValues == values) { return (M) this; } else if (newValues.isEmpty()) { return (M) createFromMap(back.remove(key)); } else { return (M) createFromMap(back.put(key, newValues)); } }
@Override @SuppressWarnings("unchecked") public BitSet<T> addAll(Iterable<? extends T> elements) { final Stream<Integer> source = Stream.ofAll(elements).map(toInt); if (source.isEmpty()) { return this; } else { final long[] copy = copyExpand(1 + (source.max().getOrElse(0) >> ADDRESS_BITS_PER_WORD)); source.forEach(element -> { if (element < 0) { throw new IllegalArgumentException("bitset element must be >= 0"); } setElement(copy, element); }); final BitSet<T> bitSet = fromBitMaskNoCopy(copy); return (bitSet.length() == length()) ? this : bitSet; } }
@Override public MarkupDocBuilder apply(MarkupDocBuilder markupDocBuilder, Parameters params) { DataFrame dataFrame = params.dataFrame; java.util.List<MarkupTableColumn> columnSpecs = dataFrame.getColumns().map(column -> { Integer widthRatio = Integer.valueOf(column.getMetaData().get(WIDTH_RATIO).getOrElse("0")); return new MarkupTableColumn(column.getId().getName()) .withWidthRatio(widthRatio) .withHeaderColumn(Boolean.parseBoolean(column.getMetaData().get(HEADER_COLUMN).getOrElse("false"))) .withMarkupSpecifiers(MarkupLanguage.ASCIIDOC, ".^" + widthRatio + "a"); } ).toJavaList(); IndexedSeq<IndexedSeq<String>> columnValues = dataFrame.getColumns() .map(column -> ((StringColumn) column).getValues()); java.util.List<java.util.List<String>> cells = Array.range(0, dataFrame.getRowCount()) .map(rowNumber -> columnValues.map(values -> values.get(rowNumber)).toJavaList()).toJavaList(); return markupDocBuilder.tableWithColumnSpecs(columnSpecs, cells); }
@Override public Vector<T> replace(T currentElement, T newElement) { return indexOfOption(currentElement) .map(i -> update(i, newElement)) .getOrElse(this); }