Usage of the spring Assert tool class

Source: Internet
Author: User
Tags object object

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.