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)