日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Guava学习笔记:Preconditions优雅的检验参数

發布時間:2025/7/14 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Guava学习笔记:Preconditions优雅的检验参数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  在日常開發中,我們經常會對方法的輸入參數做一些數據格式上的驗證,以便保證方法能夠按照正常流程執行下去。對于可預知的一些數據上的錯誤,我們一定要做事前檢測和判斷,來避免程序流程出錯,而不是完全通過錯誤處理來保證流程正確執行,畢竟錯誤處理是比較消耗資源的方式。在平常情況下我們對參數的判斷都需要自己來逐個寫方法判斷,代碼量不少并且復用性不高,如下所示:

import org.junit.Test;public class PreconditionsTest {@Testpublic void Preconditions() throws Exception { getPerson(8,"peida");getPerson(-9,"peida");getPerson(8,"");getPerson(8,null);}public static void getPerson(int age,String neme)throws Exception{if(age>0&&neme!=null&&neme.isEmpty()!=true){System.out.println("a person age:"+age+",neme:"+neme);}else{System.out.println("參數輸入有誤!");}} }

  說明:參數驗證,我們每次都要添加if語句來做判斷, 重復的工作會做好多次。getPerson方法只有2個參數,驗證規則也不是很復雜,如果參數過度,驗證規則復雜后,上面代碼的可讀性都會很差的,復用性就更談不上了。

  Guava類庫中提供了一個作參數檢查的工具類--Preconditions類,?該類可以大大地簡化我們代碼中對于參數的預判斷和處理,讓我們對方法輸入參數的驗證實現起來更加簡單優雅,下面我們看看Preconditions類的使用實例: 

import org.junit.Test; import com.google.common.base.Preconditions;public class PreconditionsTest {@Testpublic void Preconditions() throws Exception { getPersonByPrecondition(8,"peida");try {getPersonByPrecondition(-9,"peida");} catch (Exception e) {System.out.println(e.getMessage());}try {getPersonByPrecondition(8,"");} catch (Exception e) {System.out.println(e.getMessage());}try {getPersonByPrecondition(8,null);} catch (Exception e) {System.out.println(e.getMessage());} }public static void getPersonByPrecondition(int age,String neme)throws Exception{Preconditions.checkNotNull(neme, "neme為null");Preconditions.checkArgument(neme.length()>0, "neme為\'\'");Preconditions.checkArgument(age>0, "age 必須大于0");System.out.println("a person age:"+age+",neme:"+neme);} }

  運行結果:

a person age:8,neme:peida age 必須大于0 neme為'' neme為null

  Preconditions里面的方法:

  1 .checkArgument(boolean) :
  功能描述:檢查boolean是否為真。 用作方法中檢查參數
  失敗時拋出的異常類型: IllegalArgumentException

  2.checkNotNull(T): ?? ?
  功能描述:檢查value不為null, 直接返回value;
  失敗時拋出的異常類型:NullPointerException

  3.checkState(boolean):
  功能描述:檢查對象的一些狀態,不依賴方法參數。 例如, Iterator可以用來next是否在remove之前被調用。
  失敗時拋出的異常類型:IllegalStateException

  4.checkElementIndex(int index, int size):
  功能描述:檢查index是否為在一個長度為size的list, string或array合法的范圍。 index的范圍區間是[0, size)(包含0不包含size)。無需直接傳入list, string或array, 只需傳入大小。返回index。??
  失敗時拋出的異常類型:IndexOutOfBoundsException


  5.checkPositionIndex(int index, int size):
  功能描述:檢查位置index是否為在一個長度為size的list, string或array合法的范圍。 index的范圍區間是[0, size)(包含0不包含size)。無需直接傳入list, string或array, 只需傳入大小。返回index。
  失敗時拋出的異常類型:IndexOutOfBoundsException

  6.checkPositionIndexes(int start, int end, int size):
  功能描述:檢查[start, end)是一個長度為size的list, string或array合法的范圍子集。伴隨著錯誤信息。
  失敗時拋出的異常類型:IndexOutOfBoundsException

  一個比較實用實例:

import java.util.ArrayList; import java.util.List; import org.junit.Test; import com.google.common.base.Preconditions;public class PreconditionsTest {@Testpublic void Preconditions() throws Exception { getPersonByPrecondition(8,"peida");try {getPersonByPrecondition(-9,"peida");} catch (Exception e) {System.out.println(e.getMessage());}try {getPersonByPrecondition(8,"");} catch (Exception e) {System.out.println(e.getMessage());}try {getPersonByPrecondition(8,null);} catch (Exception e) {System.out.println(e.getMessage());}List<Integer> intList=new ArrayList<Integer> ();for(int i=0;i<10;i++){ try {checkState(intList,9);intList.add(i);} catch (Exception e) {System.out.println(e.getMessage());}}try {checkPositionIndex(intList,3); } catch (Exception e) {System.out.println(e.getMessage());}try {checkPositionIndex(intList,13); } catch (Exception e) {System.out.println(e.getMessage());}try {checkPositionIndexes(intList,3,7);} catch (Exception e) {System.out.println(e.getMessage());}try {checkPositionIndexes(intList,3,17);} catch (Exception e) {System.out.println(e.getMessage());}try {checkPositionIndexes(intList,13,17);} catch (Exception e) {System.out.println(e.getMessage());}try {checkElementIndex(intList,6);} catch (Exception e) {System.out.println(e.getMessage());}try {checkElementIndex(intList,16);} catch (Exception e) {System.out.println(e.getMessage());}}public static void getPersonByPrecondition(int age,String neme)throws Exception{Preconditions.checkNotNull(neme, "neme為null");Preconditions.checkArgument(neme.length()>0, "neme為\'\'");Preconditions.checkArgument(age>0, "age 必須大于0");System.out.println("a person age:"+age+",neme:"+neme);}public static void checkState(List<Integer> intList,int index)throws Exception{//表達式為true不拋異常Preconditions.checkState(intList.size()<index, " intList size 不能大于"+index);}public static void checkPositionIndex(List<Integer> intList,int index) throws Exception{Preconditions.checkPositionIndex(index, intList.size(), "index "+index+" 不在 list中, List size為:"+intList.size());}public static void checkPositionIndexes(List<Integer> intList,int start,int end) throws Exception{Preconditions.checkPositionIndexes(start, end, intList.size());}public static void checkElementIndex(List<Integer> intList,int index) throws Exception{Preconditions.checkElementIndex(index, intList.size(),"index 為 "+index+" 不在 list中, List size為: "+intList.size());} }

  輸出結果:

a person age:8,neme:peida age 必須大于0 neme為'' neme為nullintList size 不能大于9 index 13 不在 list中, List size為:9 (13) must not be greater than size (9) end index (17) must not be greater than size (9) start index (13) must not be greater than size (9) index 為 16 不在 list中, List size為: 9 (16) must be less than size (9)

?

  Guava的preconditions有這樣幾個優點:

  在靜態導入后, 方法很明確無歧義, checkNotNull可以清楚地告訴你它是干什么的, 它會拋出怎樣的異常.
  checkNotNull在驗證通過后直接返回, 可以這樣方便地寫代碼: this.field = checkNotNull(field).
????? 簡單而又強大的可變參數'printf'風格的自定義錯誤信息.

?

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的Guava学习笔记:Preconditions优雅的检验参数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。