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
024    /**
025     * 判断数组是否为空
026     *
027     * @param array
028     * @param <T>
029     * @return 空 true
030     */
031    public static <T> boolean isEmpty(T[] array) {
032        return array == null || array.length == 0;
033    }
034
035
036    /**
037     * 判断数组是否不为空
038     *
039     * @param array
040     * @param <T>
041     * @return
042     */
043    public static <T> boolean isNotEmpty(T[] array) {
044        return !isEmpty(array);
045    }
046
047
048    /**
049     * 合并两个数组为一个新的数组
050     *
051     * @param first  第一个数组
052     * @param second 第二个数组
053     * @param <T>
054     * @return 新的数组
055     */
056    public static <T> T[] concat(T[] first, T[] second) {
057        if (first == null && second == null) {
058            throw new IllegalArgumentException("not allow first and second are null.");
059        } else if (isEmpty(first)) {
060            return second;
061        } else if (isEmpty(second)) {
062            return first;
063        } else {
064            T[] result = Arrays.copyOf(first, first.length + second.length);
065            System.arraycopy(second, 0, result, first.length, second.length);
066            return result;
067        }
068    }
069
070
071    public static <T> T[] concat(T[] first, T[] second, T[] third, T[]... others) {
072        T[] results = concat(first, second);
073        results = concat(results, third);
074
075        if (others != null && others.length > 0) {
076            for (T[] other : others) {
077                results = concat(results, other);
078            }
079        }
080        return results;
081    }
082
083
084    /**
085     * 查看数组中是否包含某一个值
086     *
087     * @param arrays 数组
088     * @param object 用于检测的值
089     * @param <T>
090     * @return true 包含
091     */
092    public static <T> boolean contains(T[] arrays, T object) {
093        if (isEmpty(arrays)) {
094            return false;
095        }
096        for (T array : arrays) {
097            if (Objects.equals(array, object)) {
098                return true;
099            }
100        }
101        return false;
102    }
103
104
105}