List of usage examples for java.util Map computeIfAbsent
default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)
From source file:at.gridtec.lambda4j.function.bi.conversion.ThrowableBiIntToShortFunction.java
/** * Returns a memoized (caching) version of this {@link ThrowableBiIntToShortFunction}. 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 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 ThrowableBiIntToShortFunction}. * @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 ThrowableBiIntToShortFunction<X> memoized() { if (isMemoized()) { return this; } else { final Map<Pair<Integer, Integer>, Short> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (ThrowableBiIntToShortFunction<X> & Memoized) (value1, value2) -> { final short returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Pair.of(value1, value2), ThrowableFunction.of(key -> applyAsShortThrows(key.getLeft(), key.getRight()))); } return returnValue; }; } }
From source file:at.gridtec.lambda4j.function.bi.conversion.ThrowableBiLongToDoubleFunction.java
/** * Returns a memoized (caching) version of this {@link ThrowableBiLongToDoubleFunction}. 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 av 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 ThrowableBiLongToDoubleFunction}. * @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 ThrowableBiLongToDoubleFunction<X> memoized() { if (isMemoized()) { return this; } else { final Map<Pair<Long, Long>, Double> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (ThrowableBiLongToDoubleFunction<X> & Memoized) (value1, value2) -> { final double returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Pair.of(value1, value2), ThrowableFunction.of(key -> applyAsDoubleThrows(key.getLeft(), key.getRight()))); } return returnValue; }; } }
From source file:at.gridtec.lambda4j.function.bi.conversion.ThrowableBiShortToFloatFunction.java
/** * Returns a memoized (caching) version of this {@link ThrowableBiShortToFloatFunction}. 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 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 ThrowableBiShortToFloatFunction}. * @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 ThrowableBiShortToFloatFunction<X> memoized() { if (isMemoized()) { return this; } else { final Map<Pair<Short, Short>, Float> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (ThrowableBiShortToFloatFunction<X> & Memoized) (value1, value2) -> { final float returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Pair.of(value1, value2), ThrowableFunction.of(key -> applyAsFloatThrows(key.getLeft(), key.getRight()))); } return returnValue; }; } }
From source file:at.gridtec.lambda4j.function.bi.conversion.ThrowableBiDoubleToIntFunction.java
/** * Returns a memoized (caching) version of this {@link ThrowableBiDoubleToIntFunction}. 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 ava 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 ThrowableBiDoubleToIntFunction}. * @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 ThrowableBiDoubleToIntFunction<X> memoized() { if (isMemoized()) { return this; } else { final Map<Pair<Double, Double>, Integer> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (ThrowableBiDoubleToIntFunction<X> & Memoized) (value1, value2) -> { final int returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Pair.of(value1, value2), ThrowableFunction.of(key -> applyAsIntThrows(key.getLeft(), key.getRight()))); } return returnValue; }; } }
From source file:at.gridtec.lambda4j.function.bi.conversion.ThrowableBiBooleanToDoubleFunction.java
/** * Returns a memoized (caching) version of this {@link ThrowableBiBooleanToDoubleFunction}. 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 ava 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 ThrowableBiBooleanToDoubleFunction}. * @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 ThrowableBiBooleanToDoubleFunction<X> memoized() { if (isMemoized()) { return this; } else { final Map<Pair<Boolean, Boolean>, Double> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (ThrowableBiBooleanToDoubleFunction<X> & Memoized) (value1, value2) -> { final double returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Pair.of(value1, value2), ThrowableFunction.of(key -> applyAsDoubleThrows(key.getLeft(), key.getRight()))); } return returnValue; }; } }
From source file:at.gridtec.lambda4j.function.bi.conversion.ThrowableBiCharToByteFunction.java
/** * Returns a memoized (caching) version of this {@link ThrowableBiCharToByteFunction}. 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 ThrowableBiCharToByteFunction}. * @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 ThrowableBiCharToByteFunction<X> memoized() { if (isMemoized()) { return this; } else { final Map<Pair<Character, Character>, Byte> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (ThrowableBiCharToByteFunction<X> & Memoized) (value1, value2) -> { final byte returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Pair.of(value1, value2), ThrowableFunction.of(key -> applyAsByteThrows(key.getLeft(), key.getRight()))); } return returnValue; }; } }
From source file:at.gridtec.lambda4j.function.bi.conversion.ThrowableBiCharToLongFunction.java
/** * Returns a memoized (caching) version of this {@link ThrowableBiCharToLongFunction}. 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 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 ThrowableBiCharToLongFunction}. * @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 ThrowableBiCharToLongFunction<X> memoized() { if (isMemoized()) { return this; } else { final Map<Pair<Character, Character>, Long> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (ThrowableBiCharToLongFunction<X> & Memoized) (value1, value2) -> { final long returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Pair.of(value1, value2), ThrowableFunction.of(key -> applyAsLongThrows(key.getLeft(), key.getRight()))); } return returnValue; }; } }
From source file:at.gridtec.lambda4j.function.bi.conversion.ThrowableBiFloatToCharFunction.java
/** * Returns a memoized (caching) version of this {@link ThrowableBiFloatToCharFunction}. 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 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 ThrowableBiFloatToCharFunction}. * @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 ThrowableBiFloatToCharFunction<X> memoized() { if (isMemoized()) { return this; } else { final Map<Pair<Float, Float>, Character> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (ThrowableBiFloatToCharFunction<X> & Memoized) (value1, value2) -> { final char returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Pair.of(value1, value2), ThrowableFunction.of(key -> applyAsCharThrows(key.getLeft(), key.getRight()))); } return returnValue; }; } }
From source file:at.gridtec.lambda4j.function.bi.conversion.ThrowableBiIntToCharFunction.java
/** * Returns a memoized (caching) version of this {@link ThrowableBiIntToCharFunction}. 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 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 ThrowableBiIntToCharFunction}. * @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 ThrowableBiIntToCharFunction<X> memoized() { if (isMemoized()) { return this; } else { final Map<Pair<Integer, Integer>, Character> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (ThrowableBiIntToCharFunction<X> & Memoized) (value1, value2) -> { final char returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Pair.of(value1, value2), ThrowableFunction.of(key -> applyAsCharThrows(key.getLeft(), key.getRight()))); } return returnValue; }; } }
From source file:com.github.s4ke.moar.json.MoarJSONSerializer.java
public static MoaPattern fromJSON(String jsonString) { JSONObject jsonObject = new JSONObject(jsonString); Moa moa = new Moa(); Map<String, Variable> variables = new HashMap<>(); {//from w w w.ja v a 2 s . c om JSONArray varsArray = jsonObject.optJSONArray("vars"); if (varsArray != null) { for (int i = 0; i < varsArray.length(); ++i) { String variableName = varsArray.getString(i); variables.put(variableName, new Variable(variableName)); } } } moa.setVariables(variables); EdgeGraph edgeGraph = new EdgeGraph(); edgeGraph.addState(Moa.SRC); edgeGraph.addState(Moa.SNK); { JSONArray statesArray = jsonObject.getJSONArray("states"); for (int i = 0; i < statesArray.length(); ++i) { JSONObject stateObj = statesArray.getJSONObject(i); int idx = stateObj.getInt("idx"); State state; if (stateObj.has("name")) { state = new BasicState(idx, stateObj.getString("name")); } else if (stateObj.has("ref")) { state = new VariableState(idx, stateObj.getString("ref")); } else if (stateObj.has("bound")) { String boundIdent = stateObj.getString("bound"); state = new BoundState(idx, boundIdent, BoundConstants.getFN(boundIdent)); } else if (stateObj.has("set")) { //TODO: this is a bit hacky, maybe do this better? String setString = stateObj.getString("set"); CodePointSet setDescriptor; if (setString.startsWith("[")) { setDescriptor = buildFnFromSetExpression(setString); } else { setDescriptor = CharacterClassesUtils.getFn(setString); } state = new SetState(idx, 1, setDescriptor, stateObj.getString("set")); } else { throw new IllegalArgumentException( "state must have either name, ref, bound or set in the JSON string"); } edgeGraph.addState(state); } } { JSONArray edgesArray = jsonObject.getJSONArray("edges"); Map<State, Collection<EdgeGraph.Edge>> edges = new HashMap<>(); for (int i = 0; i < edgesArray.length(); ++i) { State fromState; State toState; Set<MemoryAction> memoryActionSet = new HashSet<>(); { JSONObject edgeObj = edgesArray.getJSONObject(i); { int from = edgeObj.getInt("from"); fromState = edgeGraph.getState(from); if (fromState == null) { throw new IllegalArgumentException("a state with idx " + from + " does not exist"); } int to = edgeObj.getInt("to"); toState = edgeGraph.getState(to); if (toState == null) { throw new IllegalArgumentException("a state with idx " + to + " does not exist"); } } JSONArray memoryActions = edgeObj.optJSONArray("memoryActions"); if (memoryActions != null) { for (int j = 0; j < memoryActions.length(); ++j) { String memoryActionString = memoryActions.getString(j); MoaMatcher matcher = MEMORY_ACTION_PATTERN.matcher(memoryActionString); if (matcher.matches()) { String action = matcher.getVariableContent("action"); String variableName = matcher.getVariableContent("name"); if (!variables.containsKey(variableName)) { throw new IllegalArgumentException( "variable with name " + variableName + " does not exist"); } memoryActionSet.add(new MemoryAction(ActionType.fromString(action), variableName)); } else { throw new IllegalArgumentException( memoryActionString + " is no valid memoryAction string"); } } } } edges.computeIfAbsent(fromState, (state) -> new ArrayList<>()) .add(new EdgeGraph.Edge(memoryActionSet, toState)); } for (Map.Entry<State, Collection<EdgeGraph.Edge>> entry : edges.entrySet()) { edgeGraph.addEdgesWithDeterminismCheck(entry.getKey(), entry.getValue()); } } moa.setEdges(edgeGraph); moa.freeze(); String regexString = jsonObject.optString("regex"); return MoaPattern.build(moa, regexString); }