List of usage examples for java.util Map computeIfAbsent
default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)
From source file:at.gridtec.lambda4j.function.tri.to.ToCharTriFunction.java
/** * Returns a memoized (caching) version of this {@link ToCharTriFunction}. Whenever it is called, the mapping * between the input parameters and the return value is preserved in a cache, making subsequent calls returning the * memoized value instead of computing the return value again. * <p>// w ww.ja v a2s . co m * Unless the function and therefore the used cache will be garbage-collected, it will keep all memoized values * forever. * * @return A memoized (caching) version of this {@code ToCharTriFunction}. * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the * resulting memoized function, as the cache used internally does not permit {@code null} keys or values. * @implNote The returned memoized function can be safely used concurrently from multiple threads which makes it * thread-safe. */ @Nonnull default ToCharTriFunction<T, U, V> memoized() { if (isMemoized()) { return this; } else { final Map<Triple<T, U, V>, Character> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (ToCharTriFunction<T, U, V> & Memoized) (t, u, v) -> { final char returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Triple.of(t, u, v), key -> applyAsChar(key.getLeft(), key.getMiddle(), key.getRight())); } return returnValue; }; } }
From source file:at.gridtec.lambda4j.function.tri.to.ToDoubleTriFunction.java
/** * Returns a memoized (caching) version of this {@link ToDoubleTriFunction}. Whenever it is called, the mapping * between the input parameters and the return value is preserved in a cache, making subsequent calls returning the * memoized value instead of computing the return value again. * <p>// w w w . java 2s . c o m * Unless the function and therefore the used cache will be garbage-collected, it will keep all memoized values * forever. * * @return A memoized (caching) version of this {@code ToDoubleTriFunction}. * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the * resulting memoized function, as the cache used internally does not permit {@code null} keys or values. * @implNote The returned memoized function can be safely used concurrently from multiple threads which makes it * thread-safe. */ @Nonnull default ToDoubleTriFunction<T, U, V> memoized() { if (isMemoized()) { return this; } else { final Map<Triple<T, U, V>, Double> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (ToDoubleTriFunction<T, U, V> & Memoized) (t, u, v) -> { final double returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Triple.of(t, u, v), key -> applyAsDouble(key.getLeft(), key.getMiddle(), key.getRight())); } return returnValue; }; } }
From source file:at.gridtec.lambda4j.function.bi.ThrowableBiFunction.java
/** * Returns a memoized (caching) version of this {@link ThrowableBiFunction}. Whenever it is called, the mapping * between the input parameters and the return value is preserved in a cache, making subsequent calls returning the * memoized value instead of computing the return value again. * <p>/*from w w w . j a va 2 s .c om*/ * Unless the function and therefore the used cache will be garbage-collected, it will keep all memoized values * forever. * * @return A memoized (caching) version of this {@code ThrowableBiFunction}. * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the * resulting memoized function, as the cache used internally does not permit {@code null} keys or values. * @implNote The returned memoized function can be safely used concurrently from multiple threads which makes it * thread-safe. */ @Nonnull default ThrowableBiFunction<T, U, R, X> memoized() { if (isMemoized()) { return this; } else { final Map<Pair<T, U>, R> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (ThrowableBiFunction<T, U, R, X> & Memoized) (t, u) -> { final R returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Pair.of(t, u), ThrowableFunction.of(key -> applyThrows(key.getLeft(), key.getRight()))); } return returnValue; }; } }
From source file:com.evolveum.midpoint.prism.marshaller.PrismBeanInspector.java
private <V, P1, P2, P3> V find3(final Map<P1, Map<P2, Map<P3, V>>> cache, final P1 param1, final P2 param2, final P3 param3, final Getter3<V, P1, P2, P3> getter) { Map<P2, Map<P3, V>> cache2 = cache.computeIfAbsent(param1, k -> Collections.synchronizedMap(new HashMap<>())); return find2(cache2, param2, param3, (p, q) -> getter.get(param1, p, q)); }
From source file:at.gridtec.lambda4j.function.tri.TriByteFunction.java
/** * Returns a memoized (caching) version of this {@link TriByteFunction}. Whenever it is called, the mapping between * the input parameters and the return value is preserved in a cache, making subsequent calls returning the memoized * value instead of computing the return value again. * <p>/*from w ww .j a v a 2 s.com*/ * Unless the function and therefore the used cache will be garbage-collected, it will keep all memoized values * forever. * * @return A memoized (caching) version of this {@code TriByteFunction}. * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the * resulting memoized function, as the cache used internally does not permit {@code null} keys or values. * @implNote The returned memoized function can be safely used concurrently from multiple threads which makes it * thread-safe. */ @Nonnull default TriByteFunction<R> memoized() { if (isMemoized()) { return this; } else { final Map<Triple<Byte, Byte, Byte>, R> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (TriByteFunction<R> & Memoized) (value1, value2, value3) -> { final R returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Triple.of(value1, value2, value3), key -> apply(key.getLeft(), key.getMiddle(), key.getRight())); } return returnValue; }; } }
From source file:at.gridtec.lambda4j.function.tri.TriLongFunction.java
/** * Returns a memoized (caching) version of this {@link TriLongFunction}. Whenever it is called, the mapping between * the input parameters and the return value is preserved in a cache, making subsequent calls returning the memoized * value instead of computing the return value again. * <p>//from w ww . j av a2 s.c om * Unless the function and therefore the used cache will be garbage-collected, it will keep all memoized values * forever. * * @return A memoized (caching) version of this {@code TriLongFunction}. * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the * resulting memoized function, as the cache used internally does not permit {@code null} keys or values. * @implNote The returned memoized function can be safely used concurrently from multiple threads which makes it * thread-safe. */ @Nonnull default TriLongFunction<R> memoized() { if (isMemoized()) { return this; } else { final Map<Triple<Long, Long, Long>, R> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (TriLongFunction<R> & Memoized) (value1, value2, value3) -> { final R returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Triple.of(value1, value2, value3), key -> apply(key.getLeft(), key.getMiddle(), key.getRight())); } return returnValue; }; } }
From source file:at.gridtec.lambda4j.function.tri.ThrowableTriFunction.java
/** * Returns a memoized (caching) version of this {@link ThrowableTriFunction}. Whenever it is called, the mapping * between the input parameters and the return value is preserved in a cache, making subsequent calls returning the * memoized value instead of computing the return value again. * <p>/*from w w w . j a va 2 s .c o m*/ * Unless the function and therefore the used cache will be garbage-collected, it will keep all memoized values * forever. * * @return A memoized (caching) version of this {@code ThrowableTriFunction}. * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the * resulting memoized function, as the cache used internally does not permit {@code null} keys or values. * @implNote The returned memoized function can be safely used concurrently from multiple threads which makes it * thread-safe. */ @Nonnull default ThrowableTriFunction<T, U, V, R, X> memoized() { if (isMemoized()) { return this; } else { final Map<Triple<T, U, V>, R> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (ThrowableTriFunction<T, U, V, R, X> & Memoized) (t, u, v) -> { final R returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Triple.of(t, u, v), ThrowableFunction .of(key -> applyThrows(key.getLeft(), key.getMiddle(), key.getRight()))); } return returnValue; }; } }
From source file:eu.fthevenet.binjr.data.codec.CsvDecoder.java
@Override public Map<TimeSeriesInfo<T>, TimeSeriesProcessor<T>> decode(InputStream in, List<TimeSeriesInfo<T>> seriesInfo) throws IOException, DecodingDataFromAdapterException { try (Profiler ignored = Profiler.start("Building time series from csv data", logger::trace)) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, encoding))) { CSVFormat csvFormat = CSVFormat.DEFAULT.withAllowMissingColumnNames(false).withFirstRecordAsHeader() .withSkipHeaderRecord().withDelimiter(delimiter); Iterable<CSVRecord> records = csvFormat.parse(reader); Map<TimeSeriesInfo<T>, TimeSeriesProcessor<T>> series = new HashMap<>(); final AtomicLong nbpoints = new AtomicLong(0); for (CSVRecord csvRecord : records) { nbpoints.incrementAndGet(); ZonedDateTime timeStamp = dateParser.apply(csvRecord.get(0)); for (TimeSeriesInfo<T> info : seriesInfo) { T val = numberParser.apply(csvRecord.get(info.getBinding().getLabel())); XYChart.Data<ZonedDateTime, T> point = new XYChart.Data<>(timeStamp, val); TimeSeriesProcessor<T> l = series.computeIfAbsent(info, k -> timeSeriesFactory.create()); l.addSample(point);//from ww w . j a va 2 s . co m } } logger.trace(() -> String.format("Built %d series with %d samples each (%d total samples)", seriesInfo.size(), nbpoints.get(), seriesInfo.size() * nbpoints.get())); return series; } } }
From source file:at.gridtec.lambda4j.function.tri.TriFloatFunction.java
/** * Returns a memoized (caching) version of this {@link TriFloatFunction}. Whenever it is called, the mapping between * the input parameters and the return value is preserved in a cache, making subsequent calls returning the memoized * value instead of computing the return value again. * <p>/*w w w.j a va 2 s . co m*/ * Unless the function and therefore the used cache will be garbage-collected, it will keep all memoized values * forever. * * @return A memoized (caching) version of this {@code TriFloatFunction}. * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the * resulting memoized function, as the cache used internally does not permit {@code null} keys or values. * @implNote The returned memoized function can be safely used concurrently from multiple threads which makes it * thread-safe. */ @Nonnull default TriFloatFunction<R> memoized() { if (isMemoized()) { return this; } else { final Map<Triple<Float, Float, Float>, R> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (TriFloatFunction<R> & Memoized) (value1, value2, value3) -> { final R returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Triple.of(value1, value2, value3), key -> apply(key.getLeft(), key.getMiddle(), key.getRight())); } return returnValue; }; } }
From source file:at.gridtec.lambda4j.function.tri.TriShortFunction.java
/** * Returns a memoized (caching) version of this {@link TriShortFunction}. Whenever it is called, the mapping between * the input parameters and the return value is preserved in a cache, making subsequent calls returning the memoized * value instead of computing the return value again. * <p>/*from w w w . j av a 2s . c o m*/ * Unless the function and therefore the used cache will be garbage-collected, it will keep all memoized values * forever. * * @return A memoized (caching) version of this {@code TriShortFunction}. * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the * resulting memoized function, as the cache used internally does not permit {@code null} keys or values. * @implNote The returned memoized function can be safely used concurrently from multiple threads which makes it * thread-safe. */ @Nonnull default TriShortFunction<R> memoized() { if (isMemoized()) { return this; } else { final Map<Triple<Short, Short, Short>, R> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (TriShortFunction<R> & Memoized) (value1, value2, value3) -> { final R returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Triple.of(value1, value2, value3), key -> apply(key.getLeft(), key.getMiddle(), key.getRight())); } return returnValue; }; } }