KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > core > refactoring > Assert


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ltk.internal.core.refactoring;
12
13 /**
14  * <code>Assert</code> is useful for for embedding runtime sanity checks
15  * in code.
16  * The predicate methods all test a condition and throw some
17  * type of unchecked exception if the condition does not hold.
18  * <p>
19  * Assertion failure exceptions, like most runtime exceptions, are
20  * thrown when something is misbehaving. Assertion failures are invariably
21  * unspecified behavior; consequently, clients should never rely on
22  * these being thrown (and certainly should not being catching them
23  * specifically).
24  * </p>
25  */

26 public final class Assert {
27     
28     /* This class is not intended to be instantiated. */
29     private Assert() {
30         // not allowed
31
}
32     /** Asserts that an argument is legal. If the given boolean is
33      * not <code>true</code>, an <code>IllegalArgumentException</code>
34      * is thrown.
35      *
36      * @param expression the outcode of the check
37      * @return <code>true</code> if the check passes (does not return
38      * if the check fails)
39      * @exception IllegalArgumentException if the legality test failed
40      */

41     public static boolean isLegal(boolean expression) {
42         return isLegal(expression, ""); //$NON-NLS-1$
43
}
44     /** Asserts that an argument is legal. If the given boolean is
45      * not <code>true</code>, an <code>IllegalArgumentException</code>
46      * is thrown.
47      * The given message is included in that exception, to aid debugging.
48      *
49      * @param expression the outcode of the check
50      * @param message the message to include in the exception
51      * @return <code>true</code> if the check passes (does not return
52      * if the check fails)
53      * @exception IllegalArgumentException if the legality test failed
54      */

55     public static boolean isLegal(boolean expression, String JavaDoc message) {
56         if (!expression)
57             throw new IllegalArgumentException JavaDoc(message);
58         return expression;
59     }
60     /** Asserts that the given object is not <code>null</code>. If this
61      * is not the case, some kind of unchecked exception is thrown.
62      *
63      * @param object the value to test
64      * @exception IllegalArgumentException if the object is <code>null</code>
65      */

66     public static void isNotNull(Object JavaDoc object) {
67         isNotNull(object, ""); //$NON-NLS-1$
68
}
69     /** Asserts that the given object is not <code>null</code>. If this
70      * is not the case, some kind of unchecked exception is thrown.
71      * The given message is included in that exception, to aid debugging.
72      *
73      * @param object the value to test
74      * @param message the message to include in the exception
75      * @exception IllegalArgumentException if the object is <code>null</code>
76      */

77     public static void isNotNull(Object JavaDoc object, String JavaDoc message) {
78         if (object == null)
79             throw new AssertionFailedException("null argument:" + message); //$NON-NLS-1$
80
}
81     /** Asserts that the given boolean is <code>true</code>. If this
82      * is not the case, some kind of unchecked exception is thrown.
83      *
84      * @param expression the outcode of the check
85      * @return <code>true</code> if the check passes (does not return
86      * if the check fails)
87      */

88     public static boolean isTrue(boolean expression) {
89         return isTrue(expression, ""); //$NON-NLS-1$
90
}
91     /** Asserts that the given boolean is <code>true</code>. If this
92      * is not the case, some kind of unchecked exception is thrown.
93      * The given message is included in that exception, to aid debugging.
94      *
95      * @param expression the outcode of the check
96      * @param message the message to include in the exception
97      * @return <code>true</code> if the check passes (does not return
98      * if the check fails)
99      */

100     public static boolean isTrue(boolean expression, String JavaDoc message) {
101         if (!expression)
102             throw new AssertionFailedException("assertion failed: " + message); //$NON-NLS-1$
103
return expression;
104     }
105 }
106
Popular Tags