package info.gamlor.db4oseries;

/**
 * Check precondition of method-calls etc.
 *
 * @author roman.stoffel@gamlor.info
 * @since Jul 2, 2009
 */
public final class Preconditions {

    private Preconditions() {
        throw new Error("no instance possible");
    }

    public static void checkArgument(boolean hasToBeTrue, String message) throws IllegalArgumentException {
        if (!hasToBeTrue) {
             throw new IllegalArgumentException(message);
        }
    }


    public static <T extends Comparable<T>> void checkArgumentIsBiggerThan(T thisValueIs, T biggerThanThis, String message) throws IllegalArgumentException {
        if (0>= thisValueIs.compareTo(biggerThanThis)) {
            throw new IllegalArgumentException("The argument "+message+" has to be bigger than '"+ biggerThanThis +"'" +
                    " but is '"+ thisValueIs +"'");
        }
    }




    public static <T extends Comparable<T>> void checkArgumentIsSmallerThan(T thisValueIs, T smallerThan, String message) throws IllegalArgumentException {
        if (0<=thisValueIs.compareTo(smallerThan)) {
            throw new IllegalArgumentException("The argument "+message+" has to be smaller than '"+smallerThan+"'" +
                    " but is '"+thisValueIs+"'");
        }
    }


    public static void checkArgumentNotNull(Object notNull, String variableName) throws IllegalArgumentException {
        if (null == notNull) {
            throw new IllegalArgumentException("The argument '" + variableName + "' cannot be null");
        }
    }


    /**
     * Check that the string is neither null nor empty. The string is trimmed before checked if it is empty
     *
     * @param notEmpty     the string, non-empty
     * @param variableName the parameter-name
     * @throws IllegalArgumentException the exception thrown in the precondition fails
     */
    public static void checkArgumentNotEmpty(String notEmpty, String variableName) throws IllegalArgumentException {
        if (null == notEmpty || notEmpty.trim().isEmpty()) {
            throw new IllegalArgumentException("The argument '" + variableName + "' cannot be null");
        }
    }

    public static void checkState(boolean hasToBeTrue, String message) throws IllegalStateException {
        if (!hasToBeTrue) {
            throw new IllegalStateException(message);
        }
    }
}

