david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 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_williams | f3680f0 | 2005-04-13 22:43:54 +0000 | [diff] [blame] | 13 | package org.eclipse.wst.sse.ui.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 | 16f26a8 | 2004-11-12 07:30:49 +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 | 16f26a8 | 2004-11-12 07:30:49 +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 |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 62 | * 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) |
nitind | 3a3267b | 2004-12-28 22:42:24 +0000 | [diff] [blame] | 76 | throw new IllegalArgumentException(message); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 77 | return expression; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Asserts that the given object is not <code>null</code>. If this is |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 82 | * 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) { |
nitind | 3a3267b | 2004-12-28 22:42:24 +0000 | [diff] [blame] | 94 | throw new Assert().new AssertionFailedException(message); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Asserts that the given boolean is <code>true</code>. If this is not |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 100 | * 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) { |
nitind | 3a3267b | 2004-12-28 22:42:24 +0000 | [diff] [blame] | 112 | throw new Assert().new AssertionFailedException(message); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 113 | } |
| 114 | return expression; |
| 115 | } |
| 116 | |
| 117 | /* This class is not intended to be instantiated. */ |
| 118 | private Assert() { |
| 119 | } |
| 120 | } |