com.sangupta.jerry.util.AssertUtils Maven / Gradle / Ivy
/**
*
* jerry - Common Java Functionality
* Copyright (c) 2012-2014, Sandeep Gupta
*
* http://sangupta.com/projects/jerry
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.sangupta.jerry.util;
import java.util.Collection;
import java.util.Map;
/**
* Common assertion functions that are null safe. These may not be
* performant for common Java data types like {@link String} due
* to the extra overhead of a method call.
*
* @author sangupta
*
*/
public class AssertUtils {
/**
* Check if a given string is null or zero-length. Returns
* false even if the string contains white spaces. Use
* {@link AssertUtils#isBlank(String)} to check by ignoring white-spaces.
*
* @param string
* the string to tested for emptiness.
*
* @return true if string is empty, false
* otherwise.
*/
public static boolean isEmpty(String string) {
if(string == null || string.length() == 0) {
return true;
}
return false;
}
/**
* Check if a given string is null or zero-length. Returns
* true even if the string contains white spaces. Use
* {@link AssertUtils#isEmpty(String)} to check without ignoring
* white-spaces.
*
* @param string
* the string to tested for emptiness.
*
* @return true if string is empty, false
* otherwise.
*/
public static boolean isBlank(String string) {
if(string == null || string.trim().length() == 0) {
return true;
}
return false;
}
/**
* Check if a given string is non null and non-zero length.
* White-spaces are considered to be non-empty. Use
* {@link AssertUtils#isNotBlank(String)} method to check by ignoring
* white-spaces.
*
* @param string
* the string to tested for non-emptiness.
*
* @return false if string is empty, true
* otherwise.
*/
public static boolean isNotEmpty(String string) {
if(string == null || string.length() == 0) {
return false;
}
return true;
}
/**
* Check if a given string is non null and non-zero length.
* White-spaces are considered to be empty. Use
* {@link AssertUtils#isNotEmpty(String)} method to check without ignoring
* white-spaces.
*
* @param string
* the string to tested for non-emptiness.
*
* @return false if string is empty, true
* otherwise.
*/
public static boolean isNotBlank(String string) {
if(string == null || string.trim().length() == 0) {
return false;
}
return true;
}
/**
* Check if given array is null or zero-length.
*
* @param array
* the array to be tested
*
* @return true if array is null or zero-length,
* false otherwise.
*/
public static boolean isEmpty(Object[] array) {
if(array == null || array.length == 0) {
return true;
}
return false;
}
/**
* Check if given array is not-null and non-zero-length.
*
* @param array
* the array to be tested
*
* @return false if array is null or zero-length,
* true otherwise.
*/
public static boolean isNotEmpty(Object[] array) {
if(array == null || array.length == 0) {
return false;
}
return true;
}
/**
* Check if the given map is null or empty.
*
* @param map
* the map to be tested
*
* @return true if the map is either null or empty,
* false otherwise.
*
*/
@SuppressWarnings("rawtypes")
public static boolean isEmpty(Map map) {
if(map == null || map.isEmpty()) {
return true;
}
return false;
}
/**
* Check if the given map is not-null and not-empty.
*
* @param map
* the map to be tested
*
* @return false if the map is either null or
* empty, true otherwise.
*/
@SuppressWarnings("rawtypes")
public static boolean isNotEmpty(Map map) {
if(map == null || map.isEmpty()) {
return false;
}
return true;
}
/**
* Check if the given collection is null or empty.
*
* @param collection
* the collection to be tested
*
* @return true if the collection is either null
* or empty, false otherwise.
*/
@SuppressWarnings("rawtypes")
public static boolean isEmpty(Collection collection) {
if(collection == null || collection.isEmpty()) {
return true;
}
return false;
}
/**
* Check if the given collection is not-null and has values.
*
* @param collection
* the collection to be tested
*
* @return true if the collection is not-empty,
* false otherwise
*/
@SuppressWarnings("rawtypes")
public static boolean isNotEmpty(Collection collection) {
if(collection == null || collection.isEmpty()) {
return false;
}
return true;
}
/**
* Check if an object is null. Ideally this method should
* be replaced by a normal null check. It is there only to
* support the abstraction of this class, when the incoming object type is
* not known.
*
* @param object
* the object to be tested
*
* @return returns true if object is null,
* false otherwise.
*/
public static boolean isEmpty(Object object) {
if(object == null) {
return true;
}
return false;
}
/**
* Check if an object is NOT-null. Ideally this method should
* be replaced by a normal null check. It is there only to
* support the abstraction of this class, when the incoming object type is
* not known.
*
* @param object
* the object to be tested
*
* @return returns true if object is not-null,
* false otherwise.
*/
public static boolean isNotEmpty(Object object) {
if(object == null) {
return false;
}
return true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy