diff --git a/spring-core/src/main/java/org/springframework/util/ObjectUtils.java b/spring-core/src/main/java/org/springframework/util/ObjectUtils.java index 218d43fd3b..00d5916e4a 100644 --- a/spring-core/src/main/java/org/springframework/util/ObjectUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ObjectUtils.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. + * + *

Mainly for internal use within the framework. * *

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. + *

This method supports the following object types. + *

+ *

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 empty + * @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}, diff --git a/spring-core/src/test/java/org/springframework/util/ObjectUtilsTests.java b/spring-core/src/test/java/org/springframework/util/ObjectUtilsTests.java index ce796d0c1a..07bc3a0d57 100644 --- a/spring-core/src/test/java/org/springframework/util/ObjectUtilsTests.java +++ b/spring-core/src/test/java/org/springframework/util/ObjectUtilsTests.java @@ -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 set = new HashSet(); + set.add("foo"); + assertFalse(isEmpty(set)); + assertFalse(isEmpty(Arrays.asList("foo"))); + } + + @Test + public void isEmptyMap() { + assertTrue(isEmpty(Collections.emptyMap())); + + HashMap map = new HashMap(); + 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};