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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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