KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > util > Assert


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

27 public final class Assert {
28     /* This class is not intended to be instantiated. */
29     private Assert() {
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     /** Asserts that an argument is legal. If the given boolean is
44      * not <code>true</code>, an <code>IllegalArgumentException</code>
45      * is thrown.
46      * The given message is included in that exception, to aid debugging.
47      *
48      * @param expression the outcode of the check
49      * @param message the message to include in the exception
50      * @return <code>true</code> if the check passes (does not return
51      * if the check fails)
52      * @exception IllegalArgumentException if the legality test failed
53      */

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

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

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

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

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