写作初衷:由于日常开发经常需要用到很多工具类,经常根据需求自己写也比较麻烦 网上好了一些工具类例如commom.lang3或者hutool或者Jodd这样的开源工具,但是 发现他们之中虽然设计不错,但是如果我想要使用,就必须要引入依赖并且去维护依赖,有些 甚至会有存在版本编译不通过问题,故此想要写作一个每个类都可以作为独立工具类使用 每个使用者只需要复制该类,到任何项目当中都可以使用,所以需要尊从以下两个原则才能 做到.在此诚邀各位大佬参与.可以把各自用过的工具,整合成只依赖JDK,每个类都能够单独 使用的工具.每个人当遇到业务需求需要使用的时候,只需要到这里单独拷贝一个即可使用. 抛弃传统的需要引入依赖的烦恼.让大家一起来解决你所面临的业务问题吧!
遵从两大原则
package *;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
/**
* @program: simple_tools
* @description: 断言类, 类似于阿里的Assert
* @author: ChenWenLong
* @create: 2019-06-04 15:22
**/
public class ValidateUtil {
public ValidateUtil() {
super();
}
/**
* 功能描述:
* 〈校验结果是否为true〉
*
* @params : [expression, message, value]
* @return : void
* @author : cwl
* @date : 2019/6/4 15:27
*/
public static void isTrue(boolean expression, String message, Object value) {
if (expression == false) {
throw new IllegalArgumentException(message + value);
}
}
/**
* 功能描述:
* 〈校验结果是否为true〉
*
* @params : [expression, message, value]
* @return : void
* @author : cwl
* @date : 2019/6/4 15:27
*/
public static void isTrue(boolean expression, String message, long value) {
if (expression == false) {
throw new IllegalArgumentException(message + value);
}
}
/**
* 功能描述:
* 〈校验结果是否为true〉
*
* @params : [expression, message, value]
* @return : void
* @author : cwl
* @date : 2019/6/4 15:27
*/
public static void isTrue(boolean expression, String message, double value) {
if (expression == false) {
throw new IllegalArgumentException(message + value);
}
}
/**
* 功能描述:
* 〈校验结果是否为true〉
*
* @params : [expression, message, value]
* @return : void
* @author : cwl
* @date : 2019/6/4 15:27
*/
public static void isTrue(boolean expression, String message) {
if (expression == false) {
throw new IllegalArgumentException(message);
}
}
/**
* 功能描述:
* 〈校验结果是否为true〉
*
* @params : [expression, message, value]
* @return : void
* @author : cwl
* @date : 2019/6/4 15:27
*/
public static void isTrue(boolean expression) {
if (expression == false) {
throw new IllegalArgumentException("The validated expression is false");
}
}
/**
* 功能描述:
* 〈校验结果是否为true〉
*
* @params : [expression, message, value]
* @return : void
* @author : cwl
* @date : 2019/6/4 15:27
*/
public static void notNull(Object object, String message) {
if (object == null) {
throw new IllegalArgumentException(message);
}
}
/**
* 功能描述:
* 〈校验对象object是否为空〉
*
* @params : [object]
* @return : void
* @author : cwl
* @date : 2019/6/4 15:29
*/
public static void notNull(Object object) {
if (object == null) {
throw new IllegalArgumentException("The validated object is null");
}
}
/**
* 功能描述:
* 〈校验数组是否为空〉
*
* @params : [array, message]
* @return : void
* @author : cwl
* @date : 2019/6/4 15:30
*/
public static void notEmpty(Object[] array, String message) {
if (array == null || array.length == 0) {
throw new IllegalArgumentException(message);
}
}
/**
* 功能描述:
* 〈校验数组是否为空〉
*
* @params : [array, message]
* @return : void
* @author : cwl
* @date : 2019/6/4 15:30
*/
public static void notEmpty(Object[] array) {
if (array == null || array.length == 0) {
throw new IllegalArgumentException("The validated array is empty");
}
}
/**
* 功能描述:
* 〈校验集合是否为空〉
*
* @params : [collection, message]
* @return : void
* @author : cwl
* @date : 2019/6/4 15:32
*/
public static void notEmpty(Collection collection, String message) {
if (collection == null || collection.size() == 0) {
throw new IllegalArgumentException(message);
}
}
/**
* 功能描述:
* 〈校验集合是否为空〉
*
* @params : [collection]
* @return : void
* @author : cwl
* @date : 2019/6/4 15:32
*/
public static void notEmpty(Collection collection) {
if (collection == null || collection.size() == 0) {
throw new IllegalArgumentException("The validated collection is empty");
}
}
/**
* 功能描述:
* 〈校验map集合是否为空〉
*
* @params : [map, message]
* @return : void
* @author : cwl
* @date : 2019/6/4 15:33
*/
public static void notEmpty(Map map, String message) {
if (map == null || map.size() == 0) {
throw new IllegalArgumentException(message);
}
}
/**
* 功能描述:
* 〈校验map集合是否为空〉
*
* @params : [map]
* @return : void
* @author : cwl
* @date : 2019/6/4 15:33
*/
public static void notEmpty(Map map) {
if (map == null || map.size() == 0) {
throw new IllegalArgumentException("The validated map is empty");
}
}
/**
* 功能描述:
* 〈校验字符串是否为空〉
*
* @params : [string, message]
* @return : void
* @author : cwl
* @date : 2019/6/4 15:33
*/
public static void notEmpty(String string, String message) {
if (string == null || string.length() == 0) {
throw new IllegalArgumentException(message);
}
}
/**
* 功能描述:
* 〈校验字符串是否为空〉
*
* @params : [string]
* @return : void
* @author : cwl
* @date : 2019/6/4 15:34
*/
public static void notEmpty(String string) {
if (string == null || string.length() == 0) {
throw new IllegalArgumentException("The validated string is empty");
}
}
/**
* 功能描述:
* 〈校验数组array的每一个元素都不为空〉
*
* @params : [array, message]
* @return : void
* @author : cwl
* @date : 2019/6/4 15:35
*/
public static void noNullElements(Object[] array, String message) {
notNull(array);
for (int i = 0; i < array.length; i++) {
if (array[i] == null) {
throw new IllegalArgumentException(message);
}
}
}
/**
* 功能描述:
* 〈校验数组的每一个元素都不为空〉
*
* @params : [array]
* @return : void
* @author : cwl
* @date : 2019/6/4 15:36
*/
public static void noNullElements(Object[] array) {
notNull(array);
for (int i = 0; i < array.length; i++) {
if (array[i] == null) {
throw new IllegalArgumentException("The validated array contains null element at index: " + i);
}
}
}
/**
* 功能描述:
* 〈校验集合的每个元素都不为空〉
*
* @params : [collection, message]
* @return : void
* @author : cwl
* @date : 2019/6/4 15:36
*/
public static void noNullElements(Collection collection, String message) {
notNull(collection);
for (Iterator it = collection.iterator(); it.hasNext();) {
if (it.next() == null) {
throw new IllegalArgumentException(message);
}
}
}
/**
* 功能描述:
* 〈校验集合中的每个元素都不为空〉
*
* @params : [collection]
* @return : void
* @author : cwl
* @date : 2019/6/4 15:37
*/
public static void noNullElements(Collection collection) {
notNull(collection);
int i = 0;
for (Iterator it = collection.iterator(); it.hasNext(); i++) {
if (it.next() == null) {
throw new IllegalArgumentException("The validated collection contains null element at index: " + i);
}
}
}
/**
* 功能描述:
* 〈校验集合中的每个元素都是clazz类型〉
*
* @params : [collection, clazz, message]
* @return : void
* @author : cwl
* @date : 2019/6/4 15:38
*/
public static void allElementsOfType(Collection collection, Class clazz, String message) {
notNull(collection);
notNull(clazz);
for (Iterator it = collection.iterator(); it.hasNext(); ) {
if (clazz.isInstance(it.next()) == false) {
throw new IllegalArgumentException(message);
}
}
}
/**
* 功能描述:
* 〈校验集合中的每个元素都是clazz的类型〉
*
* @params : [collection, clazz]
* @return : void
* @author : cwl
* @date : 2019/6/4 15:38
*/
public static void allElementsOfType(Collection collection, Class clazz) {
notNull(collection);
notNull(clazz);
int i = 0;
for (Iterator it = collection.iterator(); it.hasNext(); i++) {
if (clazz.isInstance(it.next()) == false) {
throw new IllegalArgumentException("The validated collection contains an element not of type "
+ clazz.getName() + " at index: " + i);
}
}
}
}