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

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

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

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

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

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

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

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

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

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

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

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

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

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