Introduce general purpose isEmpty() method in ObjectUtils

Prior to this commit, there existed several isEmpty() methods scattered
across various utilities such as ObjectUtils, CollectionUtils, and
StringUtils; however, each of these methods requires a cast to the type
supported for that particular variant.

This commit introduces a general-purpose isEmpty(Object) method in
ObjectUtils that transparently supports multiple object types in a
central location without the need for casts or juggling multiple
utility classes.

Issue: SPR-13119
master
Sam Brannen 9 years ago
parent e7f4544248
commit 05de9a8c4a
  1. 55
      spring-core/src/main/java/org/springframework/util/ObjectUtils.java
  2. 58
      spring-core/src/test/java/org/springframework/util/ObjectUtilsTests.java

@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,10 +18,13 @@ package org.springframework.util;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
/**
* Miscellaneous object utility methods.
* Mainly for internal use within the framework.
*
* <p>Mainly for internal use within the framework.
*
* <p>Thanks to Alex Ruiz for contributing several enhancements to this class!
*
@ -30,7 +33,11 @@ import java.util.Arrays;
* @author Rod Johnson
* @author Rob Harrop
* @author Chris Beams
* @author Sam Brannen
* @since 19.03.2004
* @see ClassUtils
* @see CollectionUtils
* @see StringUtils
*/
public abstract class ObjectUtils {
@ -92,11 +99,55 @@ public abstract class ObjectUtils {
* Determine whether the given array is empty:
* i.e. {@code null} or of zero length.
* @param array the array to check
* @see #isEmpty(Object)
*/
public static boolean isEmpty(Object[] array) {
return (array == null || array.length == 0);
}
/**
* Determine whether the given object is empty.
* <p>This method supports the following object types.
* <ul>
* <li>{@code Array}: considered empty if its length is zero</li>
* <li>{@link CharSequence}: considered empty if its length is zero</li>
* <li>{@link Collection}: delegates to {@link Collection#isEmpty()}</li>
* <li>{@link Map}: delegates to {@link Map#isEmpty()}</li>
* </ul>
* <p>If the given object is non-null and not one of the aforementioned
* supported types, this method returns {@code false}.
* @param obj the object to check
* @return {@code true} if the object is {@code null} or <em>empty</em>
* @since 4.2
* @see ObjectUtils#isEmpty(Object[])
* @see StringUtils#hasLength(CharSequence)
* @see StringUtils#isEmpty(Object)
* @see CollectionUtils#isEmpty(java.util.Collection)
* @see CollectionUtils#isEmpty(java.util.Map)
*/
@SuppressWarnings("rawtypes")
public static boolean isEmpty(Object obj) {
if (obj == null) {
return true;
}
if (obj.getClass().isArray()) {
return Array.getLength(obj) == 0;
}
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();
}
// else
return false;
}
/**
* Check whether the given array contains the given element.
* @param array the array to check (may be {@code null},

@ -18,6 +18,11 @@ package org.springframework.util;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import org.junit.Rule;
import org.junit.Test;
@ -25,6 +30,7 @@ import org.junit.rules.ExpectedException;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.util.ObjectUtils.*;
/**
* Unit tests for {@link ObjectUtils}.
@ -84,6 +90,58 @@ public class ObjectUtilsTests {
assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new Throwable(), throwable));
}
@Test
public void isEmptyNull() {
assertTrue(isEmpty(null));
}
@Test
public void isEmptyArray() {
assertTrue(isEmpty(new char[0]));
assertTrue(isEmpty(new Object[0]));
assertTrue(isEmpty(new Integer[0]));
assertFalse(isEmpty(new int[] { 42 }));
assertFalse(isEmpty(new Integer[] { new Integer(42) }));
}
@Test
public void isEmptyCollection() {
assertTrue(isEmpty(Collections.emptyList()));
assertTrue(isEmpty(Collections.emptySet()));
Set<String> set = new HashSet<String>();
set.add("foo");
assertFalse(isEmpty(set));
assertFalse(isEmpty(Arrays.asList("foo")));
}
@Test
public void isEmptyMap() {
assertTrue(isEmpty(Collections.emptyMap()));
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("foo", 42L);
assertFalse(isEmpty(map));
}
@Test
public void isEmptyCharSequence() {
assertTrue(isEmpty(new StringBuilder()));
assertTrue(isEmpty(""));
assertFalse(isEmpty(new StringBuilder("foo")));
assertFalse(isEmpty(" "));
assertFalse(isEmpty("\t"));
assertFalse(isEmpty("foo"));
}
@Test
public void isEmptyUnsupportedObjectType() {
assertFalse(isEmpty(42L));
assertFalse(isEmpty(new Object()));
}
@Test
public void toObjectArray() {
int[] a = new int[] {1, 2, 3, 4, 5};

Loading…
Cancel
Save