Take stock of those empty sentences in the Spring source code

Posted by djheru on Wed, 08 Dec 2021 05:56:23 +0100

Those empty sentences in Spring source code

Background & Introduction

I believe that many people use StringUtils of org.apache.commons.lang3 when judging space at ordinary times
When I read the Spring source code, I found a treasure. StringUtils comes with the Spring framework, and it can also be null judged

First, we can take a look at the source code of StringUtils under the commons package

  1. isEmpty() is used to judge whether the input string is null or the string length is 0, for example: null, "" (empty string)
  2. The function of isBlank() is to add the blank judgment on the basis of isEmpty(), such as "" (space character), "", "" (tab), etc
    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    public static boolean isNotEmpty(CharSequence cs) {
        return !isEmpty(cs);
    }

    public static boolean isBlank(CharSequence cs) {
        int strLen;
        if (cs != null && (strLen = cs.length()) != 0) {
            for(int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }
    
	public static boolean isNotBlank(CharSequence cs) {
	        return !isBlank(cs);
	    }

Then let's look at the StringUtils source code of the spring framework

  1. You can see that the tool class also has isEmpty() method, but the input parameter receives Object type, and there may be conversion exceptions. Therefore, it is not recommended to use it
  2. The hasLength() here is equivalent to that under commons! isEmpty() or isNotEmpty. Indicates that the String has length / content
  3. The hasText() here is equivalent to that under commons! isBlank() or isNotBlank()
	public static boolean isEmpty(@Nullable Object str) {
		return (str == null || "".equals(str));
	}
	
	public static boolean hasLength(@Nullable String str) {
		return (str != null && !str.isEmpty());
	}

	public static boolean hasText(@Nullable String str) {
		return (str != null && !str.isEmpty() && containsText(str));
	}

	private static boolean containsText(CharSequence str) {
		int strLen = str.length();
		for (int i = 0; i < strLen; i++) {
			if (!Character.isWhitespace(str.charAt(i))) {
				return true;
			}
		}
		return false;
	}

Conclusion & ex amp les of use

conclusion

If we only use StringUtils to empty, we can use spring's native tool class to empty without introducing org.apache.common jar

Mode of use

  • hasLength() is equivalent to that under commons! isEmpty() or isnotempty()
  • hasText() is equivalent to under commons! isBlank() or isNotBlank()
  • Us ing the above methods can show the source code you have seen invisibly and make the latecomers admire it

give an example:

Alas? wait
When I looked for an example of using StringUtils in the spring framework in the source code, I found an isEmpty(),
However, this blank checking is not for String type, but for list set, and then put the mouse on this method. The result is overjoyed
Under this package, there is a method to empty the list set. Its function is: if the list has no elements, it will return true


Check the underlying source code, by judging the number of elements in the list set, so as to achieve the effect of empty judgment

    public boolean isEmpty() {
        return size() == 0;
    }

Then we can compare it with the collection empty judgment method under import org.springframework.util.CollectionUtils, which I often use,
It can be seen that this tool class has one more judgment when it is null than the methods provided by the collection above, which is to prevent it from being set to null during initialization or when the data is queried. If the isEmpty() provided by the collection is called directly in the case of null, a null pointer exception will be reported. Therefore, when determining the null of the collection, It is recommended to use collectionutils.isempty (Collection) for null determination
Sure enough, the source code teaches us to be human~~~

    public static boolean isEmpty(@Nullable Collection<?> collection) {
        return collection == null || collection.isEmpty();
    }
   public boolean isEmpty() {
        return size() == 0;
    }

Topics: Spring