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.Arrays;
019import java.util.Objects;
020
021public class ArrayUtil {
022
023    private ArrayUtil() {}
024
025
026    /**
027     * 判断数组是否为空
028     *
029     * @param array 数组
030     * @param <T>   数组类型
031     * @return {@code true} 数组为空,{@code false} 数组不为空
032     */
033    public static <T> boolean isEmpty(T[] array) {
034        return array == null || array.length == 0;
035    }
036
037
038    /**
039     * 判断数组是否不为空
040     *
041     * @param array 数组
042     * @param <T>   数组类型
043     * @return {@code true} 数组不为空,{@code false} 数组为空
044     */
045    public static <T> boolean isNotEmpty(T[] array) {
046        return !isEmpty(array);
047    }
048
049
050    /**
051     * 合并两个数组为一个新的数组
052     *
053     * @param first  第一个数组
054     * @param second 第二个数组
055     * @param <T>
056     * @return 新的数组
057     */
058    public static <T> T[] concat(T[] first, T[] second) {
059        if (first == null && second == null) {
060            throw new IllegalArgumentException("not allow first and second are null.");
061        } else if (isEmpty(first) && second != null) {
062            return second;
063        } else if (isEmpty(second)) {
064            return first;
065        } else {
066            T[] result = Arrays.copyOf(first, first.length + second.length);
067            System.arraycopy(second, 0, result, first.length, second.length);
068            return result;
069        }
070    }
071
072
073    public static <T> T[] concat(T[] first, T[] second, T[] third, T[]... others) {
074        T[] results = concat(first, second);
075        results = concat(results, third);
076
077        if (others != null && others.length > 0) {
078            for (T[] other : others) {
079                results = concat(results, other);
080            }
081        }
082        return results;
083    }
084
085
086    /**
087     * 可变长参形式数组
088     *
089     * @param first  第一个数组
090     * @param second 第二个数组
091     * @param <T>
092     * @return 新的数组
093     */
094    @SafeVarargs
095    public static <T> T[] append(T[] first, T... second) {
096        if (first == null && second == null) {
097            throw new IllegalArgumentException("not allow first and second are null.");
098        } else if (isEmpty(first) && second != null) {
099            return second;
100        } else if (isEmpty(second)) {
101            return first;
102        } else {
103            T[] result = Arrays.copyOf(first, first.length + second.length);
104            System.arraycopy(second, 0, result, first.length, second.length);
105            return result;
106        }
107    }
108
109
110    /**
111     * 查看数组中是否包含某一个值
112     *
113     * @param arrays 数组
114     * @param object 用于检测的值
115     * @param <T>
116     * @return true 包含
117     */
118    public static <T> boolean contains(T[] arrays, T object) {
119        if (isEmpty(arrays)) {
120            return false;
121        }
122        for (T array : arrays) {
123            if (Objects.equals(array, object)) {
124                return true;
125            }
126        }
127        return false;
128    }
129
130
131}