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.obj.ObjDoubleToCharFunction.java

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

From source file:at.gridtec.lambda4j.function.bi.obj.ObjFloatToDoubleFunction.java

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

From source file:at.gridtec.lambda4j.function.bi.obj.ObjShortToDoubleFunction.java

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

From source file:at.gridtec.lambda4j.function.bi.obj.ObjDoubleToDoubleFunction.java

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

From source file:at.gridtec.lambda4j.function.bi.obj.ObjIntToDoubleFunction.java

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

From source file:at.gridtec.lambda4j.function.bi.obj.ObjBooleanToCharFunction.java

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

From source file:at.gridtec.lambda4j.function.bi.obj.ObjCharToDoubleFunction.java

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

From source file:at.gridtec.lambda4j.function.bi.obj.ObjBooleanToDoubleFunction.java

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

From source file:at.gridtec.lambda4j.operator.binary.ByteBinaryOperator.java

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

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