All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.persistence.jpa.jpql.Assert Maven / Gradle / Ivy

There is a newer version: 5.0.0-B07
Show newest version
/*******************************************************************************
 * Copyright (c) 2011, 2013 Oracle and/or its affiliates. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
 * which accompanies this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     Oracle - initial API and implementation
 *
 ******************************************************************************/
package org.eclipse.persistence.jpa.jpql;

/**
 * An utility class that provides various checks and when the condition fails, then an {@link
 * AssertException} is thrown.
 *
 * @version 2.4
 * @since 2.4
 * @author Pascal Filion
 */
public final class Assert {

	/**
	 * Prevents instantiation.
	 */
	private Assert() {
		super();
	}

	/**
	 * Throws an {@link AssertException} immediately.
	 *
	 * @param message The message to display
	 */
	public static void fail(String message) {
		throw new AssertException(message);
	}

	/**
	 * Determines whether the given two objects are equal (identity). If the two objects are not
	 * identical, then an {@link AssertException} is thrown
	 *
	 * @param object1 The first object used to be compared to the second object
	 * @param object2 The second object used to be compared to the first object
	 * @param message The expression's message that will describe the reason why the check failed
	 */
	public static void isEqual(Object object1, Object object2, String message) {
		if (object1 != object2) {
			fail(message);
		}
	}

	/**
	 * Determines whether the given condition if false or true and if it is
	 * true then an {@link AssertException} is thrown.
	 *
	 * @param condition The condition to verify it is false
	 * @param message The expression's message that will describe the reason why the check failed
	 */
	public static void isFalse(boolean condition, String message) {
		if (condition) {
			fail(message);
		}
	}

	/**
	 * Determines whether the given object is not null. If the object is null,
	 * then an {@link NullPointerException} is thrown
	 *
	 * @param object The value to check to not be null
	 * @param message The expression's message that will describe the reason why the check failed
	 */
	public static void isNotNull(Object object, String message) {
		if (object == null) {
			throw new NullPointerException(message);
		}
	}

	/**
	 * Determines whether the given object is null. If the object is not null,
	 * then an {@link AssertException} is thrown
	 *
	 * @param object The value to check to be null
	 * @param message The expression's message that will describe the reason why the check failed
	 */
	public static void isNull(Object object, String message) {
		if (object != null) {
			fail(message);
		}
	}

	/**
	 * Determines whether the given condition if true or false and if it is
	 * false then an {@link AssertException} is thrown.
	 *
	 * @param condition The condition to verify it is true
	 * @param message The expression's message that will describe the reason why the check failed
	 */
	public static void isTrue(boolean condition, String message) {
		if (!condition) {
			fail(message);
		}
	}

	/**
	 * Determines whether the given object is one of the given choices using identity check.
	 *
	 * @param object The object to find in the list of choices
	 * @param message The expression's message that will describe the reason why the check failed
	 * @param choices The list of valid choices
	 */
	public static void isValid(Object object, String message, Object... choices) {
		for (Object choice : choices) {
			if (object == choice) {
				return;
			}
		}
		fail(message);
	}

	/**
	 * The exception thrown when the condition is not met.
	 */
	public static class AssertException extends RuntimeException {

		/**
		 * Creates a new AssertException.
		 *
		 * @param message The message describing the reason why this exception is thrown
		 */
		public AssertException(String message) {
			super(message);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy