2007년 10월 29일
객체의 값이 비었거나 null인가?(isEmpty)
Velocity를 사용하면서 특정 객체가 비었는지(null, 빈문자열, 빈 배열, 빈 컬렉션) 확인할 필요가 자주 있어서 만들어 보았다.
JSTL을 사용한다면 empty 연산자로 쉽게 할 수 있다. 딴건 별거 없는데, 배열 검사하는 것만 좀 헷갈린다.
JSTL을 사용한다면 empty 연산자로 쉽게 할 수 있다. 딴건 별거 없는데, 배열 검사하는 것만 좀 헷갈린다.
import java.lang.reflect.Array;
import java.util.Collection;
/**
* Null이거나 빈값(빈 문자열, 빈 컬렉션)인지 검사
*
* @param object
* @return
*/
@SuppressWarnings("unchecked")
public boolean isEmpty(Object object) {
if (object == null) {
return true;
}
if (object instanceof String) {
String str = (String) object;
return str.length() == 0;
}
if (object instanceof Collection) {
Collection collection = (Collection) object;
return collection.size() == 0;
}
if (object.getClass().isArray()) {
try {
if (Array.getLength(object) == 0) {
return true;
}
} catch (Exception e) {
//do nothing
}
}
return false;
}
...
# by | 2007/10/29 22:37 | 프로그래밍 | 트랙백 | 덧글(2)







☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]