/* * @(#)Assert.java 2012-8-10 下午2:48:06 * * Copyright (c) 2011-2012 Makersoft.org all rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * */ package org.makersoft.shards.utils; /** * Assertion utility class that assists in validating arguments. * * @author Feng Kuok */ public class Assert { private Assert(){ } /** * Assert a boolean expression, throwing * <code>IllegalArgumentException</code> if the test result is * <code>false</code>. * * <pre class="code"> * Assert.isTrue(i > 0, "The value must be greater than zero"); * </pre> * * @param expression * a boolean expression * @param message * the exception message to use if the assertion fails * @throws IllegalArgumentException * if expression is <code>false</code> */ public static void isTrue(boolean expression, String message) { if (!expression) { throw new IllegalArgumentException(message); } } /** * Assert a boolean expression, throwing * <code>IllegalArgumentException</code> if the test result is * <code>false</code>. * * <pre class="code"> * Assert.isTrue(i > 0); * </pre> * * @param expression * a boolean expression * @throws IllegalArgumentException * if expression is <code>false</code> */ public static void isTrue(boolean expression) { isTrue(expression, "[Assertion failed] - this expression must be true"); } /** * Assert that an object is not <code>null</code> . * * <pre class="code"> * Assert.notNull(clazz, "The class must not be null"); * </pre> * * @param object * the object to check * @param message * the exception message to use if the assertion fails * @throws IllegalArgumentException * if the object is <code>null</code> */ public static void notNull(Object object, String message) { if (object == null) { throw new IllegalArgumentException(message); } } /** * Assert that an object is not <code>null</code> . * * <pre class="code"> * Assert.notNull(clazz); * </pre> * * @param object * the object to check * @throws IllegalArgumentException * if the object is <code>null</code> */ public static void notNull(Object object) { notNull(object, "[Assertion failed] - this argument is required; it must not be null"); } }