关注我,升职加薪就是你!
小伙伴们,你们是怎么避免空指针问题的?
下面是我就这方面问题做的一个通用工具类DataUtil。
package com.demo.nacosprovider.common;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
/**
* @Author: Paul
* @Date: 2021/10/07
* @Function: 通用对象工具类
*/
public class DataUtil {
/**
*@author: Paul
*@time: 2021/10/07
*@description: 判断类型是否是基本类型
*@param: [clazz]
*@return: boolean
*/
public static boolean isBaseType(Class<?> clazz) {
// java基本类型对象
if (clazz.isPrimitive()) {
return true;
}
// java.lang下对象不处理
if (clazz.getName().startsWith("java.lang.")) {
return true;
}
return false;
}
/**
*@author: Paul
*@time: 2021/10/07
*@description: 判断对象是否为空
*@param: [object]
*@return: boolean
*/
public static boolean isEmpty(Object object) {
if (object == null)
return true;
// 字符串
if (object instanceof String) {
return "".equals(object.toString());
}
// 集合
else if (object instanceof Collection<?>) {
return ((Collection<?>) object).isEmpty();
}
// 数组
else if (object instanceof Object[]) {
return ((Object[]) object).length == 0;
}
// Map
else if (object instanceof Map<?, ?>) {
return ((Map<?, ?>) object).isEmpty();
}
return false;
}
/**
*@author: Paul
*@time: 2021/10/07
*@description: 判断对象不为空
*@param: [value]
*@return: boolean
*/
public static boolean isNotEmpty(Object value) {
return !isEmpty(value);
}
public static String toString(Object object) {
if (object == null)
return null;
String value = null;
if (object instanceof java.util.Date) {
value = DateUtils.formatString((java.util.Date) object, DateUtils.FULL_DATE_STR);
} else if (object instanceof String[]) {
value = toString((String[]) object);
} else if (object instanceof java.lang.Throwable) {
value = toString((Throwable) object);
} else {
value = object.toString();
}
return value;
}
public static String toString(String[] array) {
if (array == null)
return null;
if (array.length == 0)
return "";
String value = null;
for (String s : array) {
if (isEmpty(s))
value = (value == null) ? s : value + "," + s;
}
return value;
}
public static String toString(Throwable e) {
if (e == null)
return null;
ByteArrayOutputStream os = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(os));
String value = os.toString();
return value;
}
/**
*@author: Paul
*@time: 2021/10/07
*@description: 将字符串转为数值类型,非数值转为默认值
*@param: [value, defaultValue]
*@return: java.math.BigDecimal
*/
public static BigDecimal getBigDecimal(String value, Object defaultValue) {
BigDecimal result = null;
try {
result = new BigDecimal(value);
} catch (Exception e) {
result = new BigDecimal(defaultValue.toString());
}
return result;
}
/**
*@author: Paul
*@time: 2021/10/07
*@description: 判断不是所有参数全部为空。true 只要存在某个参数不为空, 否则false
*@param: [values]
*@return: boolean
*/
public static boolean isNotAllEmpty(Object... values) {
// 不需要校验参数
if (values == null || values.length == 0) {
return true;
}
return Arrays.asList(values).stream().anyMatch(s -> isNotEmpty(s));
}
/**
*@author: Paul
*@time: 2021/10/07
*@description: 判断不是所有参数全部为空
*@param: [values]
*@return: boolean
*/
public static boolean isAllEmpty(Object... values) {
return !isNotAllEmpty(values);
}
/**
*@author: Paul
*@time: 2021/10/07
*@description: 参数解码。当解码异常时,将返回输入参数
*@param: [value]
*@return: java.lang.String
*/
public static String decode(String value) {
if (isEmpty(value)) {
return value;
}
try {
return URLDecoder.decode(value, "UTF-8");
} catch (Exception e) {
return value;
}
}
/**
*@author: Paul
*@time: 2021/10/07
*@description: 参数URL编码,如果编码失败。将返回输入参数
*@param: [value]
*@return: java.lang.String
*/
public static String encode(String value) {
if (isEmpty(value)) {
return value;
}
try {
return URLEncoder.encode(value, "UTF-8");
} catch (Exception e) {
return value;
}
}
/**
*@author: Paul
*@time: 2021/10/07
*@description: 合并2个数组,如果为null将返回一个长度为0的数组。
*@param: [a, b]
*@return: java.lang.String[]
*/
public static String[] combine(String[] a, String[] b) {
// 同时为空,返回长度为0的数组
if (a == null && b == null) {
return new String[0];
}
// a 和 b 其中一个为空,返回另一个数组
if (a == null) {
return b;
}
if (b == null) {
return a;
}
int al = a == null ? 0 : a.length;
int bl = b == null ? 0 : b.length;
String[] c = new String[al + bl];
int index = 0;
for (int i = 0; i < a.length; i++) {
c[index++] = a[i];
}
for (int i = 0; i < b.length; i++) {
c[index++] = b[i];
}
return c;
}
/**
*@author: Paul
*@time: 2021/10/07
*@description: 返回Boolean的值。如果value为空时,将返回false
*@param: [value]
*@return: boolean
*/
public static boolean isTrue(Boolean value) {
return value == null ? false : value;
}
/**
*@author: Paul
*@time: 2021/10/07
*@description: 判断数组是否包括指定的值
*@param: [value, values]
*@return: boolean
*/
public static boolean contain(Object value,Object[] values) {
if(DataUtil.isEmpty(value)) {
return false;
}
if(DataUtil.isEmpty(values)) {
return false;
}
//比较值是否相等
for(Object v : values) {
if(eq(v, value)) {
return true;
}
}
return false;
}
public static boolean eq(Object value1,Object value2) {
if(value1 == null || value2 == null) {
return false;
}
return value1.equals(value2);
}
}
完事啦。
关注我,升职加薪就是你!