From 9f13de6405c7fd66e26058571c2e5a9e17f10553 Mon Sep 17 00:00:00 2001 From: Aiden Yoo Date: Mon, 28 Aug 2023 17:34:18 +0900 Subject: [PATCH] Added isEmpty method at EgovObjectUtil --- .../rte/fdl/string/EgovObjectUtil.java | 26 +++++++++++++++++++ .../rte/fdl/string/EgovObjectUtilTest.java | 15 +++++++++++ 2 files changed, 41 insertions(+) diff --git a/Foundation/org.egovframe.rte.fdl.string/src/main/java/org/egovframe/rte/fdl/string/EgovObjectUtil.java b/Foundation/org.egovframe.rte.fdl.string/src/main/java/org/egovframe/rte/fdl/string/EgovObjectUtil.java index 76af126..74d801e 100644 --- a/Foundation/org.egovframe.rte.fdl.string/src/main/java/org/egovframe/rte/fdl/string/EgovObjectUtil.java +++ b/Foundation/org.egovframe.rte.fdl.string/src/main/java/org/egovframe/rte/fdl/string/EgovObjectUtil.java @@ -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; /** * 객체의 로딩을 지원하는 유틸 클래스 @@ -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; + } + } } diff --git a/Foundation/org.egovframe.rte.fdl.string/src/test/java/org/egovframe/rte/fdl/string/EgovObjectUtilTest.java b/Foundation/org.egovframe.rte.fdl.string/src/test/java/org/egovframe/rte/fdl/string/EgovObjectUtilTest.java index 862dc6f..a08971d 100644 --- a/Foundation/org.egovframe.rte.fdl.string/src/test/java/org/egovframe/rte/fdl/string/EgovObjectUtilTest.java +++ b/Foundation/org.egovframe.rte.fdl.string/src/test/java/org/egovframe/rte/fdl/string/EgovObjectUtilTest.java @@ -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.*; @@ -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 list = new ArrayList<>(); + list.add("12124"); + + assertFalse(EgovObjectUtil.isEmpty(new Object())); + assertTrue(EgovObjectUtil.isEmpty(null)); + assertFalse(EgovObjectUtil.isEmpty(list)); + } /** * @throws Exception