Example usage for java.util Map computeIfAbsent

List of usage examples for java.util Map computeIfAbsent

Introduction

In this page you can find the example usage for java.util Map computeIfAbsent.

Prototype

default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) 

Source Link

Document

If the specified key is not already associated with a value (or is mapped to null ), attempts to compute its value using the given mapping function and enters it into this map unless null .

Usage

From source file:at.gridtec.lambda4j.predicate.bi.BiBytePredicate.java

/**
 * Returns a memoized (caching) version of this {@link BiBytePredicate}. 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>//www  .  ja va2s.co m
 * Unless the predicate and therefore the used cache will be garbage-collected, it will keep all memoized values
 * forever.
 *
 * @return A memoized (caching) version of this {@code BiBytePredicate}.
 * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the
 * resulting memoized predicate, as the cache used internally does not permit {@code null} keys or values.
 * @implNote The returned memoized predicate can be safely used concurrently from multiple threads which makes it
 * thread-safe.
 */
@Nonnull
default BiBytePredicate memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Pair<Byte, Byte>, Boolean> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (BiBytePredicate & Memoized) (value1, value2) -> {
            final boolean returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Pair.of(value1, value2),
                        key -> test(key.getLeft(), key.getRight()));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.predicate.bi.BiLongPredicate.java

/**
 * Returns a memoized (caching) version of this {@link BiLongPredicate}. 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 2s . c  o m*/
 * Unless the predicate and therefore the used cache will be garbage-collected, it will keep all memoized values
 * forever.
 *
 * @return A memoized (caching) version of this {@code BiLongPredicate}.
 * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the
 * resulting memoized predicate, as the cache used internally does not permit {@code null} keys or values.
 * @implNote The returned memoized predicate can be safely used concurrently from multiple threads which makes it
 * thread-safe.
 */
@Nonnull
default BiLongPredicate memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Pair<Long, Long>, Boolean> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (BiLongPredicate & Memoized) (value1, value2) -> {
            final boolean returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Pair.of(value1, value2),
                        key -> test(key.getLeft(), key.getRight()));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.predicate.bi.BiFloatPredicate.java

/**
 * Returns a memoized (caching) version of this {@link BiFloatPredicate}. 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 . jav  a 2s. c o  m
 * Unless the predicate and therefore the used cache will be garbage-collected, it will keep all memoized values
 * forever.
 *
 * @return A memoized (caching) version of this {@code BiFloatPredicate}.
 * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the
 * resulting memoized predicate, as the cache used internally does not permit {@code null} keys or values.
 * @implNote The returned memoized predicate can be safely used concurrently from multiple threads which makes it
 * thread-safe.
 */
@Nonnull
default BiFloatPredicate memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Pair<Float, Float>, Boolean> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (BiFloatPredicate & Memoized) (value1, value2) -> {
            final boolean returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Pair.of(value1, value2),
                        key -> test(key.getLeft(), key.getRight()));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.predicate.bi.BiShortPredicate.java

/**
 * Returns a memoized (caching) version of this {@link BiShortPredicate}. 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 a v  a  2 s  . c  o m
 * Unless the predicate and therefore the used cache will be garbage-collected, it will keep all memoized values
 * forever.
 *
 * @return A memoized (caching) version of this {@code BiShortPredicate}.
 * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the
 * resulting memoized predicate, as the cache used internally does not permit {@code null} keys or values.
 * @implNote The returned memoized predicate can be safely used concurrently from multiple threads which makes it
 * thread-safe.
 */
@Nonnull
default BiShortPredicate memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Pair<Short, Short>, Boolean> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (BiShortPredicate & Memoized) (value1, value2) -> {
            final boolean returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Pair.of(value1, value2),
                        key -> test(key.getLeft(), key.getRight()));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.function.tri.obj.ObjBiByteToByteFunction.java

/**
 * Returns a memoized (caching) version of this {@link ObjBiByteToByteFunction}. 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>/*  ww w  .  j av a  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 ObjBiByteToByteFunction}.
 * @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 ObjBiByteToByteFunction<T> memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Triple<T, Byte, Byte>, Byte> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (ObjBiByteToByteFunction<T> & Memoized) (t, value1, value2) -> {
            final byte returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Triple.of(t, value1, value2),
                        key -> applyAsByte(key.getLeft(), key.getMiddle(), key.getRight()));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.predicate.bi.BiIntPredicate.java

/**
 * Returns a memoized (caching) version of this {@link BiIntPredicate}. 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  2s .co  m*/
 * Unless the predicate and therefore the used cache will be garbage-collected, it will keep all memoized values
 * forever.
 *
 * @return A memoized (caching) version of this {@code BiIntPredicate}.
 * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the
 * resulting memoized predicate, as the cache used internally does not permit {@code null} keys or values.
 * @implNote The returned memoized predicate can be safely used concurrently from multiple threads which makes it
 * thread-safe.
 */
@Nonnull
default BiIntPredicate memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Pair<Integer, Integer>, Boolean> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (BiIntPredicate & Memoized) (value1, value2) -> {
            final boolean returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Pair.of(value1, value2),
                        key -> test(key.getLeft(), key.getRight()));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.function.tri.obj.ObjBiLongToLongFunction.java

/**
 * Returns a memoized (caching) version of this {@link ObjBiLongToLongFunction}. 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 a  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 ObjBiLongToLongFunction}.
 * @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 ObjBiLongToLongFunction<T> memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Triple<T, Long, Long>, Long> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (ObjBiLongToLongFunction<T> & Memoized) (t, value1, value2) -> {
            final long returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Triple.of(t, value1, value2),
                        key -> applyAsLong(key.getLeft(), key.getMiddle(), key.getRight()));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.predicate.bi.BiDoublePredicate.java

/**
 * Returns a memoized (caching) version of this {@link BiDoublePredicate}. 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  .  jav a  2  s  .  c  om*/
 * Unless the predicate and therefore the used cache will be garbage-collected, it will keep all memoized values
 * forever.
 *
 * @return A memoized (caching) version of this {@code BiDoublePredicate}.
 * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the
 * resulting memoized predicate, as the cache used internally does not permit {@code null} keys or values.
 * @implNote The returned memoized predicate can be safely used concurrently from multiple threads which makes it
 * thread-safe.
 */
@Nonnull
default BiDoublePredicate memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Pair<Double, Double>, Boolean> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (BiDoublePredicate & Memoized) (value1, value2) -> {
            final boolean returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Pair.of(value1, value2),
                        key -> test(key.getLeft(), key.getRight()));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.function.tri.obj.ObjBiByteToIntFunction.java

/**
 * Returns a memoized (caching) version of this {@link ObjBiByteToIntFunction}. 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. 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 ObjBiByteToIntFunction}.
 * @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 ObjBiByteToIntFunction<T> memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Triple<T, Byte, Byte>, Integer> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (ObjBiByteToIntFunction<T> & Memoized) (t, value1, value2) -> {
            final int returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Triple.of(t, value1, value2),
                        key -> applyAsInt(key.getLeft(), key.getMiddle(), key.getRight()));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.function.tri.obj.ObjBiLongToByteFunction.java

/**
 * Returns a memoized (caching) version of this {@link ObjBiLongToByteFunction}. 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 www  .jav  a2 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 ObjBiLongToByteFunction}.
 * @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 ObjBiLongToByteFunction<T> memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Triple<T, Long, Long>, Byte> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (ObjBiLongToByteFunction<T> & Memoized) (t, value1, value2) -> {
            final byte returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Triple.of(t, value1, value2),
                        key -> applyAsByte(key.getLeft(), key.getMiddle(), key.getRight()));
            }
            return returnValue;
        };
    }
}