# 1.2-前置條件
[原文鏈接](http://code.google.com/p/guava-libraries/wiki/PreconditionsExplained)?[譯文鏈接](http://ifeve.com/?p=8798)?**譯者:** 沈義揚
前置條件:讓方法調用的前置條件判斷更簡單。
Guava在[Preconditions](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/base/Preconditions.html)類中提供了若干前置條件判斷的實用方法,我們強烈建議[在Eclipse中靜態導入這些方法](http://ifeve.com/eclipse-static-import/)。每個方法都有三個變種:
* 沒有額外參數:拋出的異常中沒有錯誤消息;
* 有一個Object對象作為額外參數:拋出的異常使用Object.toString() 作為錯誤消息;
* 有一個String對象作為額外參數,并且有一組任意數量的附加Object對象:這個變種處理異常消息的方式有點類似printf,但考慮GWT的兼容性和效率,只支持%s指示符。例如:
```
checkArgument(i >= 0, "Argument was %s but expected nonnegative", i);
checkArgument(i < j, "Expected i < j, but %s > %s", i, j);
```
| **方法聲明(不包括額外參數)** | **描述** | **檢查失敗時拋出的異常** |
|:--- |:--- |:--- |
| [`checkArgument(boolean)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/base/Preconditions.html#checkArgument%28boolean%29) | 檢查boolean是否為true,用來檢查傳遞給方法的參數。 | IllegalArgumentException |
| [`checkNotNull(T)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/base/Preconditions.html#checkNotNull%28T%29) | 檢查value是否為null,該方法直接返回value,因此可以內嵌使用checkNotNull`。` | NullPointerException |
| [`checkState(boolean)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/base/Preconditions.html#checkState%28boolean%29) | 用來檢查對象的某些狀態。 | IllegalStateException |
| [`checkElementIndex(int index, int size)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/base/Preconditions.html#checkElementIndex%28int, int%29) | 檢查index作為索引值對某個列表、字符串或數組是否有效。index>=0 && index<size * | IndexOutOfBoundsException |
| [`checkPositionIndex(int index, int size)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/base/Preconditions.html#checkPositionIndex%28int, int%29) | 檢查index作為位置值對某個列表、字符串或數組是否有效。index>=0 && index<=size * | IndexOutOfBoundsException |
| [`checkPositionIndexes(int start, int end, int size)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/base/Preconditions.html#checkPositionIndexes%28int, int, int%29) | 檢查[start, end]表示的位置范圍對某個列表、字符串或數組是否有效* | IndexOutOfBoundsException |
_譯者注:_
_*__索引值常用來查找列表、字符串或數組中的元素,如__List.get(int), String.charAt(int)_
_*位置值和位置范圍常用來截取列表、字符串或數組,如List.subList(int,int), String.substring(int)_
相比Apache Commons提供的類似方法,我們把Guava中的Preconditions作為首選。Piotr Jagielski在[他的博客](http://piotrjagielski.com/blog/google-guava-vs-apache-commons-for-argument-validation/)中簡要地列舉了一些理由:
* 在靜態導入后,Guava方法非常清楚明晰。checkNotNull清楚地描述做了什么,會拋出什么異常;
* checkNotNull直接返回檢查的參數,讓你可以在構造函數中保持字段的單行賦值風格:this.field = checkNotNull(field)
* 簡單的、參數可變的printf風格異常信息。鑒于這個優點,在JDK7已經引入[Objects.requireNonNull](http://docs.oracle.com/javase/7/docs/api/java/util/Objects.html#requireNonNull%28java.lang.Object,java.lang.String%29)的情況下,我們仍然建議你使用checkNotNull。
_在編碼時,如果某個值有多重的前置條件,我們建議你把它們放到不同的行,這樣有助于在調試時定位。此外,把每個前置條件放到不同的行,也可以幫助你編寫清晰和有用的錯誤消息。_
- Google Guava官方教程(中文版)
- 1-基本工具
- 1.1-使用和避免null
- 1.2-前置條件
- 1.3-常見Object方法
- 1.4-排序: Guava強大的”流暢風格比較器”
- 1.5-Throwables:簡化異常和錯誤的傳播與檢查
- 2-集合
- 2.1-不可變集合
- 2.2-新集合類型
- 2.3-強大的集合工具類:java.util.Collections中未包含的集合工具
- 2.4-集合擴展工具類
- 3-緩存
- 4-函數式編程
- 5-并發
- 5.1-google Guava包的ListenableFuture解析
- 5.2-Google-Guava Concurrent包里的Service框架淺析
- 6-字符串處理:分割,連接,填充
- 7-原生類型
- 9-I/O
- 10-散列
- 11-事件總線
- 12-數學運算
- 13-反射