Determining whether objects Guava elegant and null empty string

Determining whether objects Guava elegant and null empty string

I. Introduction

The code of the parameters is essential to determine which judgment is null or empty is written by most of the code that need to determine in each place are written over a lot of repetitive code will look very bloated, this part of the code optimization Guava has written for us, brought on can be used.

In addition, we realized the idea by reference Guava source code, write a method to achieve the same functionality.

-----------------------------------------------------------------------------------------------------

Note: you need to import using the Guava Guava dependency in the IDEA, you may be introduced by dependent or maven gradle.

gradle build.gradle import file dependency: compile group: 'com.google.guava', name: 'guava', version: '28 .0-jre '

Two, Guava elegant determine whether the object is null

  • Examples of the object is determined null
import java.util.Objects;

public class NotNull {
    public static void main(String[] args) {
        //定义一个 字符串为null的参数
        String strNull = null;

        //调用方法传入 null参数
        addNull(strNull);
   }

    private static void addNull(String str) {
        //Guava提供判断为null的方法,对参数进行判断。
        Objects.requireNonNull(str);
        System.out.println(str);
    }
}
  • operation result
Exception in thread "main" java.lang.NullPointerException
	at java.util.Objects.requireNonNull(Objects.java:203)
	at day09.test.NotNull.addNull(NotNull.java:27)
	at day09.test.NotNull.main(NotNull.java:21)
  • View Objects.requireNonNull source
public static <T> T requireNonNull(T obj) {
        //判断参数为null 则抛出异常信息
        if (obj == null)
            throw new NullPointerException();
        //参数不为null,则返回参数本身
        return obj;
    }

Third, the method of packaging a function implemented Objects.requireNonNull

Objects.requireNonNull source can be seen only Analyzing null, empty string is not determined. We wrote a way to achieve judgment null and empty string along the lines of source code implementation of their own.

  • Create a tool class, a write-implemented method of determining the empty string and the null class. 
import com.google.common.base.Strings;
public class StringUtils {
    public static String requiredNonNullOrEmpty(String str) {
        if (Strings.isNullOrEmpty(str)) {
            throw new IllegalStateException("should non null or empty");
        }
        return str;
    }
}
  •  Testing Tools
import java.util.Objects;

public class NotNull {
    public static void main(String[] args) {
        //定义一个 字符串为空和null的参数
        String strNull = null;
        String strEmpty = "";

        //调用方法分别传入 null和空字符串的参数
//        addNull(strNull);
        addEmpty(strEmpty);
    }

    private static void addNull(String str) {

        //使用我们写的工具类对参数进行判断
        String nullOrEmpty = StringUtils.requiredNonNullOrEmpty(str);
        System.out.println(nullOrEmpty);
    }

    private static void addEmpty(String str) {
        
        //使用我们写的工具类对参数进行判断
        String nullOrEmpty = StringUtils.requiredNonNullOrEmpty(str);
        System.out.println(nullOrEmpty);
    }
}
  • Test Results
    • When an incoming parameter is null or empty string, an exception is thrown description of the determination of the parameters play a role.
Exception in thread "main" java.lang.IllegalStateException: should non null or empty
	at day09.test.StringUtils.requiredNonNullOrEmpty(StringUtils.java:11)
	at day09.test.NotNull.addEmpty(NotNull.java:30)
	at day09.test.NotNull.main(NotNull.java:20)

 

Published 316 original articles · won praise 117 · views 420 000 +

Guess you like

Origin blog.csdn.net/m0_38039437/article/details/104847069