Assert Util : Assert « Language Basics « Java






Assert Util

        

//package org.gwtoolbox.commons.util.client;

/**
 * @author Uri Boness
 */
public class Assert {

    public static void isEqual(Object o1, Object o2) throws IllegalArgumentException {
        isEqual(o1, o2, "Assertion failed: two given objects are expected to be equal");
    }

    public static void isEqual(Object o1, Object o2, String message) throws IllegalArgumentException {
        if (!o1.equals(o2)) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void isSame(Object o1, Object o2) {
        isSame(o1, o2, "Assertion failed: two give object are expected to be the same object");
    }

    public static void isSame(Object o1, Object o2, String message) {
        if (o1 != o2) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void notNull(Object object) {
        notNull(object, "Assertion failed: give object cannot be null");
    }

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

    public static void isTrue(boolean expression) {
        isTrue(expression, "Assertion failed: give expression is expected to be true");
    }

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

    public static void isFalse(boolean expression) {
        isFalse(expression, "Assertion failed: give expression is expected to be false");
    }

    public static void isFalse(boolean expression, String message) {
        isTrue(!expression, message);
    }

    public static void state(boolean expression) throws IllegalStateException {
        state(expression, "Assertion failed: illegal state");
    }

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

}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.A Program with Assertions
2.Enabling Assertions from the Command Line: -ea and -da enable and disable assertion in a package subtree or in a class.
3.Handling an Assertion Error
4.Catch assert exception with message
5.Assert with an informative message
6.Non-informative style of assert
7.Using the class loader to enable assertions
8.Class for checking syntax and comments for the assert
9.Assertion utility class that assists in validating arguments.
10.An assertion utility just for simple checks.