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.tri.obj.BiObjByteToByteFunction.java

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

From source file:at.gridtec.lambda4j.function.tri.ThrowableTriCharFunction.java

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

From source file:at.gridtec.lambda4j.predicate.bi.obj.ObjBytePredicate.java

/**
 * Returns a memoized (caching) version of this {@link ObjBytePredicate}. 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  .  java  2 s.co m
 * Unless the predicate and therefore the used cache will be garbage-collected, it will keep all memoized values
 * forever.
 *
 * @return A memoized (caching) version of this {@code ObjBytePredicate}.
 * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the
 * resulting memoized predicate, as the cache used internally does not permit {@code null} keys or values.
 * @implNote The returned memoized predicate can be safely used concurrently from multiple threads which makes it
 * thread-safe.
 */
@Nonnull
default ObjBytePredicate<T> memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Pair<T, Byte>, Boolean> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (ObjBytePredicate<T> & Memoized) (t, value) -> {
            final boolean returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Pair.of(t, value),
                        key -> test(key.getLeft(), key.getRight()));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.function.tri.obj.BiObjLongToByteFunction.java

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

From source file:at.gridtec.lambda4j.function.tri.obj.BiObjLongToLongFunction.java

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

From source file:at.gridtec.lambda4j.predicate.bi.obj.ObjLongPredicate.java

/**
 * Returns a memoized (caching) version of this {@link ObjLongPredicate}. 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 predicate and therefore the used cache will be garbage-collected, it will keep all memoized values
 * forever.
 *
 * @return A memoized (caching) version of this {@code ObjLongPredicate}.
 * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the
 * resulting memoized predicate, as the cache used internally does not permit {@code null} keys or values.
 * @implNote The returned memoized predicate can be safely used concurrently from multiple threads which makes it
 * thread-safe.
 */
@Nonnull
default ObjLongPredicate<T> memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Pair<T, Long>, Boolean> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (ObjLongPredicate<T> & Memoized) (t, value) -> {
            final boolean returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Pair.of(t, value),
                        key -> test(key.getLeft(), key.getRight()));
            }
            return returnValue;
        };
    }
}

From source file:com.thinkbiganalytics.feedmgr.service.template.RegisteredTemplateService.java

/**
 * If a Template changes the Processor Names the Feed Properties will no longer be associated to the correct processors
 * This will match any feed properties to a whose processor name has changed in
 * the template to the template processor/property based upon the template processor type.
 *//*  www  . j av  a 2 s. c o  m*/
private void ensureFeedPropertiesExistInTemplate(FeedMetadata feed, RegisteredTemplate registeredTemplate) {
    Set<String> templateProcessors = registeredTemplate.getProperties().stream()
            .map(property -> property.getProcessorName()).collect(Collectors.toSet());

    //Store the template Properties
    Map<String, String> templateProcessorIdProcessorNameMap = new HashMap<>();
    Map<String, String> templateProcessorTypeProcessorIdMap = new HashMap<>();
    registeredTemplate.getProperties().stream()
            .filter(property -> !templateProcessorIdProcessorNameMap.containsKey(property.getProcessorId()))
            .forEach(property1 -> {
                templateProcessorIdProcessorNameMap.put(property1.getProcessorId(),
                        property1.getProcessorName());
                templateProcessorTypeProcessorIdMap.put(property1.getProcessorType(),
                        property1.getProcessorId());
            });

    Map<String, Map<String, NifiProperty>> templatePropertiesByProcessorIdMap = new HashMap<>();
    registeredTemplate.getProperties().stream().forEach(property -> {
        templatePropertiesByProcessorIdMap
                .computeIfAbsent(property.getProcessorId(), key -> new HashMap<String, NifiProperty>())
                .put(property.getKey(), property);
    });

    //store the Feed Properties
    Map<String, String> processorIdProcessorTypeMap = new HashMap<>();
    feed.getProperties().stream()
            .filter(property -> !processorIdProcessorTypeMap.containsKey(property.getProcessorId()))
            .forEach(property1 -> {
                processorIdProcessorTypeMap.put(property1.getProcessorId(), property1.getProcessorType());
            });

    feed.getProperties().stream().filter(property -> !templateProcessors.contains(property.getProcessorName()))
            .forEach(property -> {
                //if the property doesn't match the template but the type matches try to merge in the feed properties overwriting the template ones
                String processorType = processorIdProcessorTypeMap.get(property.getProcessorId());
                if (processorType != null) {
                    String templateProcessorId = templateProcessorTypeProcessorIdMap.get(processorType);

                    if (templateProcessorId != null
                            && templateProcessorIdProcessorNameMap.containsKey(templateProcessorId)) {
                        NifiProperty templateProperty = templatePropertiesByProcessorIdMap
                                .get(templateProcessorId).get(property.getKey());
                        if (templateProperty != null) {
                            templateProperty.setValue(property.getValue());
                            templateProperty.setRenderType(property.getRenderType());
                            templateProperty.setRenderOptions(property.getRenderOptions());
                        }
                    }
                }
            });

}

From source file:at.gridtec.lambda4j.function.tri.obj.BiObjByteToLongFunction.java

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

From source file:at.gridtec.lambda4j.predicate.bi.obj.ObjFloatPredicate.java

/**
 * Returns a memoized (caching) version of this {@link ObjFloatPredicate}. 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 va 2 s. co  m
 * Unless the predicate and therefore the used cache will be garbage-collected, it will keep all memoized values
 * forever.
 *
 * @return A memoized (caching) version of this {@code ObjFloatPredicate}.
 * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the
 * resulting memoized predicate, as the cache used internally does not permit {@code null} keys or values.
 * @implNote The returned memoized predicate can be safely used concurrently from multiple threads which makes it
 * thread-safe.
 */
@Nonnull
default ObjFloatPredicate<T> memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Pair<T, Float>, Boolean> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (ObjFloatPredicate<T> & Memoized) (t, value) -> {
            final boolean returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Pair.of(t, value),
                        key -> test(key.getLeft(), key.getRight()));
            }
            return returnValue;
        };
    }
}

From source file:at.gridtec.lambda4j.predicate.bi.obj.ObjIntPredicate.java

/**
 * Returns a memoized (caching) version of this {@link ObjIntPredicate}. 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 a  2 s  .  c o  m*/
 * Unless the predicate and therefore the used cache will be garbage-collected, it will keep all memoized values
 * forever.
 *
 * @return A memoized (caching) version of this {@code ObjIntPredicate}.
 * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the
 * resulting memoized predicate, as the cache used internally does not permit {@code null} keys or values.
 * @implNote The returned memoized predicate can be safely used concurrently from multiple threads which makes it
 * thread-safe.
 */
@Nonnull
default ObjIntPredicate<T> memoized() {
    if (isMemoized()) {
        return this;
    } else {
        final Map<Pair<T, Integer>, Boolean> cache = new ConcurrentHashMap<>();
        final Object lock = new Object();
        return (ObjIntPredicate<T> & Memoized) (t, value) -> {
            final boolean returnValue;
            synchronized (lock) {
                returnValue = cache.computeIfAbsent(Pair.of(t, value),
                        key -> test(key.getLeft(), key.getRight()));
            }
            return returnValue;
        };
    }
}