星空网 > 软件开发 > ASP.net

Guava:Preconditions检验参数

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

Guava:Preconditions检验参数
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());    }   }    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);  }}
Guava:Preconditions检验参数

  运行结果:

a person age:8,neme:peidaage 必须大于0neme为''neme为null

  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

  一个比较实用实例:

Guava:Preconditions检验参数
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检验参数
a person age:8,neme:peidaage 必须大于0neme为''neme为null intList size 不能大于9index 13 不在 list中, List size为:9 (13) must not be greater than size (9)end index (17) must not be greater than size (9)start index (13) must not be greater than size (9)index 为 16 不在 list中, List size为: 9 (16) must be less than size (9)
Guava:Preconditions检验参数

 

  Guava的preconditions有这样几个优点:

  在静态导入后, 方法很明确无歧义, checkNotNull可以清楚地告诉你它是干什么的, 它会抛出怎样的异常.
  checkNotNull在验证通过后直接返回, 可以这样方便地写代码: this.field = checkNotNull(field).
      简单而又强大的可变参数'printf'风格的自定义错误信息.




原标题:Guava:Preconditions检验参数

关键词:

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

外贸建站为什么成为外贸企业刚需?:https://www.kjdsnews.com/a/778527.html
如何隐藏Facebook手机端的已读标记:https://www.kjdsnews.com/a/778528.html
警惕!亚马逊严查UPC!大量listing面临下架.....:https://www.kjdsnews.com/a/778530.html
亚马逊配送费直接8折!英国到欧盟的欧洲配送网络3月重启:https://www.kjdsnews.com/a/778534.html
外贸网站该选哪一个?:https://www.kjdsnews.com/a/778536.html
外贸网站哪个好?:https://www.kjdsnews.com/a/778542.html
instagram竞品“TikTok Notes”在加拿大和澳大利亚上线:https://www.goluckyvip.com/news/220211.html
青岛黄岛景点门票一览 - 青岛黄岛旅游景点介绍免费:https://www.vstour.cn/a/410226.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流