Skip to content

Instantly share code, notes, and snippets.

@nijogeorgep
Last active January 21, 2019 06:21
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 nijogeorgep/4e502887fef69cc0c3ee1b95e8f4b194 to your computer and use it in GitHub Desktop.
Save nijogeorgep/4e502887fef69cc0c3ee1b95e8f4b194 to your computer and use it in GitHub Desktop.
Null Checks and Empty Checks on Objects
class util {
/**
* This method returns true if the collection is null or is empty.
* @param collection
* @return true | false
*/
public static boolean isEmpty( Collection<?> collection ){
if( collection == null || collection.isEmpty() ){
return true;
}
return false;
}
/**
* This method returns true of the map is null or is empty.
* @param map
* @return true | false
*/
public static boolean isEmpty( Map<?, ?> map ){
if( map == null || map.isEmpty() ){
return true;
}
return false;
}
/**
* This method returns true if the objet is null.
* @param object
* @return true | false
*/
public static boolean isEmpty( Object object ){
if( object == null ){
return true;
}
return false;
}
/**
* This method returns true if the input array is null or its length is zero.
* @param array
* @return true | false
*/
public static boolean isEmpty( Object[] array ){
if( array == null || array.length == 0 ){
return true;
}
return false;
}
/**
* This method returns true if the input string is null or its length is zero.
* @param string
* @return true | false
*/
public static boolean isEmpty( String string ){
if( string == null || string.trim().length() == 0 ){
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment