一:抽象類Assert
抽象類不能夠實例化對象,但是可以被繼承,Assert類是功能類,所以方法都是static修飾
所以可以直接 類名.方法 調用。
public abstract class Assert
構造方法:
抽象類中的構造方法的意義,其實不是很大,因為它不能實例化對象,所以不會調用,但是
如果有類繼承Assert類,那么就會在子類中調用父類的構造方法,如果父類中構造方法時自定義
的有參構造,那么在子類構造方法中就要顯示的調用,如果是無參構造,那么不用再子類中顯示的
調用,默認就會調用父類的無參構造方法。
public Assert() { }
重載了兩個isTrue方法,判斷表達式是否是true,這種重載的方式構造代碼很好,使用
比較靈活
public static void isTrue(boolean expression, String message) {
//如果表達式的值為false,那么會拋出非法參數異常,如果是true,則結束方法的調用,return if(!expression) throw new IllegalArgumentException(message); else return; } //客戶端程序員在使用的使用,可以直接調這個方法,如果想自定義異常信息,可以調用上面那個 public static void isTrue(boolean expression) { isTrue(expression, "[Assertion failed] - this expression must be true"); }
重載兩個isNull的方法,判斷對象object是否為null
public static void isNull(Object object, String message) {
//如果object不為null,則拋參數異常,為null則結束調用 if(object != null) throw new IllegalArgumentException(message); else return; } 判斷object為null,如果想要自定義異常message,則可以調用上面那個方法 public static void isNull(Object object) { isNull(object, "[Assertion failed] - the object argument must be null"); }
重載兩個notNull方法,判斷object是否非null
public static void notNull(Object object, String message) {
//如果object為null,則拋非法參數異常,否則結束方法調用,return if(object == null) throw new IllegalArgumentException(message); else return; } //如果需要自己定義拋出異常message,需要調用上面的方法 public static void notNull(Object object) { notNull(object, "[Assertion failed] - this argument is required; it must not be null"); }
判斷字符串text是否有長度,就是是否為空(包括null或者"")
public static void hasLength(String text, String message) {
//如果為空,則拋非法參數異常,否則直接結束方法的調用,return if(!StringUtils.hasLength(text)) throw new IllegalArgumentException(message); else return; } //一般可以使用該方法直接判斷text是否為空,如果需要自定義異常message信息,可以調用上面的方法 public static void hasLength(String text) { hasLength(text, "[Assertion failed] - this String argument must have length; it must not be null or empty"); }
StringUtils.hasLength(text)
這里引入了相同包向的StringUtils類的hasLength方法
public static boolean hasLength(CharSequence str) {
//判斷是否有長度,就是判斷是否為null或者"" return str != null && str.length() > 0; } //入參為String,調用重載方法,入參為字符序列,字符序列是接口,String類以及StringBuilder以及StringBuffer的父類 public static boolean hasLength(String str) { return hasLength(((CharSequence) (str))); }
重載兩個方法hasText,判斷字符串是否有內容,就是判斷字符串text不會null或者"",或者空白,例如:" "
public static void hasText(String text, String message) {
//為空(包括空白" "),則拋參數非法異常,否則結束方法調用 if(!StringUtils.hasText(text)) throw new IllegalArgumentException(message); else return; } /如果為了自定義異常描述,可以調用上面的方法 public static void hasText(String text) { hasText(text, "[Assertion failed] - this String argument must have text; it must not be null, empty, or blank"); }
StringUtils.hasText(text)這里引入了StringUtils類下的hasText方法
public static boolean hasText(CharSequence str) {
//如果為null或者"",則直接返回false,沒有內容 if(!hasLength(str)) return false; int strLen = str.length();
//遍歷字符串,得到每一個字符,如果有一個字符不是空白,就證明有內容,返回true,否則(所以都是空白),則返回false for(int i = 0; i < strLen; i++)
if(!Character.isWhitespace(str.charAt(i))) return true; return false; } //重載入參為字符串String的方法 public static boolean hasText(String str) { return hasText(((CharSequence) (str))); }
判斷字符串textToSearch中不包含substring子串
public static void doesNotContain(String textToSearch, String substring, String message) {
//如果字符串textToSearch以及subString都不為空,並且textToSearch包含子串substring,則拋出異常,否則return結束方法調用 if(StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) && textToSearch.contains(substring)) throw new IllegalArgumentException(message); else return; } //如果需要自定義異常信息message,則可以直接調用上面的方法 public static void doesNotContain(String textToSearch, String substring) { doesNotContain(textToSearch, substring, (new StringBuilder()).append("[Assertion failed] - this String argument must not contain the substring [").append(substring).append("]").toString()); }
這里有
textToSearch.contains(substring) 方法:來自於String類的方法contains()
public boolean contains(CharSequence s) { return indexOf(s.toString()) > -1; }
包含調用indexOf則返回索引,不包含則返回-1
判斷數組array是否非空
public static void notEmpty(Object array[], String message) {
//如果數組array為空,則拋參數非法異常,否則結束方法的調用 if(ObjectUtils.isEmpty(array)) throw new IllegalArgumentException(message); else return; } //自定義異常信息,則調用上面重載的方法更加靈活 public static void notEmpty(Object array[]) { notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element"); }
這里調用
ObjectUtils.isEmpty(array)方法:
public static boolean isEmpty(Object array[]) {
//如果數組array為null或者長度length為0則為空 return array == null || array.length == 0; }
判斷數組array沒有null元素
public static void noNullElements(Object array[], String message) {
//如果array非null,則取出array數組中的每一個元素和null進行比較,如果有一個為null則拋出異常 if(array != null) { Object arr$[] = array; //這里面有個地址復制的動作,將array數組對象的地址給了arr,這樣在棧中就有兩個地址指向array數組對象 int len$ = arr$.length; for(int i$ = 0; i$ < len$; i$++) { Object element = arr$[i$]; if(element == null) throw new IllegalArgumentException(message); } } } public static void noNullElements(Object array[]) { noNullElements(array, "[Assertion failed] - this array must not contain any null elements"); }
對於集合Collection的非空判斷
public static void notEmpty(Collection collection, String message) {
//如果collection集合為空,則拋出參數非法異常,否則結束方法調用,直接return if(CollectionUtils.isEmpty(collection)) throw new IllegalArgumentException(message); else return; } public static void notEmpty(Collection collection) { notEmpty(collection, "[Assertion failed] - this collection must not be empty: it must contain at least 1 element"); }
CollectionUtils.isEmpty(collection)
public static boolean isEmpty(Collection collection) {
//集合為null或沒有元素 return collection == null || collection.isEmpty(); }
判斷集合map非空,如果為空,則拋異常
public static void notEmpty(Map map, String message) { if(CollectionUtils.isEmpty(map)) throw new IllegalArgumentException(message); else return; } public static void notEmpty(Map map) { notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry"); }
CollectionUtils.isEmpty(map)
public static boolean isEmpty(Map map) { return map == null || map.isEmpty(); }
判斷obj對象是否是clazz類的實例
public static void isInstanceOf(Class clazz, Object obj) { isInstanceOf(clazz, obj, ""); } public static void isInstanceOf(Class type, Object obj, String message) {
//如果obj不是type的實例,則拋出異常 notNull(type, "Type to check against must not be null"); if(!type.isInstance(obj)) throw new IllegalArgumentException((new StringBuilder()).append(StringUtils.hasLength(message) ? (new StringBuilder()).append(message).append(" ").toString() : "").append("Object of class [").append(obj == null ? "null" : obj.getClass().getName()).append("] must be an instance of ").append(type).toString()); else return; }
isInstanceOf方法用法類似與運算符instanceof
判斷superType等於subType或者是subType的父類
public static void isAssignable(Class superType, Class subType) { isAssignable(superType, subType, ""); } public static void isAssignable(Class superType, Class subType, String message) { notNull(superType, "Type to check against must not be null");
//如果subType為null或者superType不是subType的父類或者相同,那么拋出異常 if(subType == null || !superType.isAssignableFrom(subType)) throw new IllegalArgumentException((new StringBuilder()).append(message).append(subType).append(" is not assignable to ").append(superType).toString()); else return; }
重載兩個方法state,用途類似與之前的isTrue方法
public static void state(boolean expression, String message) { if(!expression) throw new IllegalStateException(message); else return; } public static void state(boolean expression) { state(expression, "[Assertion failed] - this state invariant must be true"); }
二:總結
spring框架是優秀的第三方框架,代碼的設計架構比較良好,仔細研究學習,對自己的編程會有很多的幫助,spring的代碼中
很多創建重載的方法,這樣使用起來更加靈活,可以更加業務場景,自定義異常信息,今天就寫到這里,后續再繼續整理。