KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > utils > Assert


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.utils;
12
13 import org.eclipse.osgi.util.NLS;
14
15 /**
16  * <code>Assert</code> is useful for for embedding runtime sanity checks
17  * in code.
18  * The predicate methods all test a condition and throw some
19  * type of unchecked exception if the condition does not hold.
20  * <p>
21  * Assertion failure exceptions, like most runtime exceptions, are
22  * thrown when something is misbehaving. Assertion failures are invariably
23  * unspecified behavior; consequently, clients should never rely on
24  * these being thrown (and certainly should not being catching them
25  * specifically).
26  * </p>
27  */

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

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

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

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

82     public static void isNotNull(Object JavaDoc object, String JavaDoc message) {
83         if (object == null)
84             throw new AssertionFailedException(NLS.bind(Messages.utils_null, message));
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(NLS.bind(Messages.utils_failed, message));
110         return expression;
111     }
112 }
113
Popular Tags