Created
May 6, 2016 10:33
-
-
Save kassim/aefc1d0518de3eb535619f8e4cced4fa to your computer and use it in GitHub Desktop.
Collection of null-safe methods, consisting of a few `isEmpty()` checks and a `equals()` check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Collection; | |
import java.util.Map; | |
public class NullSafe { | |
public static boolean isEmpty(Collection<?> collection) { | |
return collection == null || collection.isEmpty(); | |
} | |
public static boolean isEmpty(Map<?, ?> map) { | |
return map == null || map.isEmpty(); | |
} | |
public static boolean isEmpty(CharSequence chars) { | |
return chars == null || chars.length() == 0; | |
} | |
/** | |
* Taken from java.util.Objects | |
*/ | |
public static boolean equals(Object a, Object b) { | |
return (a == null) ? (b == null) : a.equals(b); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment