001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.hbase.util;
020
021import java.io.IOException;
022import java.util.concurrent.ConcurrentMap;
023import java.util.function.Supplier;
024
025import org.apache.yetus.audience.InterfaceAudience;
026
027/**
028 * Utility methods for dealing with Collections, including treating null collections as empty.
029 */
030@InterfaceAudience.Private
031public class ConcurrentMapUtils {
032
033  /**
034   * In HBASE-16648 we found that ConcurrentHashMap.get is much faster than computeIfAbsent if the
035   * value already exists. Notice that the implementation does not guarantee that the supplier will
036   * only be executed once.
037   */
038  public static <K, V> V computeIfAbsent(ConcurrentMap<K, V> map, K key, Supplier<V> supplier) {
039    return computeIfAbsent(map, key, supplier, () -> {
040    });
041  }
042
043  /**
044   * A supplier that throws IOException when get.
045   */
046  @FunctionalInterface
047  public interface IOExceptionSupplier<V> {
048    V get() throws IOException;
049  }
050
051  /**
052   * In HBASE-16648 we found that ConcurrentHashMap.get is much faster than computeIfAbsent if the
053   * value already exists. So here we copy the implementation of
054   * {@link ConcurrentMap#computeIfAbsent(Object, java.util.function.Function)}. It uses get and
055   * putIfAbsent to implement computeIfAbsent. And notice that the implementation does not guarantee
056   * that the supplier will only be executed once.
057   */
058  public static <K, V> V computeIfAbsentEx(ConcurrentMap<K, V> map, K key,
059      IOExceptionSupplier<V> supplier) throws IOException {
060    V v, newValue;
061    return ((v = map.get(key)) == null && (newValue = supplier.get()) != null
062        && (v = map.putIfAbsent(key, newValue)) == null) ? newValue : v;
063  }
064
065  public static <K, V> V computeIfAbsent(ConcurrentMap<K, V> map, K key, Supplier<V> supplier,
066      Runnable actionIfAbsent) {
067    V v = map.get(key);
068    if (v != null) {
069      return v;
070    }
071    V newValue = supplier.get();
072    v = map.putIfAbsent(key, newValue);
073    if (v != null) {
074      return v;
075    }
076    actionIfAbsent.run();
077    return newValue;
078  }
079}