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.TriShortToLongFunction.java

/**
 * Returns a memoized (caching) version of this {@link TriShortToLongFunction}. 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 ava  2s . 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 TriShortToLongFunction}.
 * @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 TriShortToLongFunction memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Triple<Short, Short, Short>, Long> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (TriShortToLongFunction & 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.to.ThrowableToFloatTriFunction.java

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

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

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

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

/**
 * Returns a memoized (caching) version of this {@link TriByteToCharFunction}. 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  .  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 TriByteToCharFunction}.
 * @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 TriByteToCharFunction memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Triple<Byte, Byte, Byte>, Character> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (TriByteToCharFunction & 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.TriFloatToIntFunction.java

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

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

/**
 * Returns a memoized (caching) version of this {@link TriLongToCharFunction}. 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  ava  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 TriLongToCharFunction}.
 * @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 TriLongToCharFunction memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Triple<Long, Long, Long>, Character> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (TriLongToCharFunction & 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.TriShortToIntFunction.java

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

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

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

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

/**
 * Returns a memoized (caching) version of this {@link ByteTernaryOperator}. 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. c om*/
 * 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 ByteTernaryOperator}.
 * @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 ByteTernaryOperator memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Triple<Byte, Byte, Byte>, Byte> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (ByteTernaryOperator & 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.function.tri.conversion.TriByteToDoubleFunction.java

/**
 * Returns a memoized (caching) version of this {@link TriByteToDoubleFunction}. 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 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 TriByteToDoubleFunction}.
 * @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 TriByteToDoubleFunction memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Triple<Byte, Byte, Byte>, Double> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (TriByteToDoubleFunction & 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;
        };
    }
}