com.gitee.starblues.loader.utils.ObjectUtils Maven / Gradle / Ivy
/**
* Copyright [2019-Present] [starBlues]
*
* 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 com.gitee.starblues.loader.utils;
import java.lang.reflect.Array;
import java.util.*;
/**
* object utils
*
* @author starBlues
* @since 3.0.0
* @version 3.0.0
*/
public class ObjectUtils {
private ObjectUtils(){}
public static boolean isEmpty(CharSequence cs) {
return cs == null || cs.length() == 0;
}
public static boolean isEmpty(Object obj) {
if (obj == null) {
return true;
}
if (obj instanceof Optional) {
return !((Optional>) obj).isPresent();
}
if (obj instanceof CharSequence) {
return ((CharSequence) obj).length() == 0;
}
if (obj.getClass().isArray()) {
return Array.getLength(obj) == 0;
}
if (obj instanceof Collection) {
return ((Collection>) obj).isEmpty();
}
if (obj instanceof Map) {
return ((Map, ?>) obj).isEmpty();
}
return false;
}
public static List toList(T... array){
if(array.length == 0){
return new ArrayList<>(0);
}
List list = new ArrayList<>(array.length);
for (T t : array) {
list.add(t);
}
return list;
}
public static T getFirst(Collection collection){
if(ObjectUtils.isEmpty(collection)){
return null;
}
for (T t : collection) {
return t;
}
return null;
}
}