轉載:https://my.oschina.net/realfighter/blog/349819
Preconditions是guava提供的用於進行代碼校驗的工具類,其中提供了許多重要的靜態校驗方法,用來簡化我們工作或開發中對代碼的校驗或預 處理,能夠確保代碼符合我們的期望,並且能夠在不符合校驗條件的地方,准確的為我們顯示出問題所在。
checkArgument(boolean expression):用來校驗表達式是否為真,一般用作方法中校驗參數
checkArgument(boolean expression, @Nullable Object errorMessage):校驗表達式是否為真,不為真時顯示指定的錯誤信息。
checkArgument(boolean expression, @Nullable String errorMessageTemplate, @Nullable Object... errorMessageArgs):校驗表達式是否為真,不為真時顯示錯誤信息,錯誤信息中允許使用占位符。
checkState(boolean expression):校驗表達式是否為真,一般用作校驗方法返回是否為真。
checkState(boolean expression, @Nullable Object errorMessage):當表達式為假的時候,顯示指定的錯誤信息。
checkState(boolean expression,@Nullable String errorMessageTemplate,@Nullable Object... errorMessageArgs):允許在錯誤信息中使用占位符。
checkNotNull(T reference):校驗對象是否為空。
checkNotNull(T reference, @Nullable Object errorMessage):對象為空時顯示指定的錯誤信息。
checkNotNull(T reference, @Nullable String errorMessageTemplate,@Nullable Object... errorMessageArgs):允許在錯誤信息中使用占位符。
checkElementIndex( int index, int size, @Nullable String desc):校驗元素的索引值是否有效,index大於等於0小於size,在無效時顯示錯誤描述信息。
checkElementIndex(int index, int size):錯誤描述信息為“index”
checkPositionIndex(int index, int size, @Nullable String desc):校驗元素的索引值是否有效,index大於等於0小於等於size,在無效時顯示錯誤描述信息。
checkPositionIndex(int index, int size):錯誤描述信息為“index”
checkPositionIndexes(int start, int end, int size):校驗大於等於start,小於end的list的長度是否為size。
public class PreconditionsTest {
// 打印輸出方法
private static void print(Object obj) {
System.out.println(String.valueOf(obj));
}
// 測試方法
private static boolean testMethod() {
return 1 > 2;
}
// 測試對象
private static Object testObject() {
return null;
}
public static void main(String[] args) {
// checkArgument
try {
// 校驗表達式是否正確,並使用占位符輸出錯誤信息
Preconditions.checkArgument(1 > 2, "%s is wrong", "1 > 2");
} catch (IllegalArgumentException e) {
print(e.getMessage()); // 1 > 2 is wrong
}
// checkState
try {
// 校驗表達式是否正確,並使用占位符輸出錯誤信息,使用方法作為表達式,一般用作校驗方法返回是否為真
Preconditions.checkState(testMethod(), "%s is wrong", "testMethod()");
} catch (IllegalStateException e) {
print(e.getMessage()); // testMethod() is wrong
}
// checkNotNull
try {
// 校驗對象是否為空,並使用占位符輸出錯誤信息
Preconditions.checkNotNull(testObject(), "%s is null", "testObject()");
} catch (NullPointerException e) {
print(e.getMessage()); // testObject() is null
}
// 初始化測試用list
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
list.add(i);
}
// checkElementIndex
try {
// 校驗元素索引是否有效 ,使用checkPositionIndex校驗
Preconditions.checkElementIndex(10, list.size());
// 在臨界值size處產生異常
} catch (IndexOutOfBoundsException e) {
print(e.getMessage()); // index (10) must be less than size (10)
}
// checkPositionIndex
try {
// 校驗元素索引是否有效,使用checkPositionIndex校驗
Preconditions.checkPositionIndex(10, list.size());
// 在臨界size處不產生異常
// print("checkPositionIndex does not throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
print(e.getMessage()); // checkPositionIndex does not throw
// IndexOutOfBoundsException
}
// checkPositionIndexes
try {
// 校驗是否是有效的索引區間
Preconditions.checkPositionIndexes(3, 11, list.size());
} catch (IndexOutOfBoundsException e) {
print(e.getMessage()); // end index (11) must not be greater than
// size (10)
}
}
}