
org.seppiko.commons.utils.ArrayUtil Maven / Gradle / Ivy
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.seppiko.commons.utils;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.Supplier;
/**
* Array Util
*
* @see Arrays
* @author Leonard Woo
*/
public class ArrayUtil {
private ArrayUtil() {}
/**
* Returns {@code true} if the provided reference is array,
* otherwise returns {@code false}.
*
* @param obj a reference to be checked against.
* @param reference type.
* @return {@code true} if the provided reference is array, otherwise {@code false}.
*/
public static boolean isArray(T obj) {
return Objects.nonNull(obj) && obj.getClass().isArray();
}
/**
* Returns {@code true} if the array is {@code null},
* otherwise returns {@code false}.
*
* @param array a reference to be checked against.
* @param reference type.
* @return {@code true} if the provided array is {@code null}, otherwise {@code false}.
*/
public static boolean isEmpty(T[] array) {
return (isNull(array) || array.length == 0);
}
/**
* Returns {@code true} if the provided reference is {@code null},
* otherwise returns {@code false}.
*
* @param obj a reference to be checked against {@code null}.
* @param reference type.
* @return {@code true} if the provided reference is {@code null}, otherwise {@code false}.
*/
public static boolean isNull(T[] obj) {
return null == obj;
}
/**
* Returns {@code true} if the provided reference is non-{@code null},
* otherwise returns {@code false}.
*
* @param obj a reference to be checked against {@code null}.
* @param reference type.
* @return {@code true} if the provided reference is non-{@code null}, otherwise {@code false}.
*/
public static boolean nonNull(T[] obj) {
return null != obj;
}
/**
* Swap T array index a and b.
*
* @param oriTs origin T array.
* @param a first index.
* @param b second index.
* @return swap result.
* @param Object type.
*/
public static T[] swap(T[] oriTs, int a, int b) {
T tmp = oriTs[a];
oriTs[a] = oriTs[b];
oriTs[b] = tmp;
return oriTs;
}
/**
* Get Supplier object with not null array.
*
* @param suppliers supplier array.
* @return object array.
* @throws NullPointerException supplier array is {@code null}.
*/
public static Object[] getSuppliers(Supplier
© 2015 - 2025 Weber Informatics LLC | Privacy Policy