KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > runtime > 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.core.internal.runtime;
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         // not allowed
30
}
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
45     /** Asserts that an argument is legal. If the given boolean is
46      * not <code>true</code>, an <code>IllegalArgumentException</code>
47      * is thrown.
48      * The given message is included in that exception, to aid debugging.
49      *
50      * @param expression the outcode of the check
51      * @param message the message to include in the exception
52      * @return <code>true</code> if the check passes (does not return
53      * if the check fails)
54      * @exception IllegalArgumentException if the legality test failed
55      */

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

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

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

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

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