package net.northking.cctp.element.util;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;

/**
 * Assert
 *
 * @author fengxiang.zhang
 * @since 1.0
 */
public class Assert {

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

    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) {
        notNull(object, "[Assertion failed] - this argument is required; it must not be null");
    }

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

    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 endsWith(String text, String suffix, String message) {
        if (!StringUtils.endsWith(text, suffix)) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void endsWith(String text, String suffix) {
        endsWith(text, suffix, "[Assertion failed] - this argument must end with " + suffix);
    }

    public static void hasElement(Object[] array, String message) {
        if (ArrayUtils.getLength(array) == 0) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void hasElement(Object[] array) {
        hasElement(array, "[Assertion failed] - this argument array does not contain any elements");
    }

    public static void hasElement(Collection collection, String message) {
        if (collection == null || collection.size() == 0) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void hasElement(Collection collection) {
        hasElement(collection, "[Assertion failed] - this argument collection does not contain any elements");
    }

    public static void equalsAny(String str, String[] search, String message) {
        if (!StringUtils.equalsAny(str, search)) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void equalsAny(String str, String[] search) {
        equalsAny(str, search, "[Assertion failed] - this argument string not equal to any of search strings");
    }
}