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.bi.obj.ThrowableObjFloatFunction.java

/**
 * Returns a memoized (caching) version of this {@link ThrowableObjFloatFunction}. 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 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 ThrowableObjFloatFunction}.
 * @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 ThrowableObjFloatFunction<T, R, X> memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Pair<T, Float>, R> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (ThrowableObjFloatFunction<T, R, X> & Memoized) (t, value) -> {
            final R returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Pair.of(t, value),
                        ThrowableFunction.of(key -> applyThrows(key.getLeft(), key.getRight())));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.function.bi.obj.ThrowableObjIntFunction.java

/**
 * Returns a memoized (caching) version of this {@link ThrowableObjIntFunction}. 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 va  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 ThrowableObjIntFunction}.
 * @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 ThrowableObjIntFunction<T, R, X> memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Pair<T, Integer>, R> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (ThrowableObjIntFunction<T, R, X> & Memoized) (t, value) -> {
            final R returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Pair.of(t, value),
                        ThrowableFunction.of(key -> applyThrows(key.getLeft(), key.getRight())));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.function.bi.obj.ThrowableObjShortFunction.java

/**
 * Returns a memoized (caching) version of this {@link ThrowableObjShortFunction}. 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  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 ThrowableObjShortFunction}.
 * @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 ThrowableObjShortFunction<T, R, X> memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Pair<T, Short>, R> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (ThrowableObjShortFunction<T, R, X> & Memoized) (t, value) -> {
            final R returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Pair.of(t, value),
                        ThrowableFunction.of(key -> applyThrows(key.getLeft(), key.getRight())));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.function.bi.obj.ThrowableObjDoubleFunction.java

/**
 * Returns a memoized (caching) version of this {@link ThrowableObjDoubleFunction}. 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  a2 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 ThrowableObjDoubleFunction}.
 * @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 ThrowableObjDoubleFunction<T, R, X> memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Pair<T, Double>, R> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (ThrowableObjDoubleFunction<T, R, X> & Memoized) (t, value) -> {
            final R returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Pair.of(t, value),
                        ThrowableFunction.of(key -> applyThrows(key.getLeft(), key.getRight())));
            }
            return returnValue;
        };
    }
}

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

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

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

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

/**
 * Returns a memoized (caching) version of this {@link ThrowableObjCharFunction}. 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 ava 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 ThrowableObjCharFunction}.
 * @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 ThrowableObjCharFunction<T, R, X> memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Pair<T, Character>, R> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (ThrowableObjCharFunction<T, R, X> & Memoized) (t, value) -> {
            final R returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Pair.of(t, value),
                        ThrowableFunction.of(key -> applyThrows(key.getLeft(), key.getRight())));
            }
            return returnValue;
        };
    }
}

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

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

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