优雅的参数校验 Preconditions

Google Guava中提供了一个Preconditions类,用于校验入参的正确性

一、引入

Java maven项目引入

<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>29.0-jre</version>
</dependency>

二、使用

源码

1、检查参数(expression)是否合法,若为false,抛出IllegalArgumentException异常
  public static void checkArgument(boolean expression) {
    if (!expression) {
      throw new IllegalArgumentException();
    }
  }

例子:

import com.google.common.base.Preconditions;

/**
 * @description:
 * @author: MengW9
 * @create: 2020-05-22 16:28
 **/
public class PreconditinosDemo {
    public static void main(String[] args) {
        boolean demo=5<0;
        Preconditions.checkArgument(demo);
    }
}

输出:

Exception in thread "main" java.lang.IllegalArgumentException
	at com.google.common.base.Preconditions.checkArgument(Preconditions.java:128)
	at com.guava.preconditions.PreconditinosDemo.main(PreconditinosDemo.java:15)
2、检查入参,带异常信息
  public static void checkArgument(boolean expression, @Nullable Object errorMessage) {
    if (!expression) {
      throw new IllegalArgumentException(String.valueOf(errorMessage));
    }
  }

例子:

import com.google.common.base.Preconditions;

/**
 * @description:
 * @author: MengW9
 * @create: 2020-05-22 16:28
 **/
public class PreconditinosDemo {
    public static void main(String[] args) {
        boolean demo=5<0;
        Preconditions.checkArgument(demo,"输入不能为false");
    }
}

输出:

Exception in thread "main" java.lang.IllegalArgumentException: 输入不能为false
	at com.google.common.base.Preconditions.checkArgument(Preconditions.java:142)
	at com.guava.preconditions.PreconditinosDemo.main(PreconditinosDemo.java:18)
3、检查入参,errorMessageTemplate表示异常信息模板,errorMessageArgs将被替换为信息模板中的参数
  public static void checkArgument(
      boolean expression,
      @Nullable String errorMessageTemplate,
      @Nullable Object @Nullable ... errorMessageArgs) {
    if (!expression) {
      throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, errorMessageArgs));
    }
  }

例子:

/**
 * @description:
 * @author: MengW9
 * @create: 2020-05-22 16:28
 **/
public class PreconditinosDemo {
    public static void main(String[] args) {
        boolean demo=5<0;
        Preconditions.checkArgument(demo,"该参数为%s",demo);
    }
}

输出:

Exception in thread "main" java.lang.IllegalArgumentException: 该参数为false
	at com.google.common.base.Preconditions.checkArgument(Preconditions.java:217)
	at com.guava.preconditions.PreconditinosDemo.main(PreconditinosDemo.java:13)

扩展用法:

/**
 * @description:
 * @author: MengW9
 * @create: 2020-05-22 16:28
 **/
public class PreconditinosDemo {
    public static void main(String[] args) {
        boolean demo=5<0;
        Preconditions.checkArgument(demo,"%s_%s为%s","参数","不能",demo);
    }
}

输出:

Exception in thread "main" java.lang.IllegalArgumentException: 参数_不能为false
	at com.google.common.base.Preconditions.checkArgument(Preconditions.java:459)
	at com.guava.preconditions.PreconditinosDemo.main(PreconditinosDemo.java:13)

三、Preconditions里面的其他方法:

1 .checkArgument(boolean) :
 功能描述:检查boolean是否为真。 用作方法中检查参数
 失败时抛出的异常类型: IllegalArgumentException

2.checkNotNull(T):
 功能描述:检查value不为null, 直接返回value;
 失败时抛出的异常类型:NullPointerException

3.checkState(boolean):
 功能描述:检查对象的一些状态,不依赖方法参数。 例如, Iterator可以用来next是否在remove之前被调用。
 失败时抛出的异常类型:IllegalStateException

4.checkElementIndex(int index, int size):
 功能描述:检查index是否为在一个长度为size的list, string或array合法的范围。 index的范围区间是[0, size)(包含0不包含size)。无需直接传入list, string或array, 只需传入大小。返回index。
 失败时抛出的异常类型:IndexOutOfBoundsException

5.checkPositionIndex(int index, int size):
 功能描述:检查位置index是否为在一个长度为size的list, string或array合法的范围。 index的范围区间是[0, size)(包含0不包含size)。无需直接传入list, string或array, 只需传入大小。返回index。
 失败时抛出的异常类型:IndexOutOfBoundsException

6.checkPositionIndexes(int start, int end, int size):
 功能描述:检查[start, end)是一个长度为size的list, string或array合法的范围子集。伴随着错误信息。
 失败时抛出的异常类型:IndexOutOfBoundsException

原文地址:https://www.cnblogs.com/mengw/p/12938110.html