Assertions in Java Programming Tool Class (Org.springframework.util.Assert) __ Programming

Source: Internet
Author: User

assertion Tool Class: Assert class, java.lang.Object--->org.springframework.util.assert

Let me give you an example,

When we write the methods of a class, we often need to check the validation of the method entry, and if the argument does not meet the requirement, the method rejects the post-processing by throwing an exception. as follows:

Public InputStream getData (String file) {

if (file = = NULL | | file.length () = = 0| | File.replaceall ("\\s", ""). Length () = 0) {

throw new IllegalArgumentException ("file into parameter is not a valid address");

}

...

}

Code similar to the above instrumentation method is very common, but it is not a good idea to use the method of manually writing detection logic in each of these methods. Read spring source, and you'll find that spring uses a Org.springframework.util.Assert generic class to accomplish this task.

Assert translation is Chinese as "assertion", and the reader who has used JUnit is familiar with this concept, it determines that one actual running value is the same as expected, otherwise throws an exception. Spring's detection of method parameters borrows this concept by providing an Assert class that has many methods of asserting the method's parameters by rule, and satisfies most of the requirements of the method's incoming parameter detection. These assertion methods throw illegalargumentexception when the parameter does not satisfy the requirement. Next, let's take a look at the common assertion methods in the Assert class:

Assertion method Description
Notnull (Object object) When an object is null to throw an exception, the Notnull (object, String message) method allows you to customize the exception information through the message. The opposite method of the Notnull () method assertion rule is the IsNull (object)/isnull (Object object, String message), which requires that the incoming parameter must be null;
IsTrue (Boolean expression)/IsTrue (Boolean expression, String message) An exception is thrown when expression is not true;
Notempty (Collection Collection)/Notempty (Collection Collection, String message) Throws an exception when the collection does not contain an element. Notempty (map map)/notempty (map map, string message) and Notempty (object[] array, string message)/Notempty (object[) array , String message) to judge the incoming parameters of the MAP and object[] types, respectively;
Haslength (string text)/Haslength (string text, String message) Throws an exception when text is null or length 0;
HasText (string text)/HasText (string text, String message) Text cannot be null and must contain at least one character that is not a space, otherwise an exception is thrown;
Isinstanceof (class Clazz, Object obj)/isinstanceof (class type, object obj, String message) If obj cannot be properly styled, the class specified for Clazz throws an exception;
Isassignable (class Supertype, class subtype)/Isassignable (class Supertype, class subtype, String message) Subtype must be able to match by type to supertype, otherwise an exception will be thrown;

You can use the Assert assertion class to simplify the code that the method parameters detect, such as InputStream getData (String file) after the Assert assertion class is applied, its code can be simplified to the following form:

Public InputStream getData (String file) {
Assert.hastext (filename, "file into parameter is not a valid address");
① using the Spring assertion class for method entry-parameter detection
...
}

It is obvious that the simplicity of the method has been improved by using Spring's Assert instead of the entry-parameter detection logic implemented by the encoding. Assert does not depend on the Spring container, you can boldly use this tool class in your own application.


Method Summary
static void Doesnotcontain (java.lang.String Texttosearch, java.lang.String substring)
Asserts that the given text does not contain the given substring.
static void Doesnotcontain (java.lang.String texttosearch, java.lang.String substring, java.lang.String message)
Asserts that the given text does not contain the given substring.
static void Haslength (java.lang.String text)
asserts that because a string is not empty, that is, it cannot be null, not an empty string.
static void Haslength (java.lang.String text, java.lang.String message)
asserts that because a string is not empty, that is, it cannot be null, not an empty string.
static void HasText (java.lang.String text)
Asserts that a string has valid textual content, that is, it cannot be null, and must contain at least one non-whitespace character.
static void HasText (java.lang.String text, java.lang.String message)
Asserts that a string has valid textual content, that is, it cannot be null, and must contain at least one non-whitespace character.
static void isassignable (Java.lang.Class supertype, Java.lang.Class subtype)
Assertion Supertype.isassignablefrom (subtype) is true.
static void isassignable (Java.lang.Class supertype, Java.lang.Class subtype, java.lang.String message)
Assertion Supertype.isassignablefrom (subtype) is true.
static void isinstanceof (Java.lang.Class clazz, Java.lang.Object obj)
Asserts that the object provided is an instance of the provided class.
static void isinstanceof (java.lang.Class type, java.lang.Object obj, java.lang.String message)
Asserts that the object provided is an instance of the provided class.
static void IsNull (Java.lang.Object Object)
Asserts that an object is null.
static void IsNull (Java.lang.Object Object, java.lang.String message)
Asserts that an object is null.
static void isTrue (Boolean expression)
Asserts a Boolean expression that throws IllegalArgumentException if the test result is false.
static void isTrue (Boolean expression, java.lang.String message)
Asserts a Boolean expression that throws IllegalArgumentException if the test result is false.
static void nonullelements (java.lang.object[] Array)
Asserts that an array has no null element.
static void nonullelements (java.lang.object[] Array, java.lang.String message)
Asserts that an array has no null element.
static void Notempty (Java.util.Collection Collection)
Asserts that a collection has elements, that is, it cannot be null, and must have at least one element.
static void Notempty (Java.util.Collection Collection, java.lang.String message)
Asserts that a collection has elements, that is, it cannot be null, and must have at least one element.
static void Notempty (Java.util.Map Map)
Asserts that a map works, that is, it cannot be null, and must have at least one entry.
static void Notempty (Java.util.Map Map, java.lang.String message)
Asserts that a map works, that is, it cannot be null, and must have at least one entry.
static void Notempty (java.lang.object[] Array)
Asserts an array element, that is, it cannot be null, and must have at least one element.
static void Notempty (java.lang.object[] Array, java.lang.String message)
Asserts an array element, that is, it cannot be null, and must have at least one element.
static void Notnull (Java.lang.Object Object)
Asserts that an object is not NULL.
static void Notnull (Java.lang.Object Object, java.lang.String message)
Asserts that an object is not NULL.
static void State (Boolean expression)
Asserts a Boolean expression that throws IllegalStateException if the test result is false.
static void State (Boolean expression, java.lang.String message)
Asserts a Boolean expression that throws IllegalStateException if the test result is false.
Methods of inheriting from class Java.lang.Object
Equals, GetClass, Hashcode, notify, Notifyall, toString, wait, wait, wait

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.