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
18 import java.util.AbstractMap;
19 import java.util.Map;
20 import java.util.Map.Entry;
21 import java.util.function.Function;
22
23 public class MapUtil {
24 /**
25 * A temporary workaround for Java 8 specific performance issue JDK-8161372 .<br>
26 * This class should be removed once we drop Java 8 support.
27 *
28 * @see <a href="https://bugs.openjdk.java.net/browse/JDK-8161372">https://bugs.openjdk.java.net/browse/JDK-8161372</a>
29 */
30 public static <K, V> V computeIfAbsent(Map<K, V> map, K key, Function<K, V> mappingFunction) {
31 V value = map.get(key);
32 if (value != null) {
33 return value;
34 }
35 return map.computeIfAbsent(key, mappingFunction);
36 }
37
38 /**
39 * Map.entry(key, value) alternative for Java 8.
40 */
41 public static <K, V> Entry<K, V> entry(K key, V value) {
42 return new AbstractMap.SimpleImmutableEntry<>(key, value);
43 }
44
45 private MapUtil() {
46 super();
47 }
48 }