guava学习笔记(1)--参数检验

来源:互联网 发布:路由器,mac在哪里 编辑:程序博客网 时间:2024/05/07 06:15

1.PreCondition中提供的参数检验方式,让你的代码变得优雅

PreCondition中的主要方法如下:

  • Methods Modifier and TypeMethod and Descriptionstatic voidcheckArgument(boolean expression)
    Ensures the truth of an expression involving one or more parameters to the calling method.
    static voidcheckArgument(boolean expression,Object errorMessage)
    Ensures the truth of an expression involving one or more parameters to the calling method.
    static voidcheckArgument(boolean expression,String errorMessageTemplate, Object... errorMessageArgs)
    Ensures the truth of an expression involving one or more parameters to the calling method.
    static intcheckElementIndex(int index, int size)
    Ensures that index specifies a valid element in an array, list or string of sizesize.
    static intcheckElementIndex(int index, int size,String desc)
    Ensures that index specifies a valid element in an array, list or string of sizesize.
    static <T> TcheckNotNull(T reference)
    Ensures that an object reference passed as a parameter to the calling method is not null.
    static <T> TcheckNotNull(T reference,Object errorMessage)
    Ensures that an object reference passed as a parameter to the calling method is not null.
    static <T> TcheckNotNull(T reference,String errorMessageTemplate, Object... errorMessageArgs)
    Ensures that an object reference passed as a parameter to the calling method is not null.
    static intcheckPositionIndex(int index, int size)
    Ensures that index specifies a valid position in an array, list or string of sizesize.
    static intcheckPositionIndex(int index, int size,String desc)
    Ensures that index specifies a valid position in an array, list or string of sizesize.
    static voidcheckPositionIndexes(int start, int end, int size)
    Ensures that start and end specify a validpositions in an array, list or string of size size, and are in order.
    static voidcheckState(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 voidcheckState(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 voidcheckState(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 {@Testpublic 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'风格的自定义错误信息.

0 0
原创粉丝点击