All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.alittlehuang.data.util.Assert Maven / Gradle / Ivy

package com.github.alittlehuang.data.util;

import java.util.Collection;
import java.util.Map;
import java.util.function.Supplier;

/**
 * @author ALittleHuang
 */
@SuppressWarnings( {"Duplicates", "unused"} )
public abstract class Assert {
    public Assert() {
    }

    public static void state(boolean expression, String message) {
        if ( !expression ) {
            throw new IllegalStateException(message);
        }
    }

    public static void state(boolean expression, Supplier messageSupplier) {
        if ( !expression ) {
            throw new IllegalStateException(nullSafeGet(messageSupplier));
        }
    }

    /**
     * @deprecated
     */
    @Deprecated
    public static void state(boolean expression) {
        state(expression, "[Assertion failed] - this state invariant must be true");
    }

    public static void isTrue(boolean expression, String message) {
        if ( !expression ) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void isTrue(boolean expression, Supplier messageSupplier) {
        if ( !expression ) {
            throw new IllegalArgumentException(nullSafeGet(messageSupplier));
        }
    }

    /**
     * @deprecated
     */
    @Deprecated
    public static void isTrue(boolean expression) {
        isTrue(expression, "[Assertion failed] - this expression must be true");
    }

    public static void isNull(Object object, String message) {
        if ( object != null ) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void isNull(Object object, Supplier messageSupplier) {
        if ( object != null ) {
            throw new IllegalArgumentException(nullSafeGet(messageSupplier));
        }
    }

    /**
     * @deprecated
     */
    @Deprecated
    public static void isNull(Object object) {
        isNull(object, "[Assertion failed] - the object argument must be null");
    }

    public static void notNull(Object object, String message) {
        if ( object == null ) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void notNull(Object object, Supplier messageSupplier) {
        if ( object == null ) {
            throw new IllegalArgumentException(nullSafeGet(messageSupplier));
        }
    }

    /**
     * @deprecated
     */
    @Deprecated
    public static void notNull(Object object) {
        notNull(object, "[Assertion failed] - this argument is required; it must not be null");
    }

    public static void hasLength(String text, String message) {
        if ( !StringUtils.hasLength(text) ) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void hasLength(String text, Supplier messageSupplier) {
        if ( !StringUtils.hasLength(text) ) {
            throw new IllegalArgumentException(nullSafeGet(messageSupplier));
        }
    }

    /**
     * @deprecated
     */
    @Deprecated
    public static void hasLength(String text) {
        hasLength(text, "[Assertion failed] - this String argument must have length; it must not be null or empty");
    }

    public static void hasText(String text, String message) {
        if ( StringUtils.isBlank(text) ) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void hasText(String text, Supplier messageSupplier) {
        if ( StringUtils.isBlank(text) ) {
            throw new IllegalArgumentException(nullSafeGet(messageSupplier));
        }
    }

    /**
     * @deprecated
     */
    @Deprecated
    public static void hasText(String text) {
        hasText(text, "[Assertion failed] - this String argument must have text; it must not be null, empty, or blank");
    }

    public static void doesNotContain(String textToSearch, String substring, String message) {
        if ( StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) && textToSearch.contains(substring) ) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void doesNotContain(String textToSearch, String substring, Supplier messageSupplier) {
        if ( StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) && textToSearch.contains(substring) ) {
            throw new IllegalArgumentException(nullSafeGet(messageSupplier));
        }
    }

    /**
     * @deprecated
     */
    @Deprecated
    public static void doesNotContain(String textToSearch, String substring) {
        doesNotContain(textToSearch, substring, () -> "[Assertion failed] - this String argument must not contain the substring [" + substring + "]");
    }

    public static void notEmpty(Object[] array, String message) {
        if ( ObjectUtils.isEmpty(array) ) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void notEmpty(Object[] array, Supplier messageSupplier) {
        if ( ObjectUtils.isEmpty(array) ) {
            throw new IllegalArgumentException(nullSafeGet(messageSupplier));
        }
    }

    /**
     * @deprecated
     */
    @Deprecated
    public static void notEmpty(Object[] array) {
        notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element");
    }

    public static void noNullElements(Object[] array, String message) {
        if ( array != null ) {
            for ( Object element : array ) {
                if ( element == null ) {
                    throw new IllegalArgumentException(message);
                }
            }
        }

    }

    public static void noNullElements(Object[] array, Supplier messageSupplier) {
        if ( array != null ) {
            for ( Object element : array ) {
                if ( element == null ) {
                    throw new IllegalArgumentException(nullSafeGet(messageSupplier));
                }
            }
        }

    }

    /**
     * @deprecated
     */
    @Deprecated
    public static void noNullElements(Object[] array) {
        noNullElements(array, "[Assertion failed] - this array must not contain any null elements");
    }

    public static void notEmpty(Collection collection, String message) {
        if ( CollectionUtils.isEmpty(collection) ) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void notEmpty(Collection collection, Supplier messageSupplier) {
        if ( CollectionUtils.isEmpty(collection) ) {
            throw new IllegalArgumentException(nullSafeGet(messageSupplier));
        }
    }

    /**
     * @deprecated
     */
    @Deprecated
    public static void notEmpty(Collection collection) {
        notEmpty(collection, "[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
    }

    public static void notEmpty(Map map, String message) {
        if ( CollectionUtils.isEmpty(map) ) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void notEmpty(Map map, Supplier messageSupplier) {
        if ( CollectionUtils.isEmpty(map) ) {
            throw new IllegalArgumentException(nullSafeGet(messageSupplier));
        }
    }

    /**
     * @deprecated
     */
    @Deprecated
    public static void notEmpty(Map map) {
        notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry");
    }

    public static void isInstanceOf(Class type, Object obj, String message) {
        notNull(type, "Type to check against must not be null");
        if ( !type.isInstance(obj) ) {
            instanceCheckFailed(type, obj, message);
        }

    }

    public static void isInstanceOf(Class type, Object obj, Supplier messageSupplier) {
        notNull(type, "Type to check against must not be null");
        if ( !type.isInstance(obj) ) {
            instanceCheckFailed(type, obj, nullSafeGet(messageSupplier));
        }

    }

    public static void isInstanceOf(Class type, Object obj) {
        isInstanceOf(type, obj, "");
    }

    public static void isAssignable(Class superType, Class subType, String message) {
        notNull(superType, "Super type to check against must not be null");
        if ( subType == null || !superType.isAssignableFrom(subType) ) {
            assignableCheckFailed(superType, subType, message);
        }

    }

    public static void isAssignable(Class superType, Class subType, Supplier messageSupplier) {
        notNull(superType, "Super type to check against must not be null");
        if ( subType == null || !superType.isAssignableFrom(subType) ) {
            assignableCheckFailed(superType, subType, nullSafeGet(messageSupplier));
        }

    }

    public static void isAssignable(Class superType, Class subType) {
        isAssignable(superType, subType, "");
    }

    private static void instanceCheckFailed(Class type, Object obj, String msg) {
        String className = obj != null ? obj.getClass().getName() : "null";
        String result = "";
        boolean defaultMessage = true;
        if ( StringUtils.hasLength(msg) ) {
            if ( endsWithSeparator(msg) ) {
                result = msg + " ";
            } else {
                result = messageWithTypeName(msg, className);
                defaultMessage = false;
            }
        }

        if ( defaultMessage ) {
            result = result + "Object of class [" + className + "] must be an instance of " + type;
        }

        throw new IllegalArgumentException(result);
    }

    private static void assignableCheckFailed(Class superType, Class subType, String msg) {
        String result = "";
        boolean defaultMessage = true;
        if ( StringUtils.hasLength(msg) ) {
            if ( endsWithSeparator(msg) ) {
                result = msg + " ";
            } else {
                result = messageWithTypeName(msg, subType);
                defaultMessage = false;
            }
        }

        if ( defaultMessage ) {
            result = result + subType + " is not assignable to " + superType;
        }

        throw new IllegalArgumentException(result);
    }

    private static boolean endsWithSeparator(String msg) {
        return msg.endsWith(":") || msg.endsWith(";") || msg.endsWith(",") || msg.endsWith(".");
    }

    private static String messageWithTypeName(String msg, Object typeName) {
        return msg + ( msg.endsWith(" ") ? "" : ": " ) + typeName;
    }


    private static String nullSafeGet(Supplier messageSupplier) {
        return messageSupplier != null ? messageSupplier.get() : null;
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy