3m9 compatible;
[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 /** Asserts that an argument is legal. If the given boolean is
20  * not <code>true</code>, an <code>IllegalArgumentException</code>
21  * is thrown.
22  *
23  * @param expression the outcode of the check
24  * @return <code>true</code> if the check passes (does not return
25  *    if the check fails)
26  * @exception IllegalArgumentException if the legality test failed
27  */
28 public static boolean isLegal(boolean expression) {
29         return isLegal(expression, ""); //$NON-NLS-1$
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  * The given message is included in that exception, to aid debugging.
35  *
36  * @param expression the outcode of the check
37  * @param message the message to include in the exception
38  * @return <code>true</code> if the check passes (does not return
39  *    if the check fails)
40  * @exception IllegalArgumentException if the legality test failed
41  */
42 public static boolean isLegal(boolean expression, String message) {
43         if (!expression)
44                 throw new IllegalArgumentException(message);
45         return expression;
46 }
47 /** Asserts that the given object is not <code>null</code>. If this
48  * is not the case, some kind of unchecked exception is thrown.
49  * 
50  * @param object the value to test
51  * @exception IllegalArgumentException if the object is <code>null</code>
52  */
53 public static void isNotNull(Object object) {
54         isNotNull(object, ""); //$NON-NLS-1$
55 }
56 /** Asserts that the given object is not <code>null</code>. If this
57  * is not the case, some kind of unchecked exception is thrown.
58  * The given message is included in that exception, to aid debugging.
59  *
60  * @param object the value to test
61  * @param message the message to include in the exception
62  * @exception IllegalArgumentException if the object is <code>null</code>
63  */
64 public static void isNotNull(Object object, String message) {
65         if (object == null)
66                 throw new AssertionFailedException("null argument; " + message); //$NON-NLS-1$
67 }
68 /** Asserts that the given boolean is <code>true</code>. If this
69  * is not the case, some kind of unchecked exception is thrown.
70  *
71  * @param expression the outcode of the check
72  * @return <code>true</code> if the check passes (does not return
73  *    if the check fails)
74  */
75 public static boolean isTrue(boolean expression) {
76         return isTrue(expression, ""); //$NON-NLS-1$
77 }
78 /** Asserts that the given boolean is <code>true</code>. If this
79  * is not the case, some kind of unchecked exception is thrown.
80  * The given message is included in that exception, to aid debugging.
81  *
82  * @param expression the outcode of the check
83  * @param message the message to include in the exception
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, String message) {
88         if (!expression)
89                 throw new AssertionFailedException("Assertion failed; " + message); //$NON-NLS-1$
90         return expression;
91 }
92
93         public static class AssertionFailedException extends RuntimeException {
94                 public AssertionFailedException(String detail) {
95                         super(detail);
96                 }
97         }
98 }