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) && second != null) { 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 first 第一个数组 088 * @param second 第二个数组 089 * @param <T> 090 * @return 新的数组 091 */ 092 @SafeVarargs 093 public static <T> T[] append(T[] first, T... second) { 094 if (first == null && second == null) { 095 throw new IllegalArgumentException("not allow first and second are null."); 096 } else if (isEmpty(first) && second != null) { 097 return second; 098 } else if (isEmpty(second)) { 099 return first; 100 } else { 101 T[] result = Arrays.copyOf(first, first.length + second.length); 102 System.arraycopy(second, 0, result, first.length, second.length); 103 return result; 104 } 105 } 106 107 108 /** 109 * 查看数组中是否包含某一个值 110 * 111 * @param arrays 数组 112 * @param object 用于检测的值 113 * @param <T> 114 * @return true 包含 115 */ 116 public static <T> boolean contains(T[] arrays, T object) { 117 if (isEmpty(arrays)) { 118 return false; 119 } 120 for (T array : arrays) { 121 if (Objects.equals(array, object)) { 122 return true; 123 } 124 } 125 return false; 126 } 127 128 129}