blob: 6409ad1ade920fe6eea22d8853c2eb9296f7c50e [file] [log] [blame]
david_williamscfdb2cd2004-11-11 08:37:49 +00001/*******************************************************************************
2 * Copyright (c) 2001, 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 * Jens Lukowski/Innoopract - initial renaming/restructuring
11 *
12 *******************************************************************************/
david_williams2aecf082005-04-13 05:03:21 +000013package org.eclipse.wst.sse.core.internal.util;
david_williamscfdb2cd2004-11-11 08:37:49 +000014
15import org.eclipse.wst.sse.core.internal.Logger;
16
17/**
18 * <code>Assert</code> is useful for for embedding runtime sanity checks in
19 * code. The predicate methods all test a condition and throw some type of
20 * unchecked exception if the condition does not hold.
21 * <p>
22 * Assertion failure exceptions, like most runtime exceptions, are thrown when
23 * something is misbehaving. Assertion failures are invariably unspecified
24 * behavior; consequently, clients should never rely on these being thrown
25 * (and certainly should not being catching them specifically).
26 * </p>
27 */
28public final class Assert {
29
30 /**
31 * <code>AssertionFailedException</code> is a runtime exception thrown
32 * by some of the methods in <code>Assert</code>.
33 * <p>
34 * This class is not declared public to prevent some misuses; programs
35 * that catch or otherwise depend on assertion failures are susceptible to
36 * unexpected breakage when assertions in the code are added or removed.
37 * </p>
38 */
39 class AssertionFailedException extends RuntimeException {
40 /**
david_williams895f8882004-11-12 06:59:26 +000041 * Comment for <code>serialVersionUID</code>
42 */
43 private static final long serialVersionUID = 1L;
44
45 /**
david_williamscfdb2cd2004-11-11 08:37:49 +000046 * Constructs a new exception.
47 */
48 public AssertionFailedException() {
david_williams895f8882004-11-12 06:59:26 +000049 super();
david_williamscfdb2cd2004-11-11 08:37:49 +000050 }
51
52 /**
53 * Constructs a new exception with the given message.
54 */
55 public AssertionFailedException(String detail) {
56 super(detail);
57 }
58 }
59
60 /**
61 * Asserts that an argument is legal. If the given boolean is not
62 * <code>true</code>, an <code>IllegalArgumentException</code> is
63 * thrown.
64 *
65 * @param expression
66 * the outcode of the check
67 * @return <code>true</code> if the check passes (does not return if the
68 * check fails)
69 * @exception IllegalArgumentException
70 * if the legality test failed
71 */
72 public static boolean isLegal(boolean expression) {
73 return isLegal(expression, ""); //$NON-NLS-1$
74 }
75
76 /**
77 * Asserts that an argument is legal. If the given boolean is not
78 * <code>true</code>, an <code>IllegalArgumentException</code> is
79 * thrown. The given message is included in that exception, to aid
80 * debugging.
81 *
82 * @param expression
83 * the outcode of the check
84 * @param message
85 * the message to include in the exception
86 * @return <code>true</code> if the check passes (does not return if the
87 * check fails)
88 * @exception IllegalArgumentException
89 * if the legality test failed
90 */
91 public static boolean isLegal(boolean expression, String message) {
92 if (!expression)
93 throw new IllegalArgumentException();
94 return expression;
95 }
96
97 /**
98 * Asserts that the given object is not <code>null</code>. If this is
99 * not the case, some kind of unchecked exception is thrown.
100 *
101 * @param object
102 * the value to test
103 * @exception IllegalArgumentException
104 * if the object is <code>null</code>
105 */
106 public static void isNotNull(Object object) {
107 isNotNull(object, ""); //$NON-NLS-1$
108 }
109
110 /**
111 * Asserts that the given object is not <code>null</code>. If this is
112 * not the case, some kind of unchecked exception is thrown. The given
113 * message is included in that exception, to aid debugging.
114 *
115 * @param object
116 * the value to test
117 * @param message
118 * the message to include in the exception
119 * @exception IllegalArgumentException
120 * if the object is <code>null</code>
121 */
122 public static void isNotNull(Object object, String message) {
123 if (object == null) {
124 Logger.log(Logger.ERROR, "null_argument: " + message); //$NON-NLS-1$
125 throw new Assert().new AssertionFailedException();
126 }
127 }
128
129 /**
130 * Asserts that the given boolean is <code>true</code>. If this is not
131 * the case, some kind of unchecked exception is thrown.
132 *
133 * @param expression
134 * the outcode of the check
135 * @return <code>true</code> if the check passes (does not return if the
136 * check fails)
137 */
138 public static boolean isTrue(boolean expression) {
139 return isTrue(expression, ""); //$NON-NLS-1$
140 }
141
142 /**
143 * Asserts that the given boolean is <code>true</code>. If this is not
144 * the case, some kind of unchecked exception is thrown. The given message
145 * is included in that exception, to aid debugging.
146 *
147 * @param expression
148 * the outcode of the check
149 * @param message
150 * the message to include in the exception
151 * @return <code>true</code> if the check passes (does not return if the
152 * check fails)
153 */
154 public static boolean isTrue(boolean expression, String message) {
155 if (!expression) {
156 Logger.log(Logger.ERROR, "assertion failed: " + message); //$NON-NLS-1$
157 throw new Assert().new AssertionFailedException();
158 }
159 return expression;
160 }
161
162 /* This class is not intended to be instantiated. */
163 private Assert() {
david_williams895f8882004-11-12 06:59:26 +0000164 super();
david_williamscfdb2cd2004-11-11 08:37:49 +0000165 }
166}