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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/**
 * Returns a memoized (caching) version of this {@link BiFloatToDoubleFunction}. 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 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 BiFloatToDoubleFunction}.
 * @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 BiFloatToDoubleFunction memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Pair<Float, Float>, Double> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (BiFloatToDoubleFunction & 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.conversion.BiIntToCharFunction.java

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

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

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