guava Preconditions
Posted inspirationboom
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了guava Preconditions相关的知识,希望对你有一定的参考价值。
1.checkArgument
2.checkState
3.checkNotNull
4.checkElementIndex
5.checkPositionIndex
该类就这五种重载方法。
1. checkArgument 用作检查参数
1.1 单纯的判断表达式 public static void checkArgument(boolean expression) { if (!expression) { throw new IllegalArgumentException(); } } 1.2 规定表达式false 返回的错误信息 public static void checkArgument(boolean expression, @Nullable Object errorMessage) { if (!expression) { throw new IllegalArgumentException(String.valueOf(errorMessage)); } } 1.3 表达式, 错误模板, 错误信息 public static void checkArgument(boolean expression, @Nullable String errorMessageTemplate, @Nullable Object... errorMessageArgs) { if (!expression) { throw new IllegalArgumentException(Strings.lenientFormat(errorMessageTemplate, errorMessageArgs)); } }
2. checkState 用作检查状态
public static void checkState(boolean expression) { if (!expression) { throw new IllegalStateException(); } } public static void checkState(boolean expression, @Nullable Object errorMessage) { if (!expression) { throw new IllegalStateException(String.valueOf(errorMessage)); } } public static void checkState(boolean expression, @Nullable String errorMessageTemplate, @Nullable Object... errorMessageArgs) { if (!expression) { throw new IllegalStateException(Strings.lenientFormat(errorMessageTemplate, errorMessageArgs)); } }
3.checkNotNull 检查参数是否不为空
@CanIgnoreReturnValue public static <T> T checkNotNull(T reference) { if (reference == null) { throw new NullPointerException(); } else { return reference; } } @CanIgnoreReturnValue public static <T> T checkNotNull(T reference, @Nullable Object errorMessage) { if (reference == null) { throw new NullPointerException(String.valueOf(errorMessage)); } else { return reference; } } @CanIgnoreReturnValue public static <T> T checkNotNull(T reference, @Nullable String errorMessageTemplate, @Nullable Object... errorMessageArgs) { if (reference == null) { throw new NullPointerException(Strings.lenientFormat(errorMessageTemplate, errorMessageArgs)); } else { return reference; } }
以上是关于guava Preconditions的主要内容,如果未能解决你的问题,请参考以下文章
Java 的参数检查与断言 - Guava Preconditions
java java chromedriver报错NoSuchMethodError:com.google.common.base.Preconditions.checkState升级Guava就可以了