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.obj.ObjBiBooleanToByteFunction.java

/**
 * Returns a memoized (caching) version of this {@link ObjBiBooleanToByteFunction}. 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  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 ObjBiBooleanToByteFunction}.
 * @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 ObjBiBooleanToByteFunction<T> memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Triple<T, Boolean, Boolean>, Byte> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (ObjBiBooleanToByteFunction<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.function.tri.obj.ObjBiCharToByteFunction.java

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

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

/**
 * Returns a memoized (caching) version of this {@link ObjBiDoubleToLongFunction}. 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 ava2s  .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 ObjBiDoubleToLongFunction}.
 * @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 ObjBiDoubleToLongFunction<T> memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Triple<T, Double, Double>, Long> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (ObjBiDoubleToLongFunction<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.function.tri.ThrowableTriByteFunction.java

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

From source file:at.gridtec.lambda4j.function.tri.ThrowableTriLongFunction.java

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

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

/**
 * Returns a memoized (caching) version of this {@link ObjBiBooleanToLongFunction}. 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  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 ObjBiBooleanToLongFunction}.
 * @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 ObjBiBooleanToLongFunction<T> memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Triple<T, Boolean, Boolean>, Long> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (ObjBiBooleanToLongFunction<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.function.tri.obj.ObjBiCharToLongFunction.java

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

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

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

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