coding……
但行好事 莫问前程

Guava PreConditions

Preconditions类提供了一系列静态方法,用来检测方法或者构造函数在调用时参数是否合法,如果不合法,则抛出异常。Preconditions有三种形式:

  1. 无报错信息,直接抛出异常
  2. 存在一个额外的Object参数作为报错信息,方法参数不合法时,额外的Object参数作为异常的message返回
  3. 存在一个额外的String模板参数作为报错信息,任意数量的Object填充String模板,方法参数不合法时,String模板填充后作为异常的message返回
S.N. 方法及说明
1 static void checkArgument(boolean expression)
static void checkArgument(boolean expression, @Nullable Object errorMessage)
static void checkArgument(boolean expression, @Nullable String errorMessageTemplate, @Nullable Object… errorMessageArgs)
如果布尔表达式结果为false,抛出IllegalArgumentException ,如果有message信息,message信息会随异常返回
2 static int checkElementIndex(int index, int size)
static int checkElementIndex(int index, int size, @Nullable String desc)
检查索引是是否是列表,字符串、指定大小的数组的有效索引,如果index不合法,抛出IndexOutOfBoundsException
3 static int checkPositionIndex(int index, int size)
static int checkPositionIndex(int index, int size, @Nullable String desc)
作用与checkElementIndex相同,检查索引是是否是列表,字符串、指定大小的数组的有效索引,如果index不合法,抛出IndexOutOfBoundsException
4 public static <T> T checkNotNull(T reference)
static <T> T checkNotNull(T reference, @Nullable Object errorMessage)
static <T> T checkNotNull(T reference, @Nullable String errorMessageTemplate, @Nullable Object… errorMessageArgs)
检查reference是否为null,为null报NullPointerException
5 static void checkState(boolean expression)
static void checkState(boolean expression, @Nullable Object errorMessage)
static void checkState(boolean expression, @Nullable String errorMessageTemplate, @Nullable Object… errorMessageArgs)
检查状态是否有效,如果布尔表达式为false,抛出IllegalStateException
  • checkArgument

public void pintAgeNoErrorMessage(int age) {
    Preconditions.checkArgument(age > 0);
    System.out.println("age is: " + age);
}

public void pintAgeWithErrorMessage(int age) {
    String message = "Age can't be zero or less than zero.";
    Preconditions.checkArgument(age > 0, message);
    System.out.println("age is: " + age);
}

public void pintAgeWithTemplateErrorMessage(int age) {
    String message = "Age should be positive number, you supplied %s.";
    Preconditions.checkArgument(age > 0, message, age);
    System.out.println("age is: " + age);
}

@Test
public void checkArgumentTest() {
    int age = -18;
    assertThatThrownBy(() -> pintAgeNoErrorMessage(age))
            .isInstanceOf(IllegalArgumentException.class)
            .hasMessage(null).hasNoCause();

    String message = "Age can't be zero or less than zero.";
    assertThatThrownBy(() -> pintAgeWithErrorMessage(age))
            .isInstanceOf(IllegalArgumentException.class)
            .hasMessage(message).hasNoCause();

    String message1 = "Age should be positive number, you supplied %s.";
    assertThatThrownBy(() -> pintAgeWithTemplateErrorMessage(age))
            .isInstanceOf(IllegalArgumentException.class)
            .hasMessage(message1, age).hasNoCause();
}
  • checkElementIndex

@Test
public void checkElementIndexTest() {
    int[] numbers = { 1, 2, 3, 4, 5 };
    String message = "Please check the bound of an array and retry";

    assertThatThrownBy(() ->
            Preconditions.checkElementIndex(6, numbers.length, message))
            .isInstanceOf(IndexOutOfBoundsException.class)
            .hasMessageStartingWith(message).hasNoCause();
}
  • checkPositionIndex

@Test
public void checkPositionIndexTest() {
    int[] numbers = { 1, 2, 3, 4, 5 };
    String message = "Please check the bound of an array and retry";

    assertThatThrownBy(
            () -> Preconditions.checkPositionIndex(6, numbers.length, message))
            .isInstanceOf(IndexOutOfBoundsException.class)
            .hasMessageStartingWith(message).hasNoCause();
}
  • checkNotNull

@Test
public void checkNotNullTest () {
    String nullObject = null;
    String message = "Please check the Object supplied, its null!";

    assertThatThrownBy(() -> Preconditions.checkNotNull(nullObject, message))
            .isInstanceOf(NullPointerException.class)
            .hasMessage(message).hasNoCause();
}
  • checkState

@Test
public void checkStateTest() {
    int[] validStates = { -1, 0, 1 };
    int givenState = 10;
    String message = "You have entered an invalid state";

    assertThatThrownBy(
            () -> Preconditions.checkState(
                    Arrays.binarySearch(validStates, givenState) > 0, message))
            .isInstanceOf(IllegalStateException.class)
            .hasMessageStartingWith(message).hasNoCause();
}

以上就是Guava PreConditions的几种常用方法,仔细观察会发现,其实checkNotNull、checkState都可以通过checkArgument实现。在实际开发中,像注解@NotNull也可以实现类似的功能,且更加方便。

测试代码:码云 – 卓立 – Guava Preconditions

  1. Google Guava Docs

赞(0) 打赏
Zhuoli's Blog » Guava PreConditions
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址