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

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

From source file:at.gridtec.lambda4j.operator.ternary.IntTernaryOperator.java

/**
 * Returns a memoized (caching) version of this {@link IntTernaryOperator}. 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>//  ww  w  . j a v  a  2  s  . com
 * Unless the operator and therefore the used cache will be garbage-collected, it will keep all memoized values
 * forever.
 *
 * @return A memoized (caching) version of this {@code IntTernaryOperator}.
 * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the
 * resulting memoized operator, as the cache used internally does not permit {@code null} keys or values.
 * @implNote The returned memoized operator can be safely used concurrently from multiple threads which makes it
 * thread-safe.
 */
@Nonnull
default IntTernaryOperator memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Triple<Integer, Integer, Integer>, Integer> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (IntTernaryOperator & Memoized) (value1, value2, value3) -> {
            final int returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Triple.of(value1, value2, value3),
                        key -> applyAsInt(key.getLeft(), key.getMiddle(), key.getRight()));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.function.tri.conversion.TriBooleanToCharFunction.java

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

From source file:at.gridtec.lambda4j.function.tri.conversion.TriCharToFloatFunction.java

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

From source file:at.gridtec.lambda4j.function.tri.conversion.TriCharToShortFunction.java

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

From source file:at.gridtec.lambda4j.function.tri.conversion.TriBooleanToDoubleFunction.java

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

From source file:at.gridtec.lambda4j.function.tri.conversion.TriCharToDoubleFunction.java

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

From source file:at.gridtec.lambda4j.operator.ternary.DoubleTernaryOperator.java

/**
 * Returns a memoized (caching) version of this {@link DoubleTernaryOperator}. 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  2 s  .co  m*/
 * Unless the operator and therefore the used cache will be garbage-collected, it will keep all memoized values
 * forever.
 *
 * @return A memoized (caching) version of this {@code DoubleTernaryOperator}.
 * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the
 * resulting memoized operator, as the cache used internally does not permit {@code null} keys or values.
 * @implNote The returned memoized operator can be safely used concurrently from multiple threads which makes it
 * thread-safe.
 */
@Nonnull
default DoubleTernaryOperator memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Triple<Double, Double, Double>, Double> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (DoubleTernaryOperator & Memoized) (value1, value2, value3) -> {
            final double returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Triple.of(value1, value2, value3),
                        key -> applyAsDouble(key.getLeft(), key.getMiddle(), key.getRight()));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.operator.ternary.CharTernaryOperator.java

/**
 * Returns a memoized (caching) version of this {@link CharTernaryOperator}. 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  a2 s .c o m*/
 * Unless the operator and therefore the used cache will be garbage-collected, it will keep all memoized values
 * forever.
 *
 * @return A memoized (caching) version of this {@code CharTernaryOperator}.
 * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the
 * resulting memoized operator, as the cache used internally does not permit {@code null} keys or values.
 * @implNote The returned memoized operator can be safely used concurrently from multiple threads which makes it
 * thread-safe.
 */
@Nonnull
default CharTernaryOperator memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Triple<Character, Character, Character>, Character> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (CharTernaryOperator & Memoized) (value1, value2, value3) -> {
            final char returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Triple.of(value1, value2, value3),
                        key -> applyAsChar(key.getLeft(), key.getMiddle(), key.getRight()));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.operator.ternary.BooleanTernaryOperator.java

/**
 * Returns a memoized (caching) version of this {@link BooleanTernaryOperator}. 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  ava  2  s  .co  m
 * Unless the operator and therefore the used cache will be garbage-collected, it will keep all memoized values
 * forever.
 *
 * @return A memoized (caching) version of this {@code BooleanTernaryOperator}.
 * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the
 * resulting memoized operator, as the cache used internally does not permit {@code null} keys or values.
 * @implNote The returned memoized operator can be safely used concurrently from multiple threads which makes it
 * thread-safe.
 */
@Nonnull
default BooleanTernaryOperator memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Triple<Boolean, Boolean, Boolean>, Boolean> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (BooleanTernaryOperator & Memoized) (value1, value2, value3) -> {
            final boolean returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Triple.of(value1, value2, value3),
                        key -> applyAsBoolean(key.getLeft(), key.getMiddle(), key.getRight()));
            }
            return returnValue;
        };
    }
}