Refactory: removed unnecessary local variables and imports.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / Assert.java
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 Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
13 /* This class is not intended to be instantiated. */
14 public final class Assert {
15
16         private Assert() {
17                 // cannot be instantiated
18         }
19
20         /**
21          * Asserts that an argument is legal. If the given boolean is not
22          * <code>true</code>, an <code>IllegalArgumentException</code> is
23          * thrown.
24          * 
25          * @param expression
26          *            the outcode of the check
27          * @return <code>true</code> if the check passes (does not return if the
28          *         check fails)
29          * @exception IllegalArgumentException
30          *                if the legality test failed
31          */
32         public static boolean isLegal(boolean expression) {
33                 return isLegal(expression, ""); //$NON-NLS-1$
34         }
35
36         /**
37          * Asserts that an argument is legal. If the given boolean is not
38          * <code>true</code>, an <code>IllegalArgumentException</code> is
39          * thrown. The given message is included in that exception, to aid
40          * debugging.
41          * 
42          * @param expression
43          *            the outcode of the check
44          * @param message
45          *            the message to include in the exception
46          * @return <code>true</code> if the check passes (does not return if the
47          *         check fails)
48          * @exception IllegalArgumentException
49          *                if the legality test failed
50          */
51         public static boolean isLegal(boolean expression, String message) {
52                 if (!expression)
53                         throw new IllegalArgumentException(message);
54                 return expression;
55         }
56
57         /**
58          * Asserts that the given object is not <code>null</code>. If this is not
59          * the case, some kind of unchecked exception is thrown.
60          * 
61          * @param object
62          *            the value to test
63          * @exception IllegalArgumentException
64          *                if the object is <code>null</code>
65          */
66         public static void isNotNull(Object object) {
67                 isNotNull(object, ""); //$NON-NLS-1$
68         }
69
70         /**
71          * Asserts that the given object is not <code>null</code>. If this is not
72          * the case, some kind of unchecked exception is thrown. The given message
73          * is included in that exception, to aid debugging.
74          * 
75          * @param object
76          *            the value to test
77          * @param message
78          *            the message to include in the exception
79          * @exception IllegalArgumentException
80          *                if the object is <code>null</code>
81          */
82         public static void isNotNull(Object object, String message) {
83                 if (object == null)
84                         throw new AssertionFailedException("null argument; " + message); //$NON-NLS-1$
85         }
86
87         /**
88          * Asserts that the given boolean is <code>true</code>. If this is not
89          * the case, some kind of unchecked exception is thrown.
90          * 
91          * @param expression
92          *            the outcode of the check
93          * @return <code>true</code> if the check passes (does not return if the
94          *         check fails)
95          */
96         public static boolean isTrue(boolean expression) {
97                 return isTrue(expression, ""); //$NON-NLS-1$
98         }
99
100         /**
101          * Asserts that the given boolean is <code>true</code>. If this is not
102          * the case, some kind of unchecked exception is thrown. The given message
103          * is included in that exception, to aid debugging.
104          * 
105          * @param expression
106          *            the outcode of the check
107          * @param message
108          *            the message to include in the exception
109          * @return <code>true</code> if the check passes (does not return if the
110          *         check fails)
111          */
112         public static boolean isTrue(boolean expression, String message) {
113                 if (!expression)
114                         throw new AssertionFailedException("Assertion failed; " + message); //$NON-NLS-1$
115                 return expression;
116         }
117
118         public static class AssertionFailedException extends RuntimeException {
119                 public AssertionFailedException(String detail) {
120                         super(detail);
121                 }
122         }
123 }