<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                #### [使用Guava前置條件](https://lingcoder.gitee.io/onjava8/#/book/16-Validating-Your-Code?id=%e4%bd%bf%e7%94%a8guava%e5%89%8d%e7%bd%ae%e6%9d%a1%e4%bb%b6) 在非嚴格的 DbC 中,前置條件是 DbC 中你不想刪除的那一部分,因為它可以檢查方法參數的有效性。那是你沒有辦法控制的事情,所以你需要對其檢查。因為 Java 在默認情況下禁用斷言,所以通常最好使用另外一個始終驗證方法參數的庫。 谷歌的 Guava 庫包含了一組很好的前置條件測試,這些測試不僅易于使用,而且命名也足夠好。在這里你可以看到它們的簡單用法。庫的設計人員建議靜態導入前置條件: ~~~ // validating/GuavaPreconditions.java // Demonstrating Guava Preconditions import java.util.function.*; import static com.google.common.base.Preconditions.*; public class GuavaPreconditions { static void test(Consumer<String> c, String s) { try { System.out.println(s); c.accept(s); System.out.println("Success"); } catch(Exception e) { String type = e.getClass().getSimpleName(); String msg = e.getMessage(); System.out.println(type + (msg == null ? "" : ": " + msg)); } } public static void main(String[] args) { test(s -> s = checkNotNull(s), "X"); test(s -> s = checkNotNull(s), null); test(s -> s = checkNotNull(s, "s was null"), null); test(s -> s = checkNotNull( s, "s was null, %s %s", "arg2", "arg3"), null); test(s -> checkArgument(s == "Fozzie"), "Fozzie"); test(s -> checkArgument(s == "Fozzie"), "X"); test(s -> checkArgument(s == "Fozzie"), null); test(s -> checkArgument( s == "Fozzie", "Bear Left!"), null); test(s -> checkArgument( s == "Fozzie", "Bear Left! %s Right!", "Frog"), null); test(s -> checkState(s.length() > 6), "Mortimer"); test(s -> checkState(s.length() > 6), "Mort"); test(s -> checkState(s.length() > 6), null); test(s -> checkElementIndex(6, s.length()), "Robert"); test(s -> checkElementIndex(6, s.length()), "Bob"); test(s -> checkElementIndex(6, s.length()), null); test(s -> checkPositionIndex(6, s.length()), "Robert"); test(s -> checkPositionIndex(6, s.length()), "Bob"); test(s -> checkPositionIndex(6, s.length()), null); test(s -> checkPositionIndexes( 0, 6, s.length()), "Hieronymus"); test(s -> checkPositionIndexes( 0, 10, s.length()), "Hieronymus"); test(s -> checkPositionIndexes( 0, 11, s.length()), "Hieronymus"); test(s -> checkPositionIndexes( -1, 6, s.length()), "Hieronymus"); test(s -> checkPositionIndexes( 7, 6, s.length()), "Hieronymus"); test(s -> checkPositionIndexes( 0, 6, s.length()), null); } } /* Output: X Success null NullPointerException null NullPointerException: s was null null NullPointerException: s was null, arg2 arg3 Fozzie Success X IllegalArgumentException null IllegalArgumentException null IllegalArgumentException: Bear Left! null IllegalArgumentException: Bear Left! Frog Right! Mortimer Success Mort IllegalStateException null NullPointerException Robert IndexOutOfBoundsException: index (6) must be less than size (6) Bob IndexOutOfBoundsException: index (6) must be less than size (3) null NullPointerException Robert Success Bob IndexOutOfBoundsException: index (6) must not be greater than size (3) null NullPointerException Hieronymus Success Hieronymus Success Hieronymus IndexOutOfBoundsException: end index (11) must not be greater than size (10) Hieronymus IndexOutOfBoundsException: start index (-1) must not be negative Hieronymus IndexOutOfBoundsException: end index (6) must not be less than start index (7) null NullPointerException */ ~~~ 雖然 Guava 的前置條件適用于所有類型,但我這里只演示**字符串(String)**類型。**test()**方法需要一個Consumer,因此我們可以傳遞一個 lambda 表達式作為第一個參數,傳遞給 lambda 表達式的字符串作為第二個參數。它顯示字符串,以便在查看輸出時確定方向,然后將字符串傳遞給 lambda 表達式。try 塊中的第二個**println**() 僅在 lambda 表達式成功時才顯示; 否則 catch 塊將捕獲并顯示錯誤信息。注意**test()**方法消除了多少重復的代碼。 每個前置條件都有三種不同的重載形式:一個什么都沒有,一個帶有簡單字符串消息,以及帶有一個字符串和替換值。為了提高效率,只允許**%s**(字符串類型)替換標記。在上面的例子中,演示了**checkNotNull()**和**checkArgument()**這兩種形式。但是它們對于所有前置條件方法都是相同的。注意**checkNotNull()**的返回參數, 所以你可以在表達式中內聯使用它。下面是如何在構造函數中使用它來防止包含**Null**值的對象構造: ~~~ / validating/NonNullConstruction.java import static com.google.common.base.Preconditions.*; public class NonNullConstruction { private Integer n; private String s; NonNullConstruction(Integer n, String s) { this.n = checkNotNull(n); this.s = checkNotNull(s); } public static void main(String[] args) { NonNullConstruction nnc = new NonNullConstruction(3, "Trousers"); } } ~~~ **checkArgument()**接受布爾表達式來對參數進行更具體的測試, 失敗時拋出**IllegalArgumentException**,**checkState()**用于測試對象的狀態(例如,不變性檢查),而不是檢查參數,并在失敗時拋出**IllegalStateException**。 最后三個方法在失敗時拋出**IndexOutOfBoundsException**。**checkElementIndex**() 確保其第一個參數是列表、字符串或數組的有效元素索引,其大小由第二個參數指定。**checkPositionIndex()**確保它的第一個參數在 0 到第二個參數(包括第二個參數)的范圍內。**checkPositionIndexes()**檢查**\[first\_arg, second\_arg\]**是一個列表的有效子列表,由第三個參數指定大小的字符串或數組。 所有的 Guava 前置條件對于基本類型和對象都有必要的重載。
                  <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>

                              哎呀哎呀视频在线观看