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

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

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

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

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

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

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

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

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

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

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

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

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

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

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