Brief introduction
Today in the spring MVC source to see the following code, it feels quite interesting, here to record
The Assert assertion tool class, typically used for data validation checks, typically writes the following code in Java programming:
if (name = = NULL | | name.equls (")") {
throw new IllegalArgumentException ("parameter wrong!");
}
It is not good to use manual detection of legality in all methods, because it affects the readability of the code, which can be simplified by using the code above the Assert tool class:
Assert.hastext (name, "parameter wrong!");
This can greatly enhance the readability of your code, so let's look at the common assertion methods in the Assert class:
Notnull (Object object, "object is required")-objects are non-empty 3HF
IsTrue (Object object, "object must be True")-the object must be True
Notempty (Collection Collection, "Collection must not being empty")-collection non-empty
Haslength (String text, "text must be specified")-the character is not null and the character length is not 0
HasText (String text, "text must not is empty")-text is not null and must contain at least one character that is not a space
Isinstanceof (class Clazz, Object obj, "clazz must be of type [Clazz]")-obj must be able to be properly styled as a clazz-specified class
Usage of the spring Assert tool class