package com.nnhx.common.util;
import com.xiaoleilu.hutool.collection.IterUtil;
import com.xiaoleilu.hutool.util.ArrayUtil;
import com.xiaoleilu.hutool.util.MapUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import java.util.*;
public class ObjectUtil {
public static final int ALL_NOT_EMPTY = 1;
public static final int ALL_EMPTY = 2;
public static final int MIXTURE_STATE = 3;
public static void main(String[] args) {
testUtils();
}
public static boolean isAllNotEmpty(boolean charBlankVerify, Object... objs){
if(ArrayUtil.isEmpty(objs)){
return false;
}
for (Object obj : objs) {
if (isEmpty(charBlankVerify, obj)){
return false;
}
}
return true;
}
public static boolean isAllEmpty(boolean charBlankVerify, Object... objs){
if(ArrayUtil.isEmpty(objs)){
return true;
}
for (Object obj : objs) {
if (!isEmpty(charBlankVerify, obj)){
return false;
}
}
return true;
}
public static boolean isMixtureState(boolean charBlankVerify, Object... objs){
return !isAllEmpty(charBlankVerify, objs) && !isAllNotEmpty(charBlankVerify, objs);
}
public static int getStatus(boolean charBlankVerify, Object... objs){
return isAllNotEmpty(charBlankVerify, objs) ? ALL_NOT_EMPTY : isAllEmpty(charBlankVerify, objs) ? ALL_EMPTY : MIXTURE_STATE;
}
public static boolean isEmpty(boolean charBlankVerify, Object obj) {
if (null == obj) {
return true;
}
if (obj instanceof CharSequence) {
return charBlankVerify ? StrUtil.isBlank((CharSequence) obj) : StrUtil.isEmpty((CharSequence) obj);
} else if (obj instanceof Map) {
return MapUtil.isEmpty((Map) obj);
} else if (obj instanceof Iterable) {
return IterUtil.isEmpty((Iterable) obj);
} else if (obj instanceof Iterator) {
return IterUtil.isEmpty((Iterator) obj);
} else if (ArrayUtil.isArray(obj)) {
return ArrayUtil.isEmpty(obj);
}else {
return null == obj;
}
}
private static void testUtils() {
List<Object> array1 = null;
List<Object> array2 = new ArrayList<>();
List<String> array3 = Arrays.asList("1","2","3");
List<Integer> array4 = Arrays.asList(1,2,3);
String str1 = null;
String str2 = "";
String str3 = "ss";
Integer int1 = null;
Integer int2 = 1;
Integer int3 = 2;
Map<Object,Object> map1 = null;
Map<Object,Object> map2 = new HashMap<>();
Map<Object,Object> map3 = new HashMap<>();
map3.put(null,null);
List<List<?>> lists1 = Arrays.asList(array1, array2,array3,array4);
List<?> lists2 = Arrays.asList(str1,str2,str3);
List<?> lists3 = Arrays.asList(int1,int2,int3);
List<?> lists4 = Arrays.asList(map1,map2,map3);
lists1 = lists1.subList(1,2);
lists1.forEach(w->{
lists2.forEach(y->{
lists4.forEach(z->{
System.out.println("----------------" + w + "\t\t" + y + "\t\t" + z);
boolean allNoBlank = isAllNotEmpty(true, w,y,z);
boolean allBlank = isAllEmpty(true, w,y,z);
boolean noAllBlank = isMixtureState(true, w,y,z);
System.out.println("全部不为空:\t" + allNoBlank);
System.out.println("全部为空:\t" + allBlank);
System.out.println("不全部为空:\t" + noAllBlank);
});
});
});
System.out.println();
}
}