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 */
018package org.apache.hadoop.hbase.util;
019
020import java.io.IOException;
021import java.util.concurrent.ConcurrentMap;
022import java.util.function.Supplier;
023import org.apache.yetus.audience.InterfaceAudience;
024
025/**
026 * Utility methods for dealing with Collections, including treating null collections as empty.
027 */
028@InterfaceAudience.Private
029public class ConcurrentMapUtils {
030
031  /**
032   * In HBASE-16648 we found that ConcurrentHashMap.get is much faster than computeIfAbsent if the
033   * value already exists. Notice that the implementation does not guarantee that the supplier will
034   * only be executed once.
035   */
036  public static <K, V> V computeIfAbsent(ConcurrentMap<K, V> map, K key, Supplier<V> supplier) {
037    return computeIfAbsent(map, key, supplier, () -> {
038    });
039  }
040
041  /**
042   * A supplier that throws IOException when get.
043   */
044  @FunctionalInterface
045  public interface IOExceptionSupplier<V> {
046    V get() throws IOException;
047  }
048
049  /**
050   * In HBASE-16648 we found that ConcurrentHashMap.get is much faster than computeIfAbsent if the
051   * value already exists. So here we copy the implementation of
052   * {@link ConcurrentMap#computeIfAbsent(Object, java.util.function.Function)}. It uses get and
053   * putIfAbsent to implement computeIfAbsent. And notice that the implementation does not guarantee
054   * that the supplier will only be executed once.
055   */
056  public static <K, V> V computeIfAbsentEx(ConcurrentMap<K, V> map, K key,
057    IOExceptionSupplier<V> supplier) throws IOException {
058    V v, newValue;
059    return ((v = map.get(key)) == null && (newValue = supplier.get()) != null
060      && (v = map.putIfAbsent(key, newValue)) == null) ? newValue : v;
061  }
062
063  public static <K, V> V computeIfAbsent(ConcurrentMap<K, V> map, K key, Supplier<V> supplier,
064    Runnable actionIfAbsent) {
065    V v = map.get(key);
066    if (v != null) {
067      return v;
068    }
069    V newValue = supplier.get();
070    v = map.putIfAbsent(key, newValue);
071    if (v != null) {
072      return v;
073    }
074    actionIfAbsent.run();
075    return newValue;
076  }
077}