KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > commands > util > Assert


1 /*******************************************************************************
2  * Copyright (c) 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.commands.util;
12
13 /**
14  * Used to sanity check runtime conditions. Assertion failures typically occur
15  * when something is being misused, such as calling a method with illegal
16  * arguments. Runtime exceptions are thrown. Once an assertion failure occurs,
17  * further behavior is unspecified.
18  *
19  * @since 3.1
20  */

21 public final class Assert {
22
23     /**
24      * Asserts that the given object is not <code>null</code>. If this is not
25      * the case, some kind of unchecked exception is thrown.
26      *
27      * @param object
28      * the value to test
29      * @exception IllegalArgumentException
30      * if the object is <code>null</code>
31      */

32     public static void isNotNull(Object JavaDoc object) {
33         isNotNull(object, ""); //$NON-NLS-1$
34
}
35
36     /**
37      * Asserts that the given object is not <code>null</code>. If this is not
38      * the case, some kind of unchecked exception is thrown. The given message
39      * is included in that exception, to aid debugging.
40      *
41      * @param object
42      * the value to test
43      * @param message
44      * the message to include in the exception
45      * @exception IllegalArgumentException
46      * if the object is <code>null</code>
47      */

48     public static void isNotNull(Object JavaDoc object, String JavaDoc message) {
49         if (object == null)
50             throw new IllegalArgumentException JavaDoc("null argument:" + message); //$NON-NLS-1$
51
}
52
53     /**
54      * Asserts that the given expression is true. If this is not the case, some
55      * kind of unchecked exception is thrown.
56      *
57      * @param expression
58      * the expression to test
59      * @return <code>true</code> if the expression is true.
60      * @exception IllegalArgumentException
61      * if the expression is not true
62      */

63     public static boolean isTrue(boolean expression) {
64         return isTrue(expression, ""); //$NON-NLS-1$
65
}
66
67     /**
68      * Asserts that the given expression is true. If this is not the case, some
69      * kind of unchecked exception is thrown. The given message is included in
70      * that exception, to aid debugging.
71      *
72      * @param expression
73      * the expression to test
74      * @param message
75      * the message to include in the exception
76      * @return <code>true</code> if the expression is true.
77      *
78      * @exception IllegalArgumentException
79      * if the expression is not true
80      */

81     public static boolean isTrue(boolean expression, String JavaDoc message) {
82         if (!expression)
83             throw new IllegalArgumentException JavaDoc("assertion failed:" + message); //$NON-NLS-1$
84
return expression;
85     }
86
87     private Assert() {
88         // This class should not be instantiated.
89
}
90 }
91
Popular Tags