david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
amywu | ecebb04 | 2007-04-10 20:07:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2001, 2005 IBM Corporation and others. |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 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 |
amywu | ecebb04 | 2007-04-10 20:07:35 +0000 | [diff] [blame] | 7 | * |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | * Jens Lukowski/Innoopract - initial renaming/restructuring |
| 11 | * |
| 12 | *******************************************************************************/ |
david_williams | 2aecf08 | 2005-04-13 05:03:21 +0000 | [diff] [blame] | 13 | package org.eclipse.wst.sse.core.internal.util; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 14 | |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 15 | |
| 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 | */ |
| 27 | public 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_williams | 895f888 | 2004-11-12 06:59:26 +0000 | [diff] [blame] | 40 | * Comment for <code>serialVersionUID</code> |
| 41 | */ |
| 42 | private static final long serialVersionUID = 1L; |
| 43 | |
| 44 | /** |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 45 | * Constructs a new exception. |
| 46 | */ |
| 47 | public AssertionFailedException() { |
david_williams | 895f888 | 2004-11-12 06:59:26 +0000 | [diff] [blame] | 48 | super(); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 49 | } |
| 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 |
| 62 | * thrown. |
| 63 | * |
| 64 | * @param expression |
| 65 | * the outcode of the check |
| 66 | * @return <code>true</code> if the check passes (does not return if the |
| 67 | * check fails) |
| 68 | * @exception IllegalArgumentException |
| 69 | * if the legality test failed |
| 70 | */ |
| 71 | public static boolean isLegal(boolean expression) { |
| 72 | return isLegal(expression, ""); //$NON-NLS-1$ |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Asserts that an argument is legal. If the given boolean is not |
| 77 | * <code>true</code>, an <code>IllegalArgumentException</code> is |
| 78 | * thrown. The given message is included in that exception, to aid |
| 79 | * debugging. |
| 80 | * |
| 81 | * @param expression |
| 82 | * the outcode of the check |
| 83 | * @param message |
| 84 | * the message to include in the exception |
| 85 | * @return <code>true</code> if the check passes (does not return if the |
| 86 | * check fails) |
| 87 | * @exception IllegalArgumentException |
| 88 | * if the legality test failed |
| 89 | */ |
| 90 | public static boolean isLegal(boolean expression, String message) { |
| 91 | if (!expression) |
| 92 | throw new IllegalArgumentException(); |
| 93 | return expression; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Asserts that the given object is not <code>null</code>. If this is |
| 98 | * not the case, some kind of unchecked exception is thrown. |
| 99 | * |
| 100 | * @param object |
| 101 | * the value to test |
| 102 | * @exception IllegalArgumentException |
| 103 | * if the object is <code>null</code> |
| 104 | */ |
| 105 | public static void isNotNull(Object object) { |
| 106 | isNotNull(object, ""); //$NON-NLS-1$ |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Asserts that the given object is not <code>null</code>. If this is |
| 111 | * not the case, some kind of unchecked exception is thrown. The given |
| 112 | * message is included in that exception, to aid debugging. |
| 113 | * |
| 114 | * @param object |
| 115 | * the value to test |
| 116 | * @param message |
| 117 | * the message to include in the exception |
| 118 | * @exception IllegalArgumentException |
| 119 | * if the object is <code>null</code> |
| 120 | */ |
| 121 | public static void isNotNull(Object object, String message) { |
| 122 | if (object == null) { |
david_williams | b1d67dc | 2005-07-04 02:58:53 +0000 | [diff] [blame] | 123 | //Logger.log(Logger.ERROR, "null_argument: " + message); //$NON-NLS-1$ |
nitind | 24f0209 | 2008-01-31 02:26:19 +0000 | [diff] [blame] | 124 | throw new Assert().new AssertionFailedException(message); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Asserts that the given boolean is <code>true</code>. If this is not |
| 130 | * the case, some kind of unchecked exception is thrown. |
| 131 | * |
| 132 | * @param expression |
| 133 | * the outcode of the check |
| 134 | * @return <code>true</code> if the check passes (does not return if the |
| 135 | * check fails) |
| 136 | */ |
| 137 | public static boolean isTrue(boolean expression) { |
| 138 | return isTrue(expression, ""); //$NON-NLS-1$ |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Asserts that the given boolean is <code>true</code>. If this is not |
| 143 | * the case, some kind of unchecked exception is thrown. The given message |
| 144 | * is included in that exception, to aid debugging. |
| 145 | * |
| 146 | * @param expression |
| 147 | * the outcode of the check |
| 148 | * @param message |
| 149 | * the message to include in the exception |
| 150 | * @return <code>true</code> if the check passes (does not return if the |
| 151 | * check fails) |
| 152 | */ |
| 153 | public static boolean isTrue(boolean expression, String message) { |
| 154 | if (!expression) { |
nitind | 24f0209 | 2008-01-31 02:26:19 +0000 | [diff] [blame] | 155 | throw new Assert().new AssertionFailedException(message); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 156 | } |
| 157 | return expression; |
| 158 | } |
| 159 | |
| 160 | /* This class is not intended to be instantiated. */ |
| 161 | private Assert() { |
david_williams | 895f888 | 2004-11-12 06:59:26 +0000 | [diff] [blame] | 162 | super(); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 163 | } |
| 164 | } |