Skip to content

Instantly share code, notes, and snippets.

@rmpestano
Last active December 28, 2015 15:29
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 rmpestano/0384c91f46c47add7a85 to your computer and use it in GitHub Desktop.
Save rmpestano/0384c91f46c47add7a85 to your computer and use it in GitHub Desktop.
import java.io.Serializable;
import java.util.Collection;
import java.util.Map;

/**
 * Created by rafael-pestano on 26/06/2015. assertions utils
 *
 */
public class Assert implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private Assert() {

    }

    /**
     * @return TRUE assertion when given objects is not null, FALSE otherwise
     */
    public static boolean has(Object object) {
        return object != null;
    }

    /**
     * @return TRUE when given text has any character, FALSE otherwise
     */
    public static boolean has(String text) {
        if (text != null && !isBlank(text)) {
            return true;
        }
        return false;
    }


    /**
     * @return TRUE when given text contains the given substring, FALSE otherwise
     */
    public static boolean contains(String textToSearch, String substring) {
        if (textToSearch != null && textToSearch.contains(substring)) {
            return true;
        }
        return false;
    }

    /**
     * @return TRUE when given array has elements; that is, it must not be {@code null} and must
     *         have at least one element. FALSE otherwise
     */
    public static boolean has(Object[] array) {
        if (array == null || array.length == 0) {
            return false;
        }
        for (Object element : array) {
            if (element != null) {
                return true;
            }
        }
        return false;
    }

    /**
     * @return TRUE when given collection has elements; that is, it must not be {@code null} and
     *         must have at least one element. @return FALSE otherwise
     */
    public static boolean has(Collection<?> collection) {
        if(collection == null || !hasElements(collection)){
            return false;
        } else {
            return true;
        }
    }

    private static boolean hasElements(Collection<?> collection) {
        if(collection.isEmpty()){
            return false;
        }
        //verifica se tem elementos diferente de nulos
        for (Object o : collection) {
            if(o != null){
                return true;
            }
        }
        return false;//nenhum elemento ou apenas elementos nulos

    }

    /**
     * @return TRUE if given Map has entries; that is, it must not be {@code null} and must have at
     *         least one entry. Queue FALSE otherwise
     */
    public static boolean has(Map<?, ?> map) {
        if (map == null) {
            return false;
        }
        if (has(map.entrySet().toArray())) {
            return true;
        }
        return false;
    }

	 private static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (Character.isWhitespace(cs.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }


}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment