4
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2006 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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.util.Assert;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17 /**
|
|
18 * <code>Assert</code> is useful for for embedding runtime sanity checks
|
|
19 * in code. The static predicate methods all test a condition and throw some
|
|
20 * type of unchecked exception if the condition does not hold.
|
|
21 * <p>
|
|
22 * Assertion failure exceptions, like most runtime exceptions, are
|
|
23 * thrown when something is misbehaving. Assertion failures are invariably
|
|
24 * unspecified behavior; consequently, clients should never rely on
|
|
25 * these being thrown (or not thrown). <b>If you find yourself in the
|
|
26 * position where you need to catch an assertion failure, you have most
|
|
27 * certainly written your program incorrectly.</b>
|
|
28 * </p>
|
|
29 * <p>
|
|
30 * Note that an <code>assert</code> statement is slated to be added to the
|
|
31 * Java language in JDK 1.4, rending this class obsolete.
|
|
32 *
|
|
33 * @deprecated As of 3.3, replaced by {@link dwtx.core.runtime.Assert}
|
|
34 * </p>
|
|
35 */
|
|
36 public final class Assert {
|
|
37
|
|
38 /**
|
|
39 * <code>AssertionFailedException</code> is a runtime exception thrown
|
|
40 * by some of the methods in <code>Assert</code>.
|
|
41 * <p>
|
|
42 * This class is not declared public to prevent some misuses; programs that catch
|
|
43 * or otherwise depend on assertion failures are susceptible to unexpected
|
|
44 * breakage when assertions in the code are added or removed.
|
|
45 * </p>
|
|
46 */
|
|
47 private static class AssertionFailedException : RuntimeException {
|
|
48
|
|
49 /**
|
|
50 * Generated serial version UID for this class.
|
|
51 * @since 3.1
|
|
52 */
|
|
53 private static final long serialVersionUID = 3257852073508024376L;
|
|
54
|
|
55 /**
|
|
56 * Constructs a new exception.
|
|
57 */
|
|
58 public this() {
|
|
59 }
|
|
60
|
|
61 /**
|
|
62 * Constructs a new exception with the given message.
|
|
63 * @param detail the detail message
|
|
64 */
|
|
65 public this(String detail) {
|
|
66 super(detail);
|
|
67 }
|
|
68 }
|
|
69
|
|
70 /* This class is not intended to be instantiated. */
|
|
71 private this() {
|
|
72 }
|
|
73
|
|
74 /**
|
|
75 * Asserts that an argument is legal. If the given bool is
|
|
76 * not <code>true</code>, an <code>IllegalArgumentException</code>
|
|
77 * is thrown.
|
|
78 *
|
|
79 * @param expression the outcome of the check
|
|
80 * @return <code>true</code> if the check passes (does not return
|
|
81 * if the check fails)
|
|
82 * @exception IllegalArgumentException if the legality test failed
|
|
83 */
|
|
84 public static bool isLegal(bool expression) {
|
|
85 // succeed as quickly as possible
|
|
86 if (expression) {
|
|
87 return true;
|
|
88 }
|
|
89 return isLegal(expression, "");//$NON-NLS-1$
|
|
90 }
|
|
91
|
|
92 /**
|
|
93 * Asserts that an argument is legal. If the given bool is
|
|
94 * not <code>true</code>, an <code>IllegalArgumentException</code>
|
|
95 * is thrown.
|
|
96 * The given message is included in that exception, to aid debugging.
|
|
97 *
|
|
98 * @param expression the outcome of the check
|
|
99 * @param message the message to include in the exception
|
|
100 * @return <code>true</code> if the check passes (does not return
|
|
101 * if the check fails)
|
|
102 * @exception IllegalArgumentException if the legality test failed
|
|
103 */
|
|
104 public static bool isLegal(bool expression, String message) {
|
|
105 if (!expression) {
|
|
106 throw new IllegalArgumentException("assertion failed; " ~ message); //$NON-NLS-1$
|
|
107 }
|
|
108 return expression;
|
|
109 }
|
|
110
|
|
111 /**
|
|
112 * Asserts that the given object is not <code>null</code>. If this
|
|
113 * is not the case, some kind of unchecked exception is thrown.
|
|
114 * <p>
|
|
115 * As a general rule, parameters passed to API methods must not be
|
|
116 * <code>null</code> unless <b>explicitly</b> allowed in the method's
|
|
117 * specification. Similarly, results returned from API methods are never
|
|
118 * <code>null</code> unless <b>explicitly</b> allowed in the method's
|
|
119 * specification. Implementations are encouraged to make regular use of
|
|
120 * <code>Assert.isNotNull</code> to ensure that <code>null</code>
|
|
121 * parameters are detected as early as possible.
|
|
122 * </p>
|
|
123 *
|
|
124 * @param object the value to test
|
|
125 * @exception AssertionFailedException an unspecified unchecked exception if the object
|
|
126 * is <code>null</code>
|
|
127 */
|
|
128 public static void isNotNull(Object object) {
|
|
129 // succeed as quickly as possible
|
|
130 if (object !is null) {
|
|
131 return;
|
|
132 }
|
|
133 isNotNull(object, "");//$NON-NLS-1$
|
|
134 }
|
|
135
|
|
136 /**
|
|
137 * Asserts that the given object is not <code>null</code>. If this
|
|
138 * is not the case, some kind of unchecked exception is thrown.
|
|
139 * The given message is included in that exception, to aid debugging.
|
|
140 * <p>
|
|
141 * As a general rule, parameters passed to API methods must not be
|
|
142 * <code>null</code> unless <b>explicitly</b> allowed in the method's
|
|
143 * specification. Similarly, results returned from API methods are never
|
|
144 * <code>null</code> unless <b>explicitly</b> allowed in the method's
|
|
145 * specification. Implementations are encouraged to make regular use of
|
|
146 * <code>Assert.isNotNull</code> to ensure that <code>null</code>
|
|
147 * parameters are detected as early as possible.
|
|
148 * </p>
|
|
149 *
|
|
150 * @param object the value to test
|
|
151 * @param message the message to include in the exception
|
|
152 * @exception AssertionFailedException an unspecified unchecked exception if the object
|
|
153 * is <code>null</code>
|
|
154 */
|
|
155 public static void isNotNull(Object object, String message) {
|
|
156 if (object is null) {
|
|
157 throw new AssertionFailedException("null argument;" ~ message);//$NON-NLS-1$
|
|
158 }
|
|
159 }
|
|
160
|
|
161 /**
|
|
162 * Asserts that the given bool is <code>true</code>. If this
|
|
163 * is not the case, some kind of unchecked exception is thrown.
|
|
164 *
|
|
165 * @param expression the outcome of the check
|
|
166 * @return <code>true</code> if the check passes (does not return
|
|
167 * if the check fails)
|
|
168 */
|
|
169 public static bool isTrue(bool expression) {
|
|
170 // succeed as quickly as possible
|
|
171 if (expression) {
|
|
172 return true;
|
|
173 }
|
|
174 return isTrue(expression, "");//$NON-NLS-1$
|
|
175 }
|
|
176
|
|
177 /**
|
|
178 * Asserts that the given bool is <code>true</code>. If this
|
|
179 * is not the case, some kind of unchecked exception is thrown.
|
|
180 * The given message is included in that exception, to aid debugging.
|
|
181 *
|
|
182 * @param expression the outcome of the check
|
|
183 * @param message the message to include in the exception
|
|
184 * @return <code>true</code> if the check passes (does not return
|
|
185 * if the check fails)
|
|
186 */
|
|
187 public static bool isTrue(bool expression, String message) {
|
|
188 if (!expression) {
|
|
189 throw new AssertionFailedException("Assertion failed: " ~ message);//$NON-NLS-1$
|
|
190 }
|
|
191 return expression;
|
|
192 }
|
|
193 }
|