/* * Copyright (c) 2008-2017, Hazelcast, Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.hazelcast.util; import com.hazelcast.core.IBiFunction; import com.hazelcast.core.IFunction; /** * A {@link java.util.concurrent.ConcurrentMap} with applyIfAbsent function. * * <p>Memory consistency effects: As with other concurrent * collections, actions in a thread prior to placing an object into a * {@code ConcurrentMap} as a key or value * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> * actions subsequent to the access or removal of that object from * the {@code ConcurrentMap} in another thread. * * * @param <K> the type of keys maintained by this map * @param <V> the type of mapped values */ public interface IConcurrentMap<K, V> extends java.util.concurrent.ConcurrentMap<K, V> { /** * {@inheritDoc} * * @implSpec * The default implementation is equivalent to the following steps for this * {@code map}, then returning the current value or {@code null} if now * absent: * * <pre> {@code * if (map.get(key) == null) { * V newValue = mappingFunction.apply(key); * if (newValue != null) * return map.putIfAbsent(key, newValue); * } * }</pre> * * The default implementation may retry these steps when multiple * threads attempt updates including potentially calling the mapping * function multiple times. * * <p>This implementation assumes that the ConcurrentMap cannot contain null * values and {@code get()} returning null unambiguously means the key is * absent. Implementations which support null values <strong>must</strong> * override this default implementation. * * @throws UnsupportedOperationException {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ V applyIfAbsent(K key, IFunction<? super K, ? extends V> mappingFunction); /** * {@inheritDoc} * * @implSpec * The default implementation is equivalent to performing the following * steps for this {@code map}, then returning the current value or * {@code null} if now absent. : * * <pre> {@code * if (map.get(key) != null) { * V oldValue = map.get(key); * V newValue = remappingFunction.apply(key, oldValue); * if (newValue != null) * map.replace(key, oldValue, newValue); * else * map.remove(key, oldValue); * } * }</pre> * * The default implementation may retry these steps when multiple threads * attempt updates including potentially calling the remapping function * multiple times. * * <p>This implementation assumes that the ConcurrentMap cannot contain null * values and {@code get()} returning null unambiguously means the key is * absent. Implementations which support null values <strong>must</strong> * override this default implementation. * * @throws UnsupportedOperationException {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ V applyIfPresent(K key, IBiFunction<? super K, ? super V, ? extends V> mappingFunction); }