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