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

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

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

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

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

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

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

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

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

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

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

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