MapUtil.java

  1. /*
  2.  *    Copyright 2009-2021 the original author or authors.
  3.  *
  4.  *    Licensed under the Apache License, Version 2.0 (the "License");
  5.  *    you may not use this file except in compliance with the License.
  6.  *    You may obtain a copy of the License at
  7.  *
  8.  *       http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *    Unless required by applicable law or agreed to in writing, software
  11.  *    distributed under the License is distributed on an "AS IS" BASIS,
  12.  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *    See the License for the specific language governing permissions and
  14.  *    limitations under the License.
  15.  */
  16. package org.apache.ibatis.util;

  17. import java.util.AbstractMap;
  18. import java.util.Map;
  19. import java.util.Map.Entry;
  20. import java.util.function.Function;

  21. public class MapUtil {
  22.   /**
  23.    * A temporary workaround for Java 8 specific performance issue JDK-8161372 .<br>
  24.    * This class should be removed once we drop Java 8 support.
  25.    *
  26.    * @see <a href="https://bugs.openjdk.java.net/browse/JDK-8161372">https://bugs.openjdk.java.net/browse/JDK-8161372</a>
  27.    */
  28.   public static <K, V> V computeIfAbsent(Map<K, V> map, K key, Function<K, V> mappingFunction) {
  29.     V value = map.get(key);
  30.     if (value != null) {
  31.       return value;
  32.     }
  33.     return map.computeIfAbsent(key, mappingFunction);
  34.   }

  35.   /**
  36.    * Map.entry(key, value) alternative for Java 8.
  37.    */
  38.   public static <K, V> Entry<K, V> entry(K key, V value) {
  39.     return new AbstractMap.SimpleImmutableEntry<>(key, value);
  40.   }

  41.   private MapUtil() {
  42.     super();
  43.   }
  44. }