Last active
April 9, 2021 06:53
-
-
Save HydrangeaPurple/3306de3cb439a35c0a1c0bb228ccd57d to your computer and use it in GitHub Desktop.
判空工具类
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import java.lang.reflect.Array; | |
import java.util.Collection; | |
import java.util.Date; | |
import java.util.Map; | |
/** | |
* @author chengyq | |
* @date 2021年4月9日 | |
* @version V1.0 | |
*/ | |
public class ObjectIsNull { | |
public static <T> T requireNonNull(T obj, String message) { | |
if (ObjectIsNull.check(obj)) { | |
throw new NullPointerException(message); | |
} | |
return obj; | |
} | |
private ObjectIsNull() { | |
} | |
public static boolean check(Object obj) { | |
if (obj == null) { | |
return true; | |
} | |
if (obj instanceof CharSequence) { | |
return check(obj.toString()); | |
} | |
if (obj instanceof Double) { | |
return check((Double) obj); | |
} | |
if (obj instanceof Integer) { | |
return check((Integer) obj); | |
} | |
if (obj instanceof Long) { | |
return check((Long) obj); | |
} | |
if (obj instanceof Iterable) { | |
return check((Iterable<?>) obj); | |
} | |
if (obj instanceof Map) { | |
return check((Map<?, ?>) obj); | |
} | |
if (obj.getClass().isArray()) { | |
return checkArray(obj); | |
} else { | |
return false; | |
} | |
} | |
public static boolean check(Collection<?> c) { | |
return c == null || c.size() <= 0; | |
} | |
public static boolean check(Iterable<?> iterable) { | |
return iterable == null || !iterable.iterator().hasNext(); | |
} | |
public static boolean check(Date date) { | |
return date == null; | |
} | |
public static boolean check(Double db) { | |
return db == null || (db >= (-1e-6) && db <= (1e-6)); | |
} | |
public static boolean check(Long long1) { | |
return long1 == null || long1 == 0L || long1 == -1L; | |
} | |
public static boolean check(Integer integer) { | |
return integer == null || integer == 0 || integer == -1; | |
} | |
public static boolean check(String string) { | |
return string == null || string.trim().length() <= 0 || "null".equalsIgnoreCase(string.trim()); | |
} | |
public static boolean check(CharSequence cs) { | |
return cs == null || check(cs.toString()); | |
} | |
public static boolean check(Map<?, ?> map) { | |
return map == null || map.size() <= 0; | |
} | |
public static boolean checkArray(Object array) { | |
return array == null || Array.getLength(array) <= 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment