comparison dwtx/jface/text/Assert.d @ 129:eb30df5ca28b

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