条件检查工具,每种条件检查都提供3个重载方法,以checkArgument方法为例:
- checkArgument(boolean expr)
- checkArgument(boolean expr, String errorMessage)
- checkArgument(boolean expr, String errorMessage, Object ... args) //errorMessage里可使用%s占位符(仅支持%s,非java的String.format用法),用args来替换
Signature (not including extra args) | Description | Exception thrown on failure |
checkArgument(boolean) | 校验状态是否合法 | IllegalArgumentException |
checkNotNull(T) | 检验值是否为空,若为空抛异常,否则返回T | NullPointerException |
checkState(boolean) | 校验状态是否合法 | IllegalStateException |
checkElementIndex(int index, int size) | 校验index是否小于size,index范围在0到size(不含),否则抛异常。 返回index | IndexOutOfBoundsException |
checkPositionIndex(int index, int size) | Checks that index is a valid position index into a list, string, or array with the specified size. A position index may range from 0 inclusive to size inclusive. You don't pass the list, string, or array directly; you just pass its size. Returns index. | IndexOutOfBoundsException |
checkPositionIndexes(int start, int end, int size) | Checks that [start, end) is a valid sub range of a list, string, or array with the specified size. Comes with its own error message. | IndexOutOfBoundsException |