001 /*--------------------------------------------------------------------------+
002 $Id: CCSMAssert.java 26283 2010-02-18 11:18:57Z juergens $
003 | |
004 | Copyright 2005-2010 Technische Universitaet Muenchen |
005 | |
006 | Licensed under the Apache License, Version 2.0 (the "License"); |
007 | you may not use this file except in compliance with the License. |
008 | You may obtain a copy of the License at |
009 | |
010 | http://www.apache.org/licenses/LICENSE-2.0 |
011 | |
012 | Unless required by applicable law or agreed to in writing, software |
013 | distributed under the License is distributed on an "AS IS" BASIS, |
014 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015 | See the License for the specific language governing permissions and |
016 | limitations under the License. |
017 +--------------------------------------------------------------------------*/
018 package edu.tum.cs.commons.assertion;
019
020 /**
021 * This class provides simple methods to implement assertions. Please refer to
022 * the {@linkplain edu.tum.cs.commons.assertion package documentation} for a
023 * discussion of assertions vs preconditions.
024 *
025 * @author deissenb
026 * @author $Author: juergens $
027 * @version $Rev: 26283 $
028 * @levd.rating GREEN Hash: E82E611F994E400B617A0A0BB1D27C39
029 */
030 public class CCSMAssert {
031
032 /**
033 * Checks if a condition is <code>true</code>.
034 *
035 * @param condition
036 * condition to check
037 * @param message
038 * exception message
039 * @throws AssertionError
040 * if the condition if <code>false</code>
041 */
042 public static void isTrue(boolean condition, String message)
043 throws AssertionError {
044 if (!condition) {
045 throw new AssertionError(message);
046 }
047 }
048
049 /**
050 * Checks if a condition is <code>false</code>.
051 *
052 * @param condition
053 * condition to check
054 * @param message
055 * exception message
056 * @throws AssertionError
057 * if the condition if <code>true</code>
058 */
059 public static void isFalse(boolean condition, String message)
060 throws AssertionError {
061 if (condition) {
062 throw new AssertionError(message);
063 }
064 }
065
066 /** Checks that the object is a instance of the class */
067 public static void isInstanceOf(Object object, Class<?> clazz) {
068 CCSMAssert.isTrue(clazz.isInstance(object), object
069 + " must be instance of " + clazz);
070 }
071
072 /**
073 * @throws AssertionError
074 * with message
075 */
076 public static void fail(String message) throws AssertionError {
077 throw new AssertionError(message);
078 }
079
080 /**
081 * Checks whether a reference is <code>null</code>.
082 *
083 * @param reference
084 * reference to check
085 * @throws AssertionError
086 * if the reference is <code>null</code>
087 */
088 public static void isNotNull(Object reference) throws AssertionError {
089 isNotNull(reference, "Reference must not be null");
090 }
091
092 /**
093 * Checks whether a reference is <code>null</code>.
094 *
095 * @param reference
096 * reference to check
097 * @param message
098 * exception message
099 * @throws AssertionError
100 * if the reference is <code>null</code>
101 */
102 public static void isNotNull(Object reference, String message)
103 throws AssertionError {
104 if (reference == null) {
105 throw new AssertionError(message);
106 }
107 }
108
109 }