Changed isEmpty to accept multiple variables
This commit is contained in:
parent
4e01c413e9
commit
75fa916ca5
2 changed files with 37 additions and 2 deletions
|
|
@ -32,10 +32,25 @@ import java.util.Map;
|
|||
*/
|
||||
public class ObjectUtil {
|
||||
|
||||
/**
|
||||
* @return true if all given Objects are null or empty String, List or Map
|
||||
*/
|
||||
public static boolean isEmpty(Object... objs) {
|
||||
if (isEmptyCheck(objs))
|
||||
return true;
|
||||
|
||||
for (Object obj : objs) {
|
||||
if (!isEmptyCheck(obj))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if obj is null or empty String, List, Map
|
||||
*/
|
||||
public static boolean isEmpty(Object obj) {
|
||||
private static boolean isEmptyCheck(Object obj) {
|
||||
if (obj == null)
|
||||
return true;
|
||||
|
||||
|
|
|
|||
|
|
@ -32,9 +32,29 @@ import static org.junit.Assert.*;
|
|||
|
||||
public class ObjectUtilTest {
|
||||
|
||||
@Test
|
||||
public void isEmptyArr() {
|
||||
assertTrue(ObjectUtil.isEmpty());
|
||||
assertTrue(ObjectUtil.isEmpty(null, null));
|
||||
assertTrue(ObjectUtil.isEmpty("", "", ""));
|
||||
assertTrue(ObjectUtil.isEmpty(new StringBuffer(), ""));
|
||||
assertTrue(ObjectUtil.isEmpty("", new StringBuilder()));
|
||||
assertTrue(ObjectUtil.isEmpty(new ArrayList<>(), ""));
|
||||
assertTrue(ObjectUtil.isEmpty(new LinkedList<>(), ""));
|
||||
assertTrue(ObjectUtil.isEmpty(new HashMap<>(), ""));
|
||||
assertTrue(ObjectUtil.isEmpty(new Hashtable<>(), ""));
|
||||
|
||||
|
||||
assertFalse(ObjectUtil.isEmpty(" ", ""));
|
||||
assertFalse(ObjectUtil.isEmpty("a", ""));
|
||||
assertFalse(ObjectUtil.isEmpty("", new StringBuilder("a")));
|
||||
assertFalse(ObjectUtil.isEmpty("", new StringBuffer("a")));
|
||||
assertFalse(ObjectUtil.isEmpty("", Arrays.asList(1, 2, 3)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isEmpty() {
|
||||
assertTrue(ObjectUtil.isEmpty(null));
|
||||
assertTrue(ObjectUtil.isEmpty((String) null));
|
||||
assertTrue(ObjectUtil.isEmpty(""));
|
||||
assertTrue(ObjectUtil.isEmpty(new StringBuffer()));
|
||||
assertTrue(ObjectUtil.isEmpty(new StringBuilder()));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue