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.function.tri.conversion.TriIntToByteFunction.java

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

From source file:at.gridtec.lambda4j.operator.ternary.LongTernaryOperator.java

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

From source file:at.gridtec.lambda4j.function.tri.conversion.TriFloatToShortFunction.java

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

From source file:at.gridtec.lambda4j.function.tri.conversion.TriIntToLongFunction.java

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

From source file:at.gridtec.lambda4j.function.tri.conversion.TriLongToDoubleFunction.java

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

From source file:at.gridtec.lambda4j.function.tri.conversion.TriShortToFloatFunction.java

/**
 * Returns a memoized (caching) version of this {@link TriShortToFloatFunction}. 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  va2 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 TriShortToFloatFunction}.
 * @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 TriShortToFloatFunction memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Triple<Short, Short, Short>, Float> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (TriShortToFloatFunction & Memoized) (value1, value2, value3) -> {
            final float returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Triple.of(value1, value2, value3),
                        key -> applyAsFloat(key.getLeft(), key.getMiddle(), key.getRight()));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.function.tri.to.ThrowableToDoubleTriFunction.java

/**
 * Returns a memoized (caching) version of this {@link ThrowableToDoubleTriFunction}. 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 2s  .  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 ThrowableToDoubleTriFunction}.
 * @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 ThrowableToDoubleTriFunction<T, U, V, X> memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Triple<T, U, V>, Double> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (ThrowableToDoubleTriFunction<T, U, V, X> & Memoized) (t, u, v) -> {
            final double returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Triple.of(t, u, v), ThrowableFunction
                        .of(key -> applyAsDoubleThrows(key.getLeft(), key.getMiddle(), key.getRight())));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.function.tri.conversion.TriFloatToCharFunction.java

/**
 * Returns a memoized (caching) version of this {@link TriFloatToCharFunction}. 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 .ja  va 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 TriFloatToCharFunction}.
 * @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 TriFloatToCharFunction memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Triple<Float, Float, Float>, Character> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (TriFloatToCharFunction & Memoized) (value1, value2, value3) -> {
            final char returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Triple.of(value1, value2, value3),
                        key -> applyAsChar(key.getLeft(), key.getMiddle(), key.getRight()));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.function.tri.conversion.TriShortToCharFunction.java

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

From source file:at.gridtec.lambda4j.function.tri.conversion.TriDoubleToByteFunction.java

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