KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > runtime > Assert


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.core.runtime;
12
13 /**
14  * <code>Assert</code> is useful for for embedding runtime sanity checks
15  * in code. The predicate methods all test a condition and throw some
16  * type of unchecked exception if the condition does not hold.
17  * <p>
18  * Assertion failure exceptions, like most runtime exceptions, are
19  * thrown when something is misbehaving. Assertion failures are invariably
20  * unspecified behavior; consequently, clients should never rely on
21  * these being thrown (and certainly should not being catching them
22  * specifically).
23  * </p><p>
24  * This class can be used without OSGi running.
25  * </p><p>
26  * This class is not intended to be instantiated or sub-classed by clients.
27  * </p>
28  * @since org.eclipse.equinox.common 3.2
29  */

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

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

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

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

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

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

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