001/*
002 *  Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.mybatisflex.core.util;
017
018import java.util.*;
019import java.util.function.Function;
020
021
022public class CollectionUtil {
023
024    private CollectionUtil() {}
025
026
027    public static boolean isEmpty(Collection<?> collection) {
028        return collection == null || collection.isEmpty();
029    }
030
031
032    public static boolean isNotEmpty(Collection<?> collection) {
033        return !isEmpty(collection);
034    }
035
036
037    public static boolean isEmpty(Map<?, ?> map) {
038        return map == null || map.isEmpty();
039    }
040
041
042    public static boolean isNotEmpty(Map<?, ?> map) {
043        return !isEmpty(map);
044    }
045
046    /**
047     * 合并 list
048     */
049    public static <T> List<T> merge(List<T> list, List<T> other) {
050        if (list == null && other == null) {
051            return new ArrayList<>();
052        } else if (isEmpty(other) && list != null) {
053            return list;
054        } else if (isEmpty(list)) {
055            return other;
056        }
057        List<T> newList = new ArrayList<>(list);
058        newList.addAll(other);
059        return newList;
060    }
061
062
063    public static <K, V> HashMap<K, V> newHashMap() {
064        return new HashMap<>();
065    }
066
067    /**
068     * 主要是用于修复 concurrentHashMap 在 jdk1.8 下的死循环问题
069     *
070     * @see <a href="https://bugs.openjdk.org/browse/JDK-8161372">https://bugs.openjdk.org/browse/JDK-8161372</a>
071     */
072    public static <K, V> V computeIfAbsent(Map<K, V> concurrentHashMap, K key, Function<? super K, ? extends V> mappingFunction) {
073        V v = concurrentHashMap.get(key);
074        if (v != null) {
075            return v;
076        }
077        return concurrentHashMap.computeIfAbsent(key, mappingFunction);
078    }
079
080    public static <T> Set<T> newHashSet(T... elements) {
081        return new HashSet<>(Arrays.asList(elements));
082    }
083
084    public static <T> List<T> newArrayList(T... elements) {
085        return new ArrayList<>(Arrays.asList(elements));
086    }
087
088    public static <T> List<T> toList(Collection<T> collection) {
089        if (collection instanceof List) {
090            return (List<T>) collection;
091        } else {
092            return new ArrayList<>(collection);
093        }
094    }
095
096    public static String[] toArrayString(Collection<?> collection) {
097        if (isEmpty(collection)) {
098            return new String[0];
099        }
100        String[] results = new String[collection.size()];
101        int index = 0;
102        for (Object o : collection) {
103            results[index++] = String.valueOf(o);
104        }
105        return results;
106    }
107
108}