1.PreCondition中提供的参数检验方式,让你的代码变得优雅
PreCondition中的主要方法如下:
-
Methods Modifier and Type Method and Description static void
checkArgument(boolean expression)
Ensures the truth of an expression involving one or more parameters to the calling method.static void
checkArgument(boolean expression, Object errorMessage)
Ensures the truth of an expression involving one or more parameters to the calling method.static void
checkArgument(boolean expression, String errorMessageTemplate, Object... errorMessageArgs)
Ensures the truth of an expression involving one or more parameters to the calling method.static int
checkElementIndex(int index, int size)
Ensures thatindex
specifies a valid element in an array, list or string of sizesize
.static int
checkElementIndex(int index, int size, String desc)
Ensures thatindex
specifies a valid element in an array, list or string of sizesize
.static <T> T
checkNotNull(T reference)
Ensures that an object reference passed as a parameter to the calling method is not null.static <T> T
checkNotNull(T reference, Object errorMessage)
Ensures that an object reference passed as a parameter to the calling method is not null.static <T> T
checkNotNull(T reference, String errorMessageTemplate, Object... errorMessageArgs)
Ensures that an object reference passed as a parameter to the calling method is not null.static int
checkPositionIndex(int index, int size)
Ensures thatindex
specifies a valid position in an array, list or string of sizesize
.static int
checkPositionIndex(int index, int size, String desc)
Ensures thatindex
specifies a valid position in an array, list or string of sizesize
.static void
checkPositionIndexes(int start, int end, int size)
Ensures thatstart
andend
specify a valid positions in an array, list or string of sizesize
, and are in order.static void
checkState(boolean expression)
Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.static void
checkState(boolean expression, Object errorMessage)
Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.static void
checkState(boolean expression, String errorMessageTemplate, Object... errorMessageArgs)
Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.
Demo1:
package org.sh.demo01;
import java.util.List;
import org.junit.Test;
import com.google.common.base.Preconditions;
public class GuavaDemo01 {
@Test
public void Preconditions(){
try {
getObjectPreConditions(8,"suhao");
} catch (Exception e) {
System.out.println(e.getMessage());
}
try {
getObjectPreConditions(8,null);
} catch (Exception e) {
System.out.println(e.getMessage());
}
try {
getObjectPreConditions(8,"");
} catch (Exception e) {
System.out.println(e.getMessage());
}
try {
getObjectPreConditions(-1,"suhao");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void getObjectPreConditions(int age ,String name) throws Exception{
Preconditions.checkNotNull(name,"name must not be null");
Preconditions.checkArgument(name.length()>0,"name 为\'\'");
Preconditions.checkArgument(age>0,"age must rl 0");
System.out.println("name="+name+",age="+age);
}
}
Guava的preconditions有这样几个优点:
在静态导入后, 方法很明确无歧义, checkNotNull可以清楚地告诉你它是干什么的, 它会抛出怎样的异常.
checkNotNull在验证通过后直接返回, 可以这样方便地写代码: this.field = checkNotNull(field).
简单而又强大的可变参数'printf'风格的自定义错误信息.