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:com.hp.octane.integrations.services.vulnerabilities.VulnerabilitiesServiceFunctionalityTest.java

private Map<String, OctaneSPEndpointSimulator> initSPEPSimulators(Set<String> spIDs,
        Map<String, List<String>> preflightRequestsCollectors,
        Map<String, List<String>> pushRequestCollectors) {
    Map<String, OctaneSPEndpointSimulator> result = new LinkedHashMap<>();

    for (String spID : spIDs) {
        OctaneSPEndpointSimulator simulator = OctaneSPEndpointSimulator.addInstance(spID);

        //  vulnerabilities preflight API
        simulator.installApiHandler(HttpMethod.GET, "^.*/vulnerabilities/preflight$", request -> {
            try {
                //  retrieve query parameters
                request.mergeQueryParameters("", request.getQueryString(), false);
                preflightRequestsCollectors.computeIfAbsent(spID, sid -> new LinkedList<>())
                        .add(request.getQueryParameters().getString("instance-id") + "|"
                                + request.getQueryParameters().getString("job-ci-id") + "|"
                                + request.getQueryParameters().getString("build-ci-id"));
                request.getResponse().setStatus(HttpStatus.SC_OK);
                request.getResponse().getWriter()
                        .write(request.getQueryParameters().getString("job-ci-id").contains("true") ? "true"
                                : "false");
                request.getResponse().getWriter().flush();
            } catch (IOException ioe) {
                throw new OctaneSDKGeneralException("failed to write response", ioe);
            }//w ww .j a va  2  s. c o  m
        });

        //  vulnerabilities push API
        simulator.installApiHandler(HttpMethod.POST, "^.*/vulnerabilities$", request -> {
            try {
                String rawVulnerabilitiesBody = CIPluginSDKUtils
                        .inputStreamToUTF8String(new GZIPInputStream(request.getInputStream()));
                pushRequestCollectors.computeIfAbsent(spID, sid -> new LinkedList<>())
                        .add(rawVulnerabilitiesBody);
                request.getResponse().setStatus(HttpStatus.SC_ACCEPTED);
                request.getResponse().getWriter().write("{\"status\": \"queued\"}");
                request.getResponse().getWriter().flush();
            } catch (IOException ioe) {
                throw new OctaneSDKGeneralException("failed to write response", ioe);
            }
        });

        result.put(spID, simulator);
    }

    return result;
}

From source file:com.streamsets.pipeline.stage.processor.tensorflow.TensorFlowProcessor.java

private Map<Pair<String, Integer>, Tensor> convertRecord(Record r, List<TensorInputConfig> inputConfigs)
        throws OnRecordErrorException {
    Map<Pair<String, Integer>, Buffer> inputBuffer = new LinkedHashMap<>();
    int numberOfRecords = 1;
    for (TensorInputConfig inputConfig : inputConfigs) {
        Pair<String, Integer> key = Pair.of(inputConfig.operation, inputConfig.index);
        long[] inputSize = (conf.useEntireBatch) ? new long[] { 1, numberOfRecords, inputConfig.fields.size() }
                : new long[] { numberOfRecords, inputConfig.fields.size() };

        TensorDataTypeSupport dtSupport = TensorTypeSupporter.INSTANCE
                .getTensorDataTypeSupport(inputConfig.tensorDataType);
        Buffer b = inputBuffer.computeIfAbsent(key, k -> dtSupport.allocateBuffer(inputSize));

        writeRecord(r, inputConfig.fields, b, dtSupport);
    }//w ww .  ja  v  a2  s.  c o m
    return createInputTensor(inputBuffer, numberOfRecords);
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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