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.conversion.BiCharToDoubleFunction.java

/**
 * Returns a memoized (caching) version of this {@link BiCharToDoubleFunction}. 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 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 BiCharToDoubleFunction}.
 * @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 BiCharToDoubleFunction memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Pair<Character, Character>, Double> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (BiCharToDoubleFunction & Memoized) (value1, value2) -> {
            final double returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Pair.of(value1, value2),
                        key -> applyAsDouble(key.getLeft(), key.getRight()));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.function.bi.to.ThrowableToByteBiFunction.java

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

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

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

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

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

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

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

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

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

From source file:at.gridtec.lambda4j.function.bi.to.ThrowableToFloatBiFunction.java

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

From source file:at.gridtec.lambda4j.function.bi.to.ThrowableToShortBiFunction.java

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

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

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

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

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