/**
* å¤æ对象æ对象æ°ç»ä¸æ¯ä¸ä¸ªå¯¹è±¡æ¯å¦ä¸ºç©º: 对象为nullï¼å符åºåé¿åº¦ä¸º0ï¼éåç±»ãMap为empty
*
* @param obj
* @return
*/
public static boolean isNullOrEmpty(Object obj) {
if (obj == null)
return true;
if (obj instanceof CharSequence)
return ((CharSequence) obj).length() == 0;
if (obj instanceof Collection)
return ((Collection) obj).isEmpty();
if (obj instanceof Map)
return ((Map) obj).isEmpty();
if (obj instanceof Object[]) {
Object[] object = (Object[]) obj;
if (object.length == 0) {
return true;
}
boolean empty = true;
for (int i = 0; i < object.length; i++) {
if (!isNullOrEmpty(object[i])) {
empty = false;
break;
}
}
return empty;
}
return false;
}
åºç¨åºæ¯ï¼
读åexcelæ件ï¼è½¬å为ä¸ä¸ªäºç»´æ°ç»ï¼Object[][] arrays
ä½æ¯excelä¸æ空è¡ï¼æ以éè¦è¿æ»¤Object[][] arraysä¸ç空çä¸ç»´æ°ç»ï¼
Java代ç
/***
* è¿æ»¤æ°ç»ä¸ç空å
ç´
*
*
* @param arrays
* @return
*/
public static Object[][] filterEmpty(Object[][] arrays) {
int sumNotNull = 0;
/***
* ç»è®¡é空å
ç´ çæ»ä¸ªæ°
*/
for (int i = 0; i < arrays.length; i++) {
Object object = arrays[i];
if (!ValueWidget.isNullOrEmpty(object)
&& !SystemUtil.isNullOrEmpty((Object[]) object)) {// å¤æå
ç´ æ¯å¦ä¸ºç©º
sumNotNull = sumNotNull + 1;
}
}
Object[][] filtedObjs = new Object[sumNotNull][];
int index = 0;
for (int i = 0; i < arrays.length; i++) {
Object[] object_tmp = arrays[i];
if (!ValueWidget.isNullOrEmpty(object_tmp)
&& !SystemUtil.isNullOrEmpty((Object[]) object_tmp)) {// å¤æå
ç´ æ¯å¦ä¸ºç©º
filtedObjs[index++] = object_tmp;
}
}
return filtedObjs;
}
å¤æ对象çæææååéæ¯å¦ä¸ºç©º
Java代ç
/***
* Determine whether the object's fields are empty
*
* @param obj
* @param isExcludeZero :true:æ°å¼ç±»åçå¼ä¸º0,åå½å为空;----false:æ°å¼ç±»åçå¼ä¸º0,åä¸ä¸ºç©º
*
* @return
* @throws SecurityException
* @throws IllegalArgumentException
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
public static boolean isNullObject(Object obj, boolean isExcludeZero)
throws SecurityException, IllegalArgumentException,
NoSuchFieldException, IllegalAccessException {
if(ValueWidget.isNullOrEmpty(obj)){//对象æ¬èº«å°±ä¸ºnull
return true;
}
List<Field> fieldList = ReflectHWUtils.getAllFieldList(obj.getClass());
boolean isNull = true;
for (int i = 0; i < fieldList.size(); i++) {
Field f = fieldList.get(i);
Object propertyValue = null;
try {
propertyValue = getObjectValue(obj, f);
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
if (!ValueWidget.isNullOrEmpty(propertyValue)) {// å段ä¸ä¸ºç©º
if (propertyValue instanceof Integer) {// æ¯æ°å
if (!((Integer) propertyValue == 0 && isExcludeZero)) {
isNull = false;
break;
}
} else if (propertyValue instanceof Double) {// æ¯æ°å
if (!((Double) propertyValue == 0 && isExcludeZero)) {
isNull = false;
break;
}
}else if (propertyValue instanceof Float) {// æ¯æ°å
if (!((Float) propertyValue == 0 && isExcludeZero)) {
isNull = false;
break;
}
}else if (propertyValue instanceof Short) {// æ¯æ°å
if (!((Short) propertyValue == 0 && isExcludeZero)) {
isNull = false;
break;
}
}else {
isNull = false;
break;
}
}
}
return isNull;
}
æµè¯:
Java代ç
@Test
public void test_isNullObject() throws SecurityException,
IllegalArgumentException, NoSuchFieldException,
IllegalAccessException {
Person2 p = new Person2();
Assert.assertEquals(true, ReflectHWUtils.isNullObject(p, true));
Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, false));
p.setAddress("beijing");
Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, true));
Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, false));
p.setAddress(null);
p.setId(0);
Assert.assertEquals(true, ReflectHWUtils.isNullObject(p, true));
Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, false));
}
Person2 æºä»£ç (çç¥getter,setteræ¹æ³):
Java代ç
import java.sql.Timestamp;
public class Person2 {
private int id;
private int age;
private double weight;
private String personName;
private Timestamp birthdate;
public String identitify;
protected String address;
String phone;
}
温馨提示:答案为网友推荐,仅供参考