/**************************************************************************** * This software is part of the gCube Project. * Site: http://www.gcube-system.org/ **************************************************************************** * The gCube/gCore software is licensed as Free Open Source software * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). * The software and documentation is provided by its authors/distributors * "as is" and no expressed or * implied warranty is given for its use, quality or fitness for a * particular case. **************************************************************************** * Filename: Assertion.java **************************************************************************** * @author Daniele Strollo ***************************************************************************/ package org.gcube.resourcemanagement.support.server.utils; import java.io.Serializable; /** * General purpose assertion handler. * Assertion can be generalized to check a boolean expression and * to raise an exception in correspondence to a failure happening * during checking. *
 * Example:
 *
 *     Assertion<TheExceptionType> assertion = new Assertion<ParamException> ();
 *     assertion.validate (param != null, new TheExceptionType("invalid parameter null"));
 *
 * or, in a more compact form:
 *    // The exception to throw in case of failure
 *    // during the evaluation of the expected condition
 *    new Assertion<TheExceptionType>().validate(
 *    	i>5,                                                     // The expected boolean condition
 *    	new TheExceptionType("Parameter must be greater than 5")); //The error message
 *
 * 
* * @author Daniele Strollo (ISTI-CNR) */ public class Assertion implements Serializable { private static final long serialVersionUID = -2007903339251667541L; /** * Makes an assertion and if the expression evaluation fails, throws an * exception of type T. *
	 * Example:
	 * 	new Assertion<MyException>().validate(whatExpected, new MyException("guard failed"));
	 * 
* @param assertion the boolean expression to evaluate * @param exc the exception to throw if the condition does not hold * @throws T the exception extending {@link java.lang.Throwable} */ public final void validate(final boolean assertion, final T exc) throws T { // TOCHECK junit.framework.Assert.assertTrue(assertion); if (!assertion) { throw exc; } } }