Java tutorial
//package com.java2s; /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ import java.util.concurrent.ConcurrentMap; public class Main { /** * <p> * Puts a value in the specified {@code ConcurrentMap} if the key is not yet * present. This method works similar to the {@code putIfAbsent()} method of * the {@code ConcurrentMap} interface, but the value returned is different. * Basically, this method is equivalent to the following code fragment: * * <pre> * if (!map.containsKey(key)) { * map.put(key, value); * return value; * } else { * return map.get(key); * } * </pre> * * except that the action is performed atomically. So this method always * returns the value which is stored in the map. * </p> * <p> * This method is <b>null</b>-safe: It accepts a <b>null</b> map as input * without throwing an exception. In this case the return value is * <b>null</b>, too. * </p> * * @param <K> the type of the keys of the map * @param <V> the type of the values of the map * @param map the map to be modified * @param key the key of the value to be added * @param value the value to be added * @return the value stored in the map after this operation */ public static <K, V> V putIfAbsent(ConcurrentMap<K, V> map, K key, V value) { if (map == null) { return null; } V result = map.putIfAbsent(key, value); return result != null ? result : value; } }