package com.yww.account.model.tools.util;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 一站式健全判空工具类(可判空 集合、字符串、对象 等各种特殊情况)
*
* @author daizhichao
* @date 2021/9/7
*/
public class EmptyUtil {
/**
* 对象是否为空
*
* @param o String,List,Map,Object[],int[],long[]
* @return
*/
@SuppressWarnings("rawtypes")
public static boolean isEmpty(Object o) {
if (o == null) {
return true;
}
if (o instanceof String) {
if ("".equals(o.toString().trim())) {
return true;
}
} else if (o instanceof List) {
if (((List) o).size() == 0) {
return true;
}
} else if (o instanceof Map) {
if (((Map) o).size() == 0) {
return true;
}
} else if (o instanceof Set) {
if (((Set) o).size() == 0) {
return true;
}
} else if (o instanceof Object[]) {
if (((Object[]) o).length == 0) {
return true;
}
} else if (o instanceof int[]) {
if (((int[]) o).length == 0) {
return true;
}
} else if (o instanceof long[]) {
if (((long[]) o).length == 0) {
return true;
}
}
return false;
}
public static boolean isEmpty(Object... o) {
boolean empty = false;
for (Object o1 : o) {
if (isEmpty(o1)) {
empty = true;
break;
}
}
return empty;
}
/**
* 对象是否不为空
*
* @param o String,List,Map,Object[],int[],long[]
* @return
*/
public static boolean isNotEmpty(Object o) {
return !isEmpty(o);
}
/**
* 对象组中是否存在 Empty Object
*
* @param os 对象组
* @return
*/
public static boolean isOneEmpty(Object... os) {
for (Object o : os) {
return isEmpty(o);
}
return false;
}
/**
* 对象组中是否全是 Empty Object
*
* @param os
* @return
*/
public static boolean isAllEmpty(Object... os) {
for (Object o : os) {
if (!isEmpty(o)) {
return false;
}
}
return true;
}
}
CopyUtil:
package com.topsec.base.utils;
import com.github.pagehelper.PageInfo;
import java.util.ArrayList;
import java.util.List;
/**
* bean复制list
*
* @author dzc
* @date 2022/3/22
* @description
*/
public class CopyUtil {
public static <T, E> List<E> copyList(List<T> source, Class<E> targetClass) {
List<E> es = new ArrayList<>();
for (T t : source) {
E e = null;
try {
e = targetClass.newInstance();
} catch (InstantiationException | IllegalAccessException e1) {
e1.printStackTrace();
}
BeanUtil.copyNotNullProperties(t, e);
es.add(e);
}
return es;
}
public static <T, E> PageInfo<E> copyPage(PageInfo<T> tPageInfo, Class<E> targetClass) {
PageInfo<E> ePageInfo = new PageInfo<>();
BeanUtil.copyNotNullProperties(tPageInfo, ePageInfo);
List<T> list = tPageInfo.getList();
List<E> es = copyList(list, targetClass);
ePageInfo.setList(es);
return ePageInfo;
}
}
BeanUtil:
package com.topsec.base.utils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.util.ObjectUtils;
import java.beans.FeatureDescriptor;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
/**
* @author dzc
*/
public class BeanUtil {
private static final String EMPTY_STRING = "";
private static final String NULL_STRING = "null";
/**
* 复制对象
*
* @param source
* @param target
*/
public static void copyProperties(Object source, Object target) {
BeanUtil.copyProperties(source, target, (String[]) null);
}
public static void copyNotNullProperties(Object source, Object target) {
String[] properties = getNullPropertyNames(source);
BeanUtil.copyProperties(source, target, properties);
}
/**
* @param source
* @param target
* @param ignoreProperties
*/
public static void copyProperties(Object source, Object target, String... ignoreProperties) {
BeanUtils.copyProperties(source, target, ignoreProperties);
}
/**
* 获取一个对象的空属性名
*
* @param source
* @return
*/
public static String[] getNullPropertyNames(Object source) {
final BeanWrapper wrappedSource = new BeanWrapperImpl(source);
return Stream.of(wrappedSource.getPropertyDescriptors())
.map(FeatureDescriptor::getName)
.filter(propertyName -> wrappedSource.getPropertyValue(propertyName) == null)
.toArray(String[]::new);
}
/**
* @param obj
* @return
*/
public static boolean isNull(Object obj) {
return obj == null;
}
/**
* null安全的类转字符串
*
* @param obj
* @return
*/
public static String nullSafeToString(Object obj) {
if (obj == null) {
return NULL_STRING;
}
if (obj instanceof String) {
return (String) obj;
}
if (obj instanceof Object[]) {
return nullSafeToString(obj);
}
if (obj instanceof boolean[]) {
return nullSafeToString(obj);
}
if (obj instanceof byte[]) {
return nullSafeToString(obj);
}
if (obj instanceof char[]) {
return nullSafeToString(obj);
}
if (obj instanceof double[]) {
return nullSafeToString(obj);
}
if (obj instanceof float[]) {
return nullSafeToString(obj);
}
if (obj instanceof int[]) {
return nullSafeToString(obj);
}
if (obj instanceof long[]) {
return nullSafeToString(obj);
}
if (obj instanceof short[]) {
return nullSafeToString(obj);
}
String str = obj.toString();
return (str != null ? str : EMPTY_STRING);
}
/**
* 是否是数组
*
* @param obj
* @return
*/
public static boolean isArray(Object obj) {
return (obj != null && obj.getClass().isArray());
}
/**
* 校验数组是否为空
*
* @param array
* @return
*/
public static boolean isEmpty(Object[] array) {
return ObjectUtils.isEmpty(array);
}
/**
* java实例对象转Map<String, Object>
*
* @param object java实例对象, Date对象会默认转成时间戳格式
* @return Map<String, Object>
*/
public static Map<String, Object> objectToStringKeyMap(Object object) {
String jsonStr = JSONObject.toJSONString(object);
return JSONObject.parseObject(jsonStr);
}
/**
* java实例对象转Map<String, Object>
*
* @param object java实例对象, Date对象按指定时间格式转换
* @param dateFormat 日期格式
* @return Map<String, Object>
*/
public static Map<String, Object> objectToStringKeyMap(Object object, String dateFormat) {
String jsonStr = JSONObject.toJSONStringWithDateFormat(object, dateFormat);
return JSONObject.parseObject(jsonStr);
}
/**
* java实例对象转Map<Object, Object>
*
* @param object java实例对象
* @return Map<Object, Object>
*/
public static Map<Object, Object> objectToObjectKeyMap(Object object) {
Map<Object, Object> map = new HashMap<>();
String jsonStr = JSONObject.toJSONString(object);
JSONObject jsonObject = JSONObject.parseObject(jsonStr);
jsonObject.keySet().forEach(key -> map.put(key, jsonObject.get(key)));
return map;
}
}