/**
* Created by lgq on 16-1-5.
*/
public class TestPreconditions {
/**
* 前置条件检查,当条件不满足时,就会抛出异常
*/
@Test
public void testPreconditions(){
String constantName = null;
//Preconditions.checkNotNull(constantName,"constantName 为 null");
String str = "abc";
Preconditions.checkNotNull(str,"str 为 null"); // 不会输出
int age = 23;
Integer number = 12;
//Preconditions.checkArgument(age < 20, "age 必须要< 20");
Preconditions.checkArgument(number < 20, "number 必须要< 20");
List<Integer> integerList = Lists.newArrayList(3,5,6,98,2,45);
for(int i=0; i<integerList.size(); i++) {
// 检查数组下标是否越界
int passIndex = Preconditions.checkElementIndex(i, integerList.size());
//System.out.println("下标:"+ passIndex + " ==> 满足要求");
}
// 检查下标值7 是否在集合integerList中
//Preconditions.checkElementIndex(7, integerList.size());
//Preconditions.checkPositionIndex(8, integerList.size());
Preconditions.checkPositionIndexes(2, 4, integerList.size());
// 判断是否为true,当为false时,会抛出IllegalStateException
Preconditions.checkState(4 < 3);
}
}
前置条件
猜你喜欢
转载自blog.csdn.net/weixin_42868638/article/details/85272840
今日推荐
周排行