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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

From source file:at.gridtec.lambda4j.predicate.bi.obj.ObjBooleanPredicate.java

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

From source file:at.gridtec.lambda4j.predicate.bi.obj.ObjCharPredicate.java

/**
 * Returns a memoized (caching) version of this {@link ObjCharPredicate}. 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  2 s .c o  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 ObjCharPredicate}.
 * @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 ObjCharPredicate<T> memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Pair<T, Character>, Boolean> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (ObjCharPredicate<T> & Memoized) (t, value) -> {
            final boolean returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Pair.of(t, value),
                        key -> test(key.getLeft(), key.getRight()));
            }
            return returnValue;
        };
    }
}