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.operator.binary.LongBinaryOperator2.java
/** * Returns a memoized (caching) version of this {@link LongBinaryOperator2}. 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 v a 2s . c o m * Unless the operator and therefore the used cache will be garbage-collected, it will keep all memoized values * forever. * * @return A memoized (caching) version of this {@code LongBinaryOperator2}. * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the * resulting memoized operator, as the cache used internally does not permit {@code null} keys or values. * @implNote The returned memoized operator can be safely used concurrently from multiple threads which makes it * thread-safe. */ @Nonnull default LongBinaryOperator2 memoized() { if (isMemoized()) { return this; } else { final Map<Pair<Long, Long>, Long> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (LongBinaryOperator2 & Memoized) (value1, value2) -> { final long returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Pair.of(value1, value2), key -> applyAsLong(key.getLeft(), key.getRight())); } return returnValue; }; } }
From source file:at.gridtec.lambda4j.operator.binary.FloatBinaryOperator.java
/** * Returns a memoized (caching) version of this {@link FloatBinaryOperator}. 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 2s . co m * Unless the operator and therefore the used cache will be garbage-collected, it will keep all memoized values * forever. * * @return A memoized (caching) version of this {@code FloatBinaryOperator}. * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the * resulting memoized operator, as the cache used internally does not permit {@code null} keys or values. * @implNote The returned memoized operator can be safely used concurrently from multiple threads which makes it * thread-safe. */ @Nonnull default FloatBinaryOperator memoized() { if (isMemoized()) { return this; } else { final Map<Pair<Float, Float>, Float> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (FloatBinaryOperator & Memoized) (value1, value2) -> { final float returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Pair.of(value1, value2), key -> applyAsFloat(key.getLeft(), key.getRight())); } return returnValue; }; } }
From source file:at.gridtec.lambda4j.operator.binary.ShortBinaryOperator.java
/** * Returns a memoized (caching) version of this {@link ShortBinaryOperator}. 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 2 s .com*/ * Unless the operator and therefore the used cache will be garbage-collected, it will keep all memoized values * forever. * * @return A memoized (caching) version of this {@code ShortBinaryOperator}. * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the * resulting memoized operator, as the cache used internally does not permit {@code null} keys or values. * @implNote The returned memoized operator can be safely used concurrently from multiple threads which makes it * thread-safe. */ @Nonnull default ShortBinaryOperator memoized() { if (isMemoized()) { return this; } else { final Map<Pair<Short, Short>, Short> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (ShortBinaryOperator & Memoized) (value1, value2) -> { final short returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Pair.of(value1, value2), key -> applyAsShort(key.getLeft(), key.getRight())); } return returnValue; }; } }
From source file:at.gridtec.lambda4j.function.bi.to.ThrowableToLongBiFunction.java
/** * Returns a memoized (caching) version of this {@link ThrowableToLongBiFunction}. 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.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 ThrowableToLongBiFunction}. * @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 ThrowableToLongBiFunction<T, U, X> memoized() { if (isMemoized()) { return this; } else { final Map<Pair<T, U>, Long> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (ThrowableToLongBiFunction<T, U, X> & Memoized) (t, u) -> { final long returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Pair.of(t, u), ThrowableFunction.of(key -> applyAsLongThrows(key.getLeft(), key.getRight()))); } return returnValue; }; } }
From source file:at.gridtec.lambda4j.function.bi.to.ThrowableToIntBiFunction.java
/** * Returns a memoized (caching) version of this {@link ThrowableToIntBiFunction}. 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 ww w .j ava2s . 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 ThrowableToIntBiFunction}. * @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 ThrowableToIntBiFunction<T, U, X> memoized() { if (isMemoized()) { return this; } else { final Map<Pair<T, U>, Integer> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (ThrowableToIntBiFunction<T, U, X> & Memoized) (t, u) -> { final int returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Pair.of(t, u), ThrowableFunction.of(key -> applyAsIntThrows(key.getLeft(), key.getRight()))); } return returnValue; }; } }
From source file:at.gridtec.lambda4j.function.bi.ThrowableBiByteFunction.java
/** * Returns a memoized (caching) version of this {@link ThrowableBiByteFunction}. 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 ww w . 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 ThrowableBiByteFunction}. * @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 ThrowableBiByteFunction<R, X> memoized() { if (isMemoized()) { return this; } else { final Map<Pair<Byte, Byte>, R> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (ThrowableBiByteFunction<R, X> & Memoized) (value1, value2) -> { final R returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Pair.of(value1, value2), ThrowableFunction.of(key -> applyThrows(key.getLeft(), key.getRight()))); } return returnValue; }; } }
From source file:at.gridtec.lambda4j.function.bi.ThrowableBiLongFunction.java
/** * Returns a memoized (caching) version of this {@link ThrowableBiLongFunction}. 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 . ja v a2s. 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 ThrowableBiLongFunction}. * @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 ThrowableBiLongFunction<R, X> memoized() { if (isMemoized()) { return this; } else { final Map<Pair<Long, Long>, R> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (ThrowableBiLongFunction<R, X> & Memoized) (value1, value2) -> { final R returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Pair.of(value1, value2), ThrowableFunction.of(key -> applyThrows(key.getLeft(), key.getRight()))); } return returnValue; }; } }
From source file:at.gridtec.lambda4j.function.bi.ThrowableBiBooleanFunction.java
/** * Returns a memoized (caching) version of this {@link ThrowableBiBooleanFunction}. 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 a2 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 ThrowableBiBooleanFunction}. * @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 ThrowableBiBooleanFunction<R, X> memoized() { if (isMemoized()) { return this; } else { final Map<Pair<Boolean, Boolean>, R> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (ThrowableBiBooleanFunction<R, X> & Memoized) (value1, value2) -> { final R returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Pair.of(value1, value2), ThrowableFunction.of(key -> applyThrows(key.getLeft(), key.getRight()))); } return returnValue; }; } }
From source file:at.gridtec.lambda4j.function.bi.ThrowableBiFloatFunction.java
/** * Returns a memoized (caching) version of this {@link ThrowableBiFloatFunction}. 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 . jav a 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 ThrowableBiFloatFunction}. * @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 ThrowableBiFloatFunction<R, X> memoized() { if (isMemoized()) { return this; } else { final Map<Pair<Float, Float>, R> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (ThrowableBiFloatFunction<R, X> & Memoized) (value1, value2) -> { final R returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Pair.of(value1, value2), ThrowableFunction.of(key -> applyThrows(key.getLeft(), key.getRight()))); } return returnValue; }; } }
From source file:at.gridtec.lambda4j.function.bi.ThrowableBiShortFunction.java
/** * Returns a memoized (caching) version of this {@link ThrowableBiShortFunction}. 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 .java2 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 ThrowableBiShortFunction}. * @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 ThrowableBiShortFunction<R, X> memoized() { if (isMemoized()) { return this; } else { final Map<Pair<Short, Short>, R> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (ThrowableBiShortFunction<R, X> & Memoized) (value1, value2) -> { final R returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Pair.of(value1, value2), ThrowableFunction.of(key -> applyThrows(key.getLeft(), key.getRight()))); } return returnValue; }; } }