001 /*--------------------------------------------------------------------------+
002 $Id: CCSMPre.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 check preconditions. Please see refer
022 * to 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: 4BF2AFF0A3284F2E41474F6ADB98344E
029 */
030 public class CCSMPre {
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 PreconditionException
040 * if the condition is <code>false</code>
041 */
042 public static void isTrue(boolean condition, String message)
043 throws PreconditionException {
044 if (!condition) {
045 throw new PreconditionException(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 PreconditionException
057 * if the condition is <code>true</code>
058 */
059 public static void isFalse(boolean condition, String message)
060 throws PreconditionException {
061 if (condition) {
062 throw new PreconditionException(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 isTrue(clazz.isInstance(object), object + " must be instance of "
069 + clazz);
070 }
071
072 /**
073 * Throws a {@link PreconditionException} with the provided message.
074 */
075 public static void fail(String message) throws PreconditionException {
076 throw new PreconditionException(message);
077 }
078
079 /**
080 * Checks whether a reference is <code>null</code>.
081 *
082 * @param reference
083 * reference to check
084 * @throws AssertionError
085 * if the reference is <code>null</code>
086 */
087 public static void isNotNull(Object reference) throws AssertionError {
088 isNotNull(reference, "Reference must not be null");
089 }
090
091 /**
092 * Checks whether a reference is <code>null</code>.
093 *
094 * @param reference
095 * reference to check
096 * @param message
097 * exception message
098 * @throws AssertionError
099 * if the reference is <code>null</code>
100 */
101 public static void isNotNull(Object reference, String message)
102 throws AssertionError {
103 if (reference == null) {
104 throw new AssertionError(message);
105 }
106 }
107
108 }