Merge branch 'LANG-1411'
This commit is contained in:
commit
44e32205a6
|
@ -21,6 +21,7 @@ import java.io.Serializable;
|
|||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
|
@ -77,6 +78,79 @@ public class ObjectUtils {
|
|||
super();
|
||||
}
|
||||
|
||||
// Empty checks
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Checks if an Object is empty or null.</p>
|
||||
*
|
||||
* The following types are supported:
|
||||
* <ul>
|
||||
* <li>{@link CharSequence}: Considered empty if its length is zero.</li>
|
||||
* <li>{@code Array}: Considered empty if its length is zero.</li>
|
||||
* <li>{@link Collection}: Considered empty if it has zero elements.</li>
|
||||
* <li>{@link Map}: Considered empty if it has zero key-value mappings.</li>
|
||||
* </ul>
|
||||
*
|
||||
* <pre>
|
||||
* ObjectUtils.isEmpty(null) = true
|
||||
* ObjectUtils.isEmpty("") = true
|
||||
* ObjectUtils.isEmpty("ab") = false
|
||||
* ObjectUtils.isEmpty(new int[]{}) = true
|
||||
* ObjectUtils.isEmpty(new int[]{1,2,3}) = false
|
||||
* ObjectUtils.isEmpty(1234) = false
|
||||
* </pre>
|
||||
*
|
||||
* @param object the {@code Object} to test, may be {@code null}
|
||||
* @return {@code true} if the object has a supported type and is empty or null,
|
||||
* {@code false} otherwise
|
||||
*/
|
||||
public static boolean isEmpty(final Object object) {
|
||||
if (object == null) {
|
||||
return true;
|
||||
}
|
||||
if (object instanceof CharSequence) {
|
||||
return ((CharSequence) object).length() == 0;
|
||||
}
|
||||
if (object.getClass().isArray()) {
|
||||
return Array.getLength(object) == 0;
|
||||
}
|
||||
if (object instanceof Collection<?>) {
|
||||
return ((Collection<?>) object).isEmpty();
|
||||
}
|
||||
if (object instanceof Map<?, ?>) {
|
||||
return ((Map<?, ?>) object).isEmpty();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks if an Object is not empty and not null.</p>
|
||||
*
|
||||
* The following types are supported:
|
||||
* <ul>
|
||||
* <li>{@link CharSequence}: Considered empty if its length is zero.</li>
|
||||
* <li>{@code Array}: Considered empty if its length is zero.</li>
|
||||
* <li>{@link Collection}: Considered empty if it has zero elements.</li>
|
||||
* <li>{@link Map}: Considered empty if it has zero key-value mappings.</li>
|
||||
* </ul>
|
||||
*
|
||||
* <pre>
|
||||
* ObjectUtils.isNotEmpty(null) = false
|
||||
* ObjectUtils.isNotEmpty("") = false
|
||||
* ObjectUtils.isNotEmpty("ab") = true
|
||||
* ObjectUtils.isNotEmpty(new int[]{}) = false
|
||||
* ObjectUtils.isNotEmpty(new int[]{1,2,3}) = true
|
||||
* ObjectUtils.isNotEmpty(1234) = true
|
||||
* </pre>
|
||||
*
|
||||
* @param object the {@code Object} to test, may be {@code null}
|
||||
* @return {@code true} if the object has an unsupported type or is not empty
|
||||
* and not null, {@code false} otherwise
|
||||
*/
|
||||
public static boolean isNotEmpty(final Object object) {
|
||||
return !isEmpty(object);
|
||||
}
|
||||
|
||||
// Defaulting
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
|
|
|
@ -31,9 +31,14 @@ import java.lang.reflect.Modifier;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.lang3.exception.CloneFailedException;
|
||||
import org.apache.commons.lang3.mutable.MutableObject;
|
||||
|
@ -47,6 +52,13 @@ import org.junit.Test;
|
|||
public class ObjectUtilsTest {
|
||||
private static final String FOO = "foo";
|
||||
private static final String BAR = "bar";
|
||||
private static final String[] NON_EMPTY_ARRAY = new String[] { FOO, BAR, };
|
||||
private static final List<String> NON_EMPTY_LIST = Arrays.asList(NON_EMPTY_ARRAY);
|
||||
private static final Set<String> NON_EMPTY_SET = new HashSet<>(NON_EMPTY_LIST);
|
||||
private static final Map<String, String> NON_EMPTY_MAP = new HashMap<>();
|
||||
static {
|
||||
NON_EMPTY_MAP.put(FOO, BAR);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
@Test
|
||||
|
@ -59,6 +71,41 @@ public class ObjectUtilsTest {
|
|||
assertFalse(Modifier.isFinal(ObjectUtils.class.getModifiers()));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
@Test
|
||||
public void testIsEmpty() {
|
||||
assertTrue(ObjectUtils.isEmpty(null));
|
||||
assertTrue(ObjectUtils.isEmpty(""));
|
||||
assertTrue(ObjectUtils.isEmpty(new int[] {}));
|
||||
assertTrue(ObjectUtils.isEmpty(Collections.emptyList()));
|
||||
assertTrue(ObjectUtils.isEmpty(Collections.emptySet()));
|
||||
assertTrue(ObjectUtils.isEmpty(Collections.emptyMap()));
|
||||
|
||||
assertFalse(ObjectUtils.isEmpty(" "));
|
||||
assertFalse(ObjectUtils.isEmpty("ab"));
|
||||
assertFalse(ObjectUtils.isEmpty(NON_EMPTY_ARRAY));
|
||||
assertFalse(ObjectUtils.isEmpty(NON_EMPTY_LIST));
|
||||
assertFalse(ObjectUtils.isEmpty(NON_EMPTY_SET));
|
||||
assertFalse(ObjectUtils.isEmpty(NON_EMPTY_MAP));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsNotEmpty() {
|
||||
assertFalse(ObjectUtils.isNotEmpty(null));
|
||||
assertFalse(ObjectUtils.isNotEmpty(""));
|
||||
assertFalse(ObjectUtils.isNotEmpty(new int[] {}));
|
||||
assertFalse(ObjectUtils.isNotEmpty(Collections.emptyList()));
|
||||
assertFalse(ObjectUtils.isNotEmpty(Collections.emptySet()));
|
||||
assertFalse(ObjectUtils.isNotEmpty(Collections.emptyMap()));
|
||||
|
||||
assertTrue(ObjectUtils.isNotEmpty(" "));
|
||||
assertTrue(ObjectUtils.isNotEmpty("ab"));
|
||||
assertTrue(ObjectUtils.isNotEmpty(NON_EMPTY_ARRAY));
|
||||
assertTrue(ObjectUtils.isNotEmpty(NON_EMPTY_LIST));
|
||||
assertTrue(ObjectUtils.isNotEmpty(NON_EMPTY_SET));
|
||||
assertTrue(ObjectUtils.isNotEmpty(NON_EMPTY_MAP));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
@Test
|
||||
public void testIsNull() {
|
||||
|
|
Loading…
Reference in New Issue