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

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

/**
 * Returns a memoized (caching) version of this {@link BiIntFunction}. 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 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 BiIntFunction}.
 * @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 BiIntFunction<R> memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Pair<Integer, Integer>, R> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (BiIntFunction<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:com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplate.java

@JsonIgnore
public void initializeNonInputProcessors() {
    Map<String, Processor> processorMap = new HashMap<>();

    properties.stream().filter(property -> !property.isInputProperty()).forEach(property -> {
        processorMap.computeIfAbsent(property.getProcessorId(), processorId -> new Processor(processorId))
                .addProperty(property);//from w  w w .j  a  va  2 s. c  o m
    });
    nonInputProcessors = Lists.newArrayList(processorMap.values());
}

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

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

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

/**
 * Returns a memoized (caching) version of this {@link BiCharFunction}. 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 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 BiCharFunction}.
 * @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 BiCharFunction<R> memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Pair<Character, Character>, R> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (BiCharFunction<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:com.evolveum.midpoint.prism.marshaller.PrismBeanInspector.java

private <V, P1, P2> V find2(final Map<P1, Map<P2, V>> cache, final P1 param1, final P2 param2,
        final Getter2<V, P1, P2> getter) {
    Map<P2, V> cache2 = cache.computeIfAbsent(param1, k -> Collections.synchronizedMap(new HashMap<>()));
    return find1(cache2, param2, p -> getter.get(param1, p));
}

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

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

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

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

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

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