Guava — Preconditions
简单的说Preconditions类提供了一堆静态方法,用来检验参数,引用是否为空,数组下表是否越界,然后抛出相应异常,简化代码。
还可格式化输入方式(%s)封装异常信息,看看源代码应该就很明了了,只是为你省了自己new异常再throw的部分。
null的检验
若非空直接返回引用,否则抛出NullPointerException
参数的检验
判断一个boolean表达式(参数满足该表达式),抛出的是IllegalArgumentException
元素下标的检验checkElementIndex与checkPositionIndex
前者index在(0,size)区间,后者index在(0, size]区间
对于size<0, 抛出IllegalArgumentException异常
对于下标不合法,抛出IndexOutOfBoundsException异常
state校验
判断一个boolean型state,抛出IllegalStateException,该异常说明
Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation.