你的位置:首页 > 软件开发 > ASP.net > Guava:Preconditions检验参数

Guava:Preconditions检验参数

发布时间:2015-11-09 17:00:09
Guava类库中提供了一个作参数检查的工具类--Preconditions类, 该类可以大大地简化我们代码中对于参数的预判断和处理,让我们对方法输入参数的验证实现起来更加简单优雅,下面我们看看Preconditions类的使用实例: import org.junit.Test ...

Guava类库中提供了一个作参数检查的工具类--Preconditions类, 该类可以大大地简化我们代码中对于参数的预判断和处理,让我们对方法输入参数的验证实现起来更加简单优雅,下面我们看看Preconditions类的使用实例: 

Guava:Preconditions检验参数  3.checkState(boolean):  4.checkElementIndex(int index, int size):
import java.util.ArrayList;import java.util.List;import org.junit.Test;import com.google.common.base.Preconditions;public 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());  }}
Guava:Preconditions检验参数

  输出结果:

Guava:Preconditions检验参数

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:Guava:Preconditions检验参数

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录