001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (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 io.jboot.utils;
017
018import java.util.*;
019
020
021public class ArrayUtil {
022
023    public static boolean isNotEmpty(Collection<?> list) {
024        return list != null && list.size() > 0;
025    }
026
027    public static boolean isNotEmpty(Map map) {
028        return map != null && map.size() > 0;
029    }
030
031    public static boolean isNotEmpty(Object[] objects) {
032        return objects != null && objects.length > 0;
033    }
034
035    public static boolean isNullOrEmpty(Collection<?> list) {
036        return list == null || list.size() == 0;
037    }
038
039    public static boolean isNullOrEmpty(Map map) {
040        return map == null || map.size() == 0;
041    }
042
043    public static boolean isNullOrEmpty(Object[] objects) {
044        return objects == null || objects.length == 0;
045    }
046
047    public static <T> T[] concat(T[] first, T[]... rest) {
048        int totalLength = first.length;
049        for (T[] array : rest) {
050            totalLength += array.length;
051        }
052        T[] result = Arrays.copyOf(first, totalLength);
053        int offset = first.length;
054        for (T[] array : rest) {
055            System.arraycopy(array, 0, result, offset, array.length);
056            offset += array.length;
057        }
058        return result;
059    }
060
061    public static <T> boolean contains(T[] array, T element) {
062        if (array == null || array.length == 0) {
063            return false;
064        }
065
066        for (T t : array) {
067            if (Objects.equals(t, element)) {
068                return true;
069            }
070        }
071        return false;
072    }
073
074    public static boolean isSameElements(Collection<?> c1, Collection<?> c2) {
075        if (c1 == c2) {
076            return true;
077        }
078
079        if ((c1 == null || c1.isEmpty()) && (c2 == null || c2.isEmpty())) {
080            return true;
081        }
082
083        if (c1 != null && c2 != null && c1.size() == c2.size() && c1.containsAll(c2)) {
084            return true;
085        }
086
087        return false;
088    }
089
090
091    public static String toString(Object[] strings, String delimiter) {
092        StringJoiner sb = new StringJoiner(delimiter);
093        if (strings != null) {
094            for (Object o : strings) {
095                sb.add(String.valueOf(o));
096            }
097        }
098        return sb.toString();
099    }
100
101}