Google Guava入門教程
阿新 • • 發佈:2019-02-02
class PreconditionsTest {
@Test
public 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());
}
}