Skip to content

Commit

Permalink
Merge pull request #124 from egovframe-contribution/add_isEmpty
Browse files Browse the repository at this point in the history
표준프레임워크 발전을 위한 소중한 의견에 감사드립니다.
  • Loading branch information
jei007 authored Sep 11, 2023
2 parents 67a8722 + 9f13de6 commit 6fd1286
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
*/
package org.egovframe.rte.fdl.string;

import com.sun.istack.internal.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;

/**
* 객체의 로딩을 지원하는 유틸 클래스
Expand Down Expand Up @@ -137,6 +142,27 @@ public static Object instantiate(String className, String[] types, Object[] valu
public static boolean isNull(Object object) {
//return ((object == null) || object.equals(null));
return (object == null) ? true : false;

}

/**
* 객체가 비어있는 값인지 확인한다. (list == [] or null)
* @param obj
* @return Null/비어있는경우 true / Null이 아니거나 데이터가 있는경우 false
*/
public static boolean isEmpty(@Nullable Object obj) {
if (obj == null) {
return true;
} else if (obj instanceof Optional) {
return !((Optional)obj).isPresent();
} else if (obj instanceof CharSequence) {
return ((CharSequence)obj).length() == 0;
} else if (obj.getClass().isArray()) {
return Array.getLength(obj) == 0;
} else if (obj instanceof Collection) {
return ((Collection)obj).isEmpty();
} else {
return obj instanceof Map ? ((Map)obj).isEmpty() : false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.ArrayList;

import static org.junit.Assert.*;


Expand Down Expand Up @@ -50,6 +52,19 @@ public void testIsNull() throws Exception {
assertTrue(EgovObjectUtil.isNull(null));
}

/**
* [Flow #-2 Positive Case : check which Obejct is empty
* @throws Exception
*/
@Test
public void testIsEmpty() throws Exception{
ArrayList<String> list = new ArrayList<>();
list.add("12124");

assertFalse(EgovObjectUtil.isEmpty(new Object()));
assertTrue(EgovObjectUtil.isEmpty(null));
assertFalse(EgovObjectUtil.isEmpty(list));
}

/**
* @throws Exception
Expand Down

0 comments on commit 6fd1286

Please sign in to comment.