KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > misc > 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.ui.internal.misc;
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     /* This class is not intended to be instantiated. */
28     private Assert() {
29     }
30
31     /** Asserts that an argument is legal. If the given boolean is
32      * not <code>true</code>, an <code>IllegalArgumentException</code>
33      * is thrown.
34      *
35      * @param expression the outcode of the check
36      * @return <code>true</code> if the check passes (does not return
37      * if the check fails)
38      * @exception IllegalArgumentException if the legality test failed
39      */

40     public static boolean isLegal(boolean expression) {
41         return isLegal(expression, "");//$NON-NLS-1$
42
}
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();
58         return expression;
59     }
60
61     /** Asserts that the given object is not <code>null</code>. If this
62      * is not the case, some kind of unchecked exception is thrown.
63      *
64      * @param object the value to test
65      * @exception IllegalArgumentException if the object is <code>null</code>
66      */

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

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

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

104     public static boolean isTrue(boolean expression, String JavaDoc message) {
105         if (!expression)
106             throw new AssertionFailedException("assertion failed; " + message);//$NON-NLS-1$
107
return expression;
108     }
109 }
110
Popular Tags