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

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

From source file:at.gridtec.lambda4j.function.bi.conversion.ThrowableBiBooleanToShortFunction.java

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

From source file:at.gridtec.lambda4j.predicate.tri.TriFloatPredicate.java

/**
 * Returns a memoized (caching) version of this {@link TriFloatPredicate}. 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 a2s . com
 * 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 TriFloatPredicate}.
 * @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 TriFloatPredicate memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Triple<Float, Float, Float>, Boolean> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (TriFloatPredicate & Memoized) (value1, value2, value3) -> {
            final boolean returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Triple.of(value1, value2, value3),
                        key -> test(key.getLeft(), key.getMiddle(), key.getRight()));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.predicate.tri.TriShortPredicate.java

/**
 * Returns a memoized (caching) version of this {@link TriShortPredicate}. 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 2s.  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 TriShortPredicate}.
 * @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 TriShortPredicate memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Triple<Short, Short, Short>, Boolean> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (TriShortPredicate & Memoized) (value1, value2, value3) -> {
            final boolean returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Triple.of(value1, value2, value3),
                        key -> test(key.getLeft(), key.getMiddle(), key.getRight()));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.function.bi.conversion.ThrowableBiBooleanToCharFunction.java

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

From source file:at.gridtec.lambda4j.function.bi.conversion.ThrowableBiByteToDoubleFunction.java

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

From source file:at.gridtec.lambda4j.function.bi.conversion.ThrowableBiDoubleToByteFunction.java

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

From source file:at.gridtec.lambda4j.function.bi.conversion.ThrowableBiDoubleToLongFunction.java

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

From source file:at.gridtec.lambda4j.function.bi.conversion.ThrowableBiFloatToShortFunction.java

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

From source file:at.gridtec.lambda4j.function.bi.conversion.ThrowableBiIntToFloatFunction.java

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