Skip to content

Instantly share code, notes, and snippets.

@kassim
Created May 6, 2016 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kassim/aefc1d0518de3eb535619f8e4cced4fa to your computer and use it in GitHub Desktop.
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
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