Google Guava中提供了一個Preconditions類,用於校驗入參的正確性
一、引入
Java maven項目引入
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
二、使用
源碼
1、檢查參數(expression)是否合法,若為false,拋出IllegalArgumentException異常
public static void checkArgument(boolean expression) {
if (!expression) {
throw new IllegalArgumentException();
}
}
例子:
import com.google.common.base.Preconditions;
/**
* @description:
* @author: MengW9
* @create: 2020-05-22 16:28
**/
public class PreconditinosDemo {
public static void main(String[] args) {
boolean demo=5<0;
Preconditions.checkArgument(demo);
}
}
輸出:
Exception in thread "main" java.lang.IllegalArgumentException
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:128)
at com.guava.preconditions.PreconditinosDemo.main(PreconditinosDemo.java:15)
2、檢查入參,帶異常信息
public static void checkArgument(boolean expression, @Nullable Object errorMessage) {
if (!expression) {
throw new IllegalArgumentException(String.valueOf(errorMessage));
}
}
例子:
import com.google.common.base.Preconditions;
/**
* @description:
* @author: MengW9
* @create: 2020-05-22 16:28
**/
public class PreconditinosDemo {
public static void main(String[] args) {
boolean demo=5<0;
Preconditions.checkArgument(demo,"輸入不能為false");
}
}
輸出:
Exception in thread "main" java.lang.IllegalArgumentException: 輸入不能為false
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:142)
at com.guava.preconditions.PreconditinosDemo.main(PreconditinosDemo.java:18)
3、檢查入參,errorMessageTemplate表示異常信息模板,errorMessageArgs將被替換為信息模板中的參數
public static void checkArgument(
boolean expression,
@Nullable String errorMessageTemplate,
@Nullable Object @Nullable ... errorMessageArgs) {
if (!expression) {
throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, errorMessageArgs));
}
}
例子:
/**
* @description:
* @author: MengW9
* @create: 2020-05-22 16:28
**/
public class PreconditinosDemo {
public static void main(String[] args) {
boolean demo=5<0;
Preconditions.checkArgument(demo,"該參數為%s",demo);
}
}
輸出:
Exception in thread "main" java.lang.IllegalArgumentException: 該參數為false
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:217)
at com.guava.preconditions.PreconditinosDemo.main(PreconditinosDemo.java:13)
擴展用法:
/**
* @description:
* @author: MengW9
* @create: 2020-05-22 16:28
**/
public class PreconditinosDemo {
public static void main(String[] args) {
boolean demo=5<0;
Preconditions.checkArgument(demo,"%s_%s為%s","參數","不能",demo);
}
}
輸出:
Exception in thread "main" java.lang.IllegalArgumentException: 參數_不能為false
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:459)
at com.guava.preconditions.PreconditinosDemo.main(PreconditinosDemo.java:13)
三、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