One: abstract class assert
An abstract class cannot instantiate an object, but it can be inherited, and the Assert class is a function class, so the method is static decorated
So the class name can be directly called. Method invocation.
Public abstract class Assert
Construction Method:
The meaning of the constructor method in an abstract class is not really large, because it cannot instantiate an object, so it is not called, but
If there is a class that inherits the Assert class, the constructor of the parent class is called in the subclass, and if the method is constructed in the parent class, the custom
, then the call that is to be shown in the subclass construction method, if it is a parameterless construct, does not have to be displayed in the sub-class.
Called, the non-parametric constructor method of the parent class is called by default.
Public Assert () { }
Overloaded with two IsTrue methods to determine if an expression is true, this overloaded way constructs code well, using
More flexible
public static void IsTrue (Boolean expression, String message) {
If the value of the expression is false, then an illegal parameter exception is thrown, and if true, the call to the End method, return if (!expression) throw new IllegalArgumentException ( message); else return; } Client programmer in use, you can directly tune this method, if you want to customize the exception information, you can call the above public static void IsTrue (Boolean expression) { isTrue ( Expression, "[assertion failed]-This expression must is true"); }
Overloads the two IsNull method to determine whether the object is null
public static void IsNull (Object object, String message) {
If object is not NULL, a throw parameter exception, NULL then ends the call if (Object! = null) throw new illegalargumentexception (message); else return; } Determine if Object is null, if you want to customize the exception message, you can call the method above public static void IsNull (Object object) { IsNull (object, "[ Assertion failed]-the object argument must be null "); }
Overload two Notnull methods to determine if object is non-null
public static void Notnull (Object object, String message) {
If object is null, then an illegal parameter exception is thrown, otherwise the end method is called, return if (object = = null) throw new illegalargumentexception (message); else return; } If you need to define your own throw exception message, you need to call the method above public static void Notnull (Object object) { Notnull (object, "[ Assertion failed]-this argument is required; It must not be null "); }
Determines whether the string text has a length, that is, whether it is empty (including null or "")
public static void Haslength (string text, String message) {
If empty, the illegal parameter exception is thrown, otherwise the call of the method ends directly, return if (! Stringutils.haslength (text)) throw new illegalargumentexception (message); else return; } You can generally use this method to directly determine whether text is empty, if you need to customize the exception message information, you can call the above method public static void Haslength (String text) { Haslength (text, "[Assertion failed]-this String argument must has length; It must not be null or empty "); }
Stringutils.haslength (text)
The Haslength method of the same packet-StringUtils class is introduced here.
public static Boolean haslength (Charsequence str) {
Determine if there is a length, that is, whether to determine if it is null or "" return str! = null && str.length () > 0; } The incoming parameter is a string, the overloaded method is called, the entry parameter is a sequence of characters, the character sequence is the interface, the string class, and the parent class of StringBuilder and StringBuffer is the static Boolean haslength ( String str) { return haslength ((charsequence) (str)); }
Overload two method HasText, judge whether the string has content, is to judge the string text is not null or "", or blank, for example: ""
public static void HasText (string text, String message) {
is empty (including whitespace " "), then an illegal exception is thrown, otherwise the end method calls the if (! Stringutils.hastext (text)) throw new illegalargumentexception (message); else return; } /If in order to customize the exception description, you can call the method above public static void HasText (String text) { hasText (text, "[Assertion failed]- This String argument must has text; It must not be null, empty, or blank "); }
Stringutils.hastext (text) introduces the HasText method under the StringUtils class.
public static Boolean hasText (Charsequence str) {
If null or "", returns false directly, without the content if (!haslength (str)) return false; int strLen = Str.length ();
Iterate through the string, get each character, if there is a character is not blank, it proves that there is content, return true, otherwise (so all is blank), then return FALSE for (int i = 0; i < StrLen; i++)
if (! Character.iswhitespace (Str.charat (i))) return true; return false; } Method of overloading the parameter string to the public static Boolean hasText (String str) { return HasText (((charsequence) (str)) ; }
To determine that the string Texttosearch does not contain a substring substring
public static void Doesnotcontain (string texttosearch, string substring, string message) {
If the string texttosearch and substring are not empty, and Texttosearch contains a substring substring, an exception is thrown, otherwise the return end method calls the if ( Stringutils.haslength (Texttosearch) && stringutils.haslength (substring) && texttosearch.contains ( SUBSTRING)) throw new illegalargumentexception (message); else return; } If you need to customize the exception information message, you can call the method above directly 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 ());
Here are
Texttosearch.contains (substring) Method: Method contains () from the String class
Public Boolean contains (Charsequence s) { return indexOf (s.tostring ()) >-1; }
Contains the call IndexOf returns the index, not including the return-1
Determines whether array arrays are not empty
public static void Notempty (Object array[], String message) {
If the array is empty, the argument is thrown with an illegal exception, otherwise the call to the end method is called if (Objectutils.isempty (array)) to the throw new IllegalArgumentException ( message); else return; } To customize the exception information, call the overloaded method above more flexible public static void Notempty (Object array[]) { notempty (array, "[assertion Failed]-this array must is empty:it must contain at least 1 element "); }
Call here
Objectutils.isempty (Array) method:
public static Boolean IsEmpty (Object array[]) {
If the array is null or the length is 0 , null return array = = NULL | | array.length = = 0; }
To determine the array of arrays without a null element
public static void Nonullelements (Object array[], String message) {
If the array is not NULL, then each element in the array is fetched and null is compared, and if there is a null then an exception is thrown if (array! = null) { Object arr$[] = array; There is a copy of the address, the address of the array object to arr, so that in the stack there are two addresses to array object 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 "); }
Non-null judgment for set collection
public static void Notempty (Collection Collection, String message) {
If the collection collection is empty, the argument is thrown with an illegal exception, otherwise the end method is called, and the direct 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 being empty:it must contain at least 1 element ");
Collectionutils.isempty (Collection)
public static Boolean IsEmpty (Collection Collection) {
Set is null or no element return collection = = NULL | | collection.isempty (); }
Determines whether the set map is non-null, or throws an exception if it is empty
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 being empty; It must contain at least one entry "); }
Collectionutils.isempty (map)
public static Boolean IsEmpty (map map) { return map = = NULL | | map.isempty (); }
Determine if the Obj object is an instance of the Clazz class
public static void Isinstanceof (Class clazz, Object obj) { isinstanceof (clazz, obj, ""); } public static void Isinstanceof (Class type, Object obj, String message) {
If obj is not an instance of type, an exception is thrown notnull (type, "type to check against must is not 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 method usage is similar to operator instanceof
Determine supertype equals subtype or subtype's parent class
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 is null");
If subtype is null or supertype is not the parent of subtype or the same, throw an exception 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; }
Overloading two methods state, similar to the previous IsTrue method
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 b e true "); }
Two: summary
Spring Framework is a good third-party framework, code design framework is relatively good, careful study of learning, for their own programming will be a lot of help, spring code
Many of the methods of creating overloads, which are more flexible to use, can be more business scenarios, custom exception information, written here today, and then continue to tidy up.
Source Analysis Six (Org.springframework.util of the package of the Assert class)