Java tutorial
/* * This file is part of Dorado 7.x (http://dorado7.bsdn.org). * * Copyright (c) 2002-2012 BSTEK Corp. All rights reserved. * * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) * and BSDN commercial (http://www.bsdn.org/licenses) licenses. * * If you are unsure which license is appropriate for your use, please contact the sales department * at http://www.bstek.com/contact. */ package com.bstek.dorado.util; import java.util.Collection; import java.util.Map; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; /** * [] * @author Benny Bao (mailto:benny.bao@bstek.com) * @since Feb 14, 2007 */ public abstract class Assert { private Assert() { } /** * expressiontrue??message? * @param expression ? * @param message ?? */ public static void isTrue(boolean expression, String message) { if (!expression) { throw new IllegalArgumentException(message); } } /** * expressiontrue? * @param expression ? */ public static void isTrue(boolean expression) { isTrue(expression, "[Assertion failed] - this expression must be true"); } /** * null??message? * @param object * @param message ?? */ public static void isNull(Object object, String message) { if (object != null) { throw new IllegalArgumentException(message); } } /** * null? * @param object */ public static void isNull(Object object) { isNull(object, "[Assertion failed] - the object argument must be null"); } /** * ?null??message? * @param object * @param message ?? */ public static void notNull(Object object, String message) { if (object == null) { throw new IllegalArgumentException(message); } } /** * ?null? * @param object */ public static void notNull(Object object) { notNull(object, "[Assertion failed] - this argument is required; it must not null"); } /** * ???message?<br> * ??null0 * @param text * @param message ?? */ public static void notEmpty(String text, String message) { if (!StringUtils.isNotEmpty(text)) { throw new IllegalArgumentException(message); } } /** * ??<br> * ??null0 * @param text */ public static void notEmpty(String text) { notEmpty(text, "[Assertion failed] - this String argument must have length; it must not be null or empty"); } /** * ?????message? * @param textToSearch ? * @param substring ? * @param message ?? */ public static void doesNotContain(String textToSearch, String substring, String message) { if (StringUtils.isNotEmpty(textToSearch) && StringUtils.isNotEmpty(substring) && textToSearch.indexOf(substring) != -1) { throw new IllegalArgumentException(message); } } /** * ???? * @param textToSearch ? * @param substring ? */ public static void doesNotContain(String textToSearch, String substring) { doesNotContain(textToSearch, substring, "[Assertion failed] - this String argument must not contain the substring [" + substring + "]"); } /** * ???message?<br> * ??nulllength0 * @param array * @param message ?? */ public static void notEmpty(Object[] array, String message) { if (array == null || ArrayUtils.isEmpty(array)) { throw new IllegalArgumentException(message); } } /** * ??<br> * ??nulllength0 * @param array */ public static void notEmpty(Object[] array) { notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element"); } /** * ????message?<br> * ???nullsize0 * @param collection ? * @param message ?? */ public static void notEmpty(Collection<?> collection, String message) { if (collection == null || collection.isEmpty()) { throw new IllegalArgumentException(message); } } /** * ???<br> * ???nullsize0 * @param collection ? */ public static void notEmpty(Collection<?> collection) { notEmpty(collection, "[Assertion failed] - this collection must not be empty: it must contain at least 1 element"); } /** * Map???message?<br> * ?Map?nullsize0 * @param map Map * @param message ?? */ public static void notEmpty(Map<?, ?> map, String message) { if (map == null || map.isEmpty()) { throw new IllegalArgumentException(message); } } /** * Map??<br> * ?Map?nullsize0 * @param map Map */ public static void notEmpty(Map<?, ?> map) { notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry"); } /** * ?Class??message? * @param type Class * @param obj * @param message ?? */ public static void isInstanceOf(Class<?> type, Object obj, String message) { notNull(type, "Type to check against must not be null"); if (!type.isInstance(obj)) { throw new IllegalArgumentException(message + "Object of class [" + (obj != null ? obj.getClass().getName() : "null") + "] must be an instance of " + type); } } /** * ?Class??message? * @param type Class * @param obj */ public static void isInstanceOf(Class<?> type, Object obj) { isInstanceOf(type, obj, ""); } /** * Class?Class???message? * @param superType * @param subType ? * @param message ?? */ public static void isAssignable(Class<?> superType, Class<?> subType, String message) { notNull(superType, "Type to check against must not be null"); if (subType == null || !superType.isAssignableFrom(subType)) { throw new IllegalArgumentException(message + subType + " is not assignable to " + superType); } } /** * Class?Class???message? * @param superType * @param subType ? */ public static void isAssignable(Class<?> superType, Class<?> subType) { isAssignable(superType, subType, ""); } }