blob: d42a5b27928f591e00a4e2e48842e2cdb0e0a44f [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_williamsf3680f02005-04-13 22:43:54 +000013package org.eclipse.wst.sse.ui.internal.util;
david_williamscfdb2cd2004-11-11 08:37:49 +000014
david_williamscfdb2cd2004-11-11 08:37:49 +000015
16/**
17 * <code>Assert</code> is useful for for embedding runtime sanity checks in
18 * code. The predicate methods all test a condition and throw some type of
19 * unchecked exception if the condition does not hold.
20 * <p>
21 * Assertion failure exceptions, like most runtime exceptions, are thrown when
22 * something is misbehaving. Assertion failures are invariably unspecified
23 * behavior; consequently, clients should never rely on these being thrown
24 * (and certainly should not being catching them specifically).
25 * </p>
26 */
27public final class Assert {
28
29 /**
30 * <code>AssertionFailedException</code> is a runtime exception thrown
31 * by some of the methods in <code>Assert</code>.
32 * <p>
33 * This class is not declared public to prevent some misuses; programs
34 * that catch or otherwise depend on assertion failures are susceptible to
35 * unexpected breakage when assertions in the code are added or removed.
36 * </p>
37 */
38 class AssertionFailedException extends RuntimeException {
39 /**
david_williams16f26a82004-11-12 07:30:49 +000040 * Comment for <code>serialVersionUID</code>
41 */
42 private static final long serialVersionUID = 1L;
43
44 /**
david_williamscfdb2cd2004-11-11 08:37:49 +000045 * Constructs a new exception.
46 */
47 public AssertionFailedException() {
david_williams16f26a82004-11-12 07:30:49 +000048 super();
david_williamscfdb2cd2004-11-11 08:37:49 +000049 }
50
51 /**
52 * Constructs a new exception with the given message.
53 */
54 public AssertionFailedException(String detail) {
55 super(detail);
56 }
57 }
58
59 /**
60 * Asserts that an argument is legal. If the given boolean is not
61 * <code>true</code>, an <code>IllegalArgumentException</code> is
david_williamscfdb2cd2004-11-11 08:37:49 +000062 * thrown. The given message is included in that exception, to aid
63 * debugging.
64 *
65 * @param expression
66 * the outcode of the check
67 * @param message
68 * the message to include in the exception
69 * @return <code>true</code> if the check passes (does not return if the
70 * check fails)
71 * @exception IllegalArgumentException
72 * if the legality test failed
73 */
74 public static boolean isLegal(boolean expression, String message) {
75 if (!expression)
nitind3a3267b2004-12-28 22:42:24 +000076 throw new IllegalArgumentException(message);
david_williamscfdb2cd2004-11-11 08:37:49 +000077 return expression;
78 }
79
80 /**
81 * Asserts that the given object is not <code>null</code>. If this is
david_williamscfdb2cd2004-11-11 08:37:49 +000082 * not the case, some kind of unchecked exception is thrown. The given
83 * message is included in that exception, to aid debugging.
84 *
85 * @param object
86 * the value to test
87 * @param message
88 * the message to include in the exception
89 * @exception IllegalArgumentException
90 * if the object is <code>null</code>
91 */
92 public static void isNotNull(Object object, String message) {
93 if (object == null) {
nitind3a3267b2004-12-28 22:42:24 +000094 throw new Assert().new AssertionFailedException(message);
david_williamscfdb2cd2004-11-11 08:37:49 +000095 }
96 }
97
98 /**
99 * Asserts that the given boolean is <code>true</code>. If this is not
david_williamscfdb2cd2004-11-11 08:37:49 +0000100 * the case, some kind of unchecked exception is thrown. The given message
101 * is included in that exception, to aid debugging.
102 *
103 * @param expression
104 * the outcode of the check
105 * @param message
106 * the message to include in the exception
107 * @return <code>true</code> if the check passes (does not return if the
108 * check fails)
109 */
110 public static boolean isTrue(boolean expression, String message) {
111 if (!expression) {
nitind3a3267b2004-12-28 22:42:24 +0000112 throw new Assert().new AssertionFailedException(message);
david_williamscfdb2cd2004-11-11 08:37:49 +0000113 }
114 return expression;
115 }
116
117 /* This class is not intended to be instantiated. */
118 private Assert() {
119 }
120}