KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > core > 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.team.internal.core;
12
13 import org.eclipse.osgi.util.NLS;
14
15  
16
17 public final class Assert {
18     public static class AssertionFailedException extends RuntimeException JavaDoc {
19         // Field required to avoid compiler warning
20
private static final long serialVersionUID = -3361573629971779153L;
21         public AssertionFailedException() {
22         }
23         public AssertionFailedException(String JavaDoc detail) {
24             super(NLS.bind(Messages.Assert_assertionFailed, new String JavaDoc[] { detail })); //$NON-NLS-1$
25
}
26     }
27 /* This class is not intended to be instantiated. */
28 private Assert() {
29 }
30 /** Asserts that an argument is legal. If the given boolean is
31  * not <code>true</code>, an <code>IllegalArgumentException</code>
32  * is thrown.
33  *
34  * @param expression the outcode of the check
35  * @return <code>true</code> if the check passes (does not return
36  * if the check fails)
37  * @exception IllegalArgumentException if the legality test failed
38  */

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

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

64 public static void isNotNull(Object JavaDoc object) {
65     if (object == null)
66         throw new AssertionFailedException("null argument"); //$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:" /*non NLS*/ + 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, ""/*nonNLS*/); //$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("assert failed:" /*non NLS*/ + message); //$NON-NLS-1$
102
return expression;
103 }
104 /**
105  * Indicates that the caller has not implemented the method.
106  * Usually this is a temporary condition.
107  */

108 public static void notYetImplemented() {
109 }
110 }
111
Popular Tags