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.to.ToFloatBiFunction.java

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

From source file:at.gridtec.lambda4j.function.bi.to.ToShortBiFunction.java

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

From source file:at.gridtec.lambda4j.function.bi.to.ToLongBiFunction2.java

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

From source file:at.gridtec.lambda4j.function.bi.to.ToCharBiFunction.java

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

From source file:at.gridtec.lambda4j.function.bi.to.ToIntBiFunction2.java

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

From source file:at.gridtec.lambda4j.function.bi.to.ToDoubleBiFunction2.java

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

From source file:com.oneops.transistor.util.CloudUtil.java

private Map<String, TreeSet<String>> getMissingCloudServices(long manifestPlatCiId,
        Set<String> requiredServices) {
    Map<String, TreeSet<String>> missingCloud2Services = new TreeMap<>();
    //get clouds/*from  w  ww.j  ava2 s .  c o  m*/
    List<CmsRfcRelation> cloudRelations = getCloudsForPlatform(manifestPlatCiId);
    // get services for all clouds
    cloudRelations.forEach(cloudRelation -> {
        Set<String> cloudServices = getCloudServices(cloudRelation);
        String cloud = cloudRelation.getToRfcCi().getCiName();
        //check if service is configured
        requiredServices.stream().filter(s -> !cloudServices.contains(s))
                .forEach(s -> missingCloud2Services.computeIfAbsent(cloud, k -> new TreeSet<>()).add(s));
        logger.debug("cloud: " + cloud + " required services:: " + requiredServices.toString()
                + " missingServices " + missingCloud2Services.keySet());
    });

    return missingCloud2Services;
}

From source file:at.gridtec.lambda4j.function.bi.BiByteFunction.java

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

From source file:at.gridtec.lambda4j.function.bi.BiLongFunction.java

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

From source file:at.gridtec.lambda4j.function.bi.BiFloatFunction.java

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