<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 1.2-前置條件 # 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指示符。例如: ``` <pre class="calibre11">``` 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,%20int%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,%20int%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,%20int,%20int%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。 *在編碼時,如果某個值有多重的前置條件,我們建議你把它們放到不同的行,這樣有助于在調試時定位。此外,把每個前置條件放到不同的行,也可以幫助你編寫清晰和有用的錯誤消息。*
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看