Guava - Preconditions

Guava

Preconditions

guava 提供了一系列前置条件检查工具,每一个方法都有三部分参数

  • mandatory: 需要检查的变量或者表达式
  • optional: 错误消息模版
  • optional:错误消息参数

样例:

//检查参数是否为true,不为true抛出IllegalArgumentException异常

checkArgument(i >= 0, "Argument was %s but expected nonnegative", i);
checkArgument(i < j, "Expected i < j, but %s > %s", i, j);
方法签名(不包括可选参数)描述错误时抛出的异常
checkArgument(boolean)检查布尔值是否为true,用于校验方法参数IllegalArgumentException
checkNotNull(T)检查参数T不为空,为空抛异常,不为空方法返回T值NullPointerException
checkState(boolean)检查对象状态IllegalStateException
checkElementIndex(int index, int size)检查index是否为list,string,或者数组的有效索引,有效索引为[0, size)。不需要直接传递list,string或者数组,仅仅传递它们的size即可IndexOutOfBoundsException
checkPositionIndex(int index, int size)检查index是否为list,string,或者数组的有效位置,有效位置为[0, size]。不需要直接传递list,string或者数组,仅仅传递它们的size即可IndexOutOfBoundsException
checkPositionIndexes(int start, int end, int size)检查 [start, end) 是否为list,string,或者数组的有效范围IndexOutOfBoundsException
checkArgument
public class Demo {
    public static void main(String[] args) {
        boolean b  = false;
        Preconditions.checkArgument(b);
    }
}

输出:
Exception in thread "main" java.lang.IllegalArgumentException
    at com.google.common.base.Preconditions.checkArgument(Preconditions.java:120)
    at com.study.guava.preconditions.Demo.main(Demo.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)   
public class Demo {
    public static void main(String[] args) {
        boolean b  = false;
        //使用占位符格式化错误消息
        Preconditions.checkArgument(b, "bool value is %s", b);
    }
}

输出:
Exception in thread "main" java.lang.IllegalArgumentException: bool value is false
    at com.google.common.base.Preconditions.checkArgument(Preconditions.java:203)
    at com.study.guava.preconditions.Demo.main(Demo.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
public class Demo {
    public static void main(String[] args) {
        int i = 3, j = 2;
        Preconditions.checkArgument(i < j, "Expected i < j, but %s > %s", i, j);
    }
}
输出:
Exception in thread "main" java.lang.IllegalArgumentException: Expected i < j, but 3 > 2
    at com.google.common.base.Preconditions.checkArgument(Preconditions.java:275)
    at com.study.guava.preconditions.Demo.main(Demo.java:12)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
checkNotNull
public class Demo {
    public static void main(String[] args) {
        Map<String,Object> map = null;
        Preconditions.checkNotNull(map);
    }
}
输出:
Exception in thread "main" java.lang.NullPointerException
    at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:782)
    at com.study.guava.preconditions.Demo.main(Demo.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
  • 如果不为空,直接返回该值
public class Demo {
    public static void main(String[] args) {
        String value = "hello world";
        System.out.println(Preconditions.checkNotNull(value));
    }
}
输出:
hello world
checkElementIndex & checkPositionIndex

区别在于是否包涵size

  • checkElementIndex: (0, size)

  • checkPositionIndex: (0, size]

public class Demo {
    public static void main(String[] args) {
        String value = "hello world";

        //校验通过
        Preconditions.checkPositionIndex(11, value.length());
        System.out.println("checkPositionIndex pass");

        Preconditions.checkElementIndex(11, value.length());
    }
}

输出:
checkPositionIndex pass
Exception in thread "main" java.lang.IndexOutOfBoundsException: index (11) must be less than size (11)
    at com.google.common.base.Preconditions.checkElementIndex(Preconditions.java:1189)
    at com.google.common.base.Preconditions.checkElementIndex(Preconditions.java:1171)
    at com.study.guava.preconditions.Demo.main(Demo.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值