KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > util > 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.jface.util;
12
13 /**
14  * <code>Assert</code> is useful for for embedding runtime sanity checks
15  * in code. The static 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 (or not thrown). <b>If you find yourself in the
22  * position where you need to catch an assertion failure, you have most
23  * certainly written your program incorrectly.</b>
24  * </p>
25  * <p>
26  * Note that an <code>assert</code> statement is slated to be added to the
27  * Java language in JDK 1.4, rending this class obsolete.
28  *
29  * @deprecated As of 3.3, replaced by {@link org.eclipse.core.runtime.Assert}
30  * </p>
31  */

32 public final class Assert {
33
34     /**
35      * <code>AssertionFailedException</code> is a runtime exception thrown
36      * by some of the methods in <code>Assert</code>.
37      * <p>
38      * This class is not declared public to prevent some misuses; programs that catch
39      * or otherwise depend on assertion failures are susceptible to unexpected
40      * breakage when assertions in the code are added or removed.
41      * </p>
42      */

43     private static class AssertionFailedException extends RuntimeException JavaDoc {
44
45         /**
46          * Generated serial version UID for this class.
47          * @since 3.1
48          */

49         private static final long serialVersionUID = 3257852073508024376L;
50
51         /**
52          * Constructs a new exception.
53          */

54         public AssertionFailedException() {
55         }
56
57         /**
58          * Constructs a new exception with the given message.
59          * @param detail the detail message
60          */

61         public AssertionFailedException(String JavaDoc detail) {
62             super(detail);
63         }
64     }
65
66     /* This class is not intended to be instantiated. */
67     private Assert() {
68     }
69
70     /**
71      * Asserts that an argument is legal. If the given boolean is
72      * not <code>true</code>, an <code>IllegalArgumentException</code>
73      * is thrown.
74      *
75      * @param expression the outcome of the check
76      * @return <code>true</code> if the check passes (does not return
77      * if the check fails)
78      * @exception IllegalArgumentException if the legality test failed
79      */

80     public static boolean isLegal(boolean expression) {
81         // succeed as quickly as possible
82
if (expression) {
83             return true;
84         }
85         return isLegal(expression, "");//$NON-NLS-1$
86
}
87
88     /**
89      * Asserts that an argument is legal. If the given boolean is
90      * not <code>true</code>, an <code>IllegalArgumentException</code>
91      * is thrown.
92      * The given message is included in that exception, to aid debugging.
93      *
94      * @param expression the outcome 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      * @exception IllegalArgumentException if the legality test failed
99      */

100     public static boolean isLegal(boolean expression, String JavaDoc message) {
101         if (!expression) {
102             throw new IllegalArgumentException JavaDoc("assertion failed; " + message); //$NON-NLS-1$
103
}
104         return expression;
105     }
106
107     /**
108      * Asserts that the given object is not <code>null</code>. If this
109      * is not the case, some kind of unchecked exception is thrown.
110      * <p>
111      * As a general rule, parameters passed to API methods must not be
112      * <code>null</code> unless <b>explicitly</b> allowed in the method's
113      * specification. Similarly, results returned from API methods are never
114      * <code>null</code> unless <b>explicitly</b> allowed in the method's
115      * specification. Implementations are encouraged to make regular use of
116      * <code>Assert.isNotNull</code> to ensure that <code>null</code>
117      * parameters are detected as early as possible.
118      * </p>
119      *
120      * @param object the value to test
121      * @exception AssertionFailedException an unspecified unchecked exception if the object
122      * is <code>null</code>
123      */

124     public static void isNotNull(Object JavaDoc object) {
125         // succeed as quickly as possible
126
if (object != null) {
127             return;
128         }
129         isNotNull(object, "");//$NON-NLS-1$
130
}
131
132     /**
133      * Asserts that the given object is not <code>null</code>. If this
134      * is not the case, some kind of unchecked exception is thrown.
135      * The given message is included in that exception, to aid debugging.
136      * <p>
137      * As a general rule, parameters passed to API methods must not be
138      * <code>null</code> unless <b>explicitly</b> allowed in the method's
139      * specification. Similarly, results returned from API methods are never
140      * <code>null</code> unless <b>explicitly</b> allowed in the method's
141      * specification. Implementations are encouraged to make regular use of
142      * <code>Assert.isNotNull</code> to ensure that <code>null</code>
143      * parameters are detected as early as possible.
144      * </p>
145      *
146      * @param object the value to test
147      * @param message the message to include in the exception
148      * @exception AssertionFailedException an unspecified unchecked exception if the object
149      * is <code>null</code>
150      */

151     public static void isNotNull(Object JavaDoc object, String JavaDoc message) {
152         if (object == null) {
153             throw new AssertionFailedException("null argument;" + message);//$NON-NLS-1$
154
}
155     }
156
157     /**
158      * Asserts that the given boolean is <code>true</code>. If this
159      * is not the case, some kind of unchecked exception is thrown.
160      *
161      * @param expression the outcome of the check
162      * @return <code>true</code> if the check passes (does not return
163      * if the check fails)
164      */

165     public static boolean isTrue(boolean expression) {
166         // succeed as quickly as possible
167
if (expression) {
168             return true;
169         }
170         return isTrue(expression, "");//$NON-NLS-1$
171
}
172
173     /**
174      * Asserts that the given boolean is <code>true</code>. If this
175      * is not the case, some kind of unchecked exception is thrown.
176      * The given message is included in that exception, to aid debugging.
177      *
178      * @param expression the outcome of the check
179      * @param message the message to include in the exception
180      * @return <code>true</code> if the check passes (does not return
181      * if the check fails)
182      */

183     public static boolean isTrue(boolean expression, String JavaDoc message) {
184         if (!expression) {
185             throw new AssertionFailedException("Assertion failed: " + message);//$NON-NLS-1$
186
}
187         return expression;
188     }
189 }
190
Popular Tags