2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.corext;
8 // org.phpeclipse.phpdt.internal.corext.refactoring.RefactoringCoreMessages;
11 * <code>Assert</code> is useful for for embedding runtime sanity checks in
12 * code. The static predicate methods all test a condition and throw some type
13 * of unchecked exception if the condition does not hold.
15 * Assertion failure exceptions, like most runtime exceptions, are thrown when
16 * something is misbehaving. Assertion failures are invariably unspecified
17 * behavior; consequently, clients should never rely on these being thrown (or
18 * not thrown). <b>If you find yourself in the position where you need to catch
19 * an assertion failure, you have most certainly written your program
23 * Note that an <code>assert</code> statement is slated to be added to the
24 * Java language in JDK 1.4, rending this class obsolete.
27 public final class Assert {
30 * <code>AssertionFailedException</code> is a runtime exception thrown by
31 * some of the methods in <code>Assert</code>.
33 * This class is not declared public to prevent some misuses; programs that
34 * catch or otherwise depend on assertion failures are susceptible to
35 * unexpected breakage when assertions in the code are added or removed.
38 public static class AssertionFailedException extends RuntimeException {
41 * Constructs a new exception.
43 public AssertionFailedException() {
47 * Constructs a new exception with the given message.
49 public AssertionFailedException(String detail) {
54 /* This class is not intended to be instantiated. */
59 * Asserts that the given object is not <code>null</code>. If this is not
60 * the case, some kind of unchecked exception is thrown.
62 * As a general rule, parameters passed to API methods must not be
63 * <code>null</code> unless <b>explicitly</b> allowed in the method's
64 * specification. Similarly, results returned from API methods are never
65 * <code>null</code> unless <b>explicitly</b> allowed in the method's
66 * specification. Implementations are encouraged to make regular use of
67 * <code>Assert.isNotNull</code> to ensure that <code>null</code>
68 * parameters are detected as early as possible.
74 public static void isNotNull(Object object) {
75 // succeed as quickly as possible
79 isNotNull(object, ""); //$NON-NLS-1$
83 * Asserts that the given object is not <code>null</code>. If this is not
84 * the case, some kind of unchecked exception is thrown. The given message
85 * is included in that exception, to aid debugging.
87 * As a general rule, parameters passed to API methods must not be
88 * <code>null</code> unless <b>explicitly</b> allowed in the method's
89 * specification. Similarly, results returned from API methods are never
90 * <code>null</code> unless <b>explicitly</b> allowed in the method's
91 * specification. Implementations are encouraged to make regular use of
92 * <code>Assert.isNotNull</code> to ensure that <code>null</code>
93 * parameters are detected as early as possible.
99 * the message to include in the exception
101 public static void isNotNull(Object object, String message) {
103 throw new AssertionFailedException("nullargument" + message); //$NON-NLS-1$
107 * Asserts that the given boolean is <code>true</code>. If this is not
108 * the case, some kind of unchecked exception is thrown.
111 * the outcome of the check
112 * @return <code>true</code> if the check passes (does not return if the
115 public static boolean isTrue(boolean expression) {
116 // succeed as quickly as possible
120 return isTrue(expression, ""); //$NON-NLS-1$
124 * Asserts that the given boolean is <code>true</code>. If this is not
125 * the case, some kind of unchecked exception is thrown. The given message
126 * is included in that exception, to aid debugging.
129 * the outcome of the check
131 * the message to include in the exception
132 * @return <code>true</code> if the check passes (does not return if the
135 public static boolean isTrue(boolean expression, String message) {
137 throw new AssertionFailedException("assertion failed" + message); //$NON-NLS-1$