• Google Guava中的前置条件


    前置条件:让方法调用的前置条件判断更简单。
    Guava在Preconditions类中提供了若干前置条件判断的实用方法,我们建议[在Eclipse中静态导入这些方法]每个方法都有三个变种:

    • checkArgument()方法,用来检查传入的值是否为true。
    boolean flag=false;
    checkArgument(flag);
    

    运行结果:

    Exception in thread "main" java.lang.IllegalArgumentException
        at com.google.common.base.Preconditions.checkArgument(Preconditions.java:108)
        at guavaDemo.Test02.main(Test02.java:9)
    
    

    当然此方法有很多重载方法,这里我们介绍一个演示一下:

    int max=1,min=2;//我们期待max是大于min的    
    checkArgument(max>min,"max的值小于min的值");
    

    运行结果:

    Exception in thread "main" java.lang.IllegalArgumentException: max的值小于min的值
       at com.google.common.base.Preconditions.checkArgument(Preconditions.java:122)
       at guavaDemo.Test02.main(Test02.java:12)
    
    
    • checkNotNull(T)方法用来检查T的值是否为null。
    String str=null; 
    checkNotNull(str);
    

    运行结果:

    Exception in thread "main" java.lang.NullPointerException
        at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:770)
        at guavaDemo.Test02.main(Test02.java:15)
    
    • checkState(boolean) 检查对象的状态。
        String str=null;
        checkState(str.isEmpty());
    

    运行结果:

    Exception in thread "main" java.lang.NullPointerException
        at guavaDemo.Test02.main(Test02.java:17)
    
    • checkElementIndex(int index, int size),检查列表,字符串,或者数组的索引值是否合法。
    int[] arr=new int[5];
    checkElementIndex(5, arr.length);
    

    运行结果:

    Exception in thread "main" java.lang.IndexOutOfBoundsException: index (5) must be less than size (5)
        at com.google.common.base.Preconditions.checkElementIndex(Preconditions.java:1177)
        at com.google.common.base.Preconditions.checkElementIndex(Preconditions.java:1159)
        at guavaDemo.Test02.main(Test02.java:20)
    
    • checkPositionIndex(int index, int size),检查该位置是否有效,下面的例子中使用的仍然是上例中定义的数组。
      checkPositionIndex(5, arr.length); 5位置存在,运行正常。
      checkPositionIndex(6, arr.length); 6位置不存在,抛出异常。
    Exception in thread "main" java.lang.IndexOutOfBoundsException: index (6) must not be greater than size (5)
        at com.google.common.base.Preconditions.checkPositionIndex(Preconditions.java:1222)
        at com.google.common.base.Preconditions.checkPositionIndex(Preconditions.java:1204)
        at guavaDemo.Test02.main(Test02.java:22)
    
    • checkPositionIndexes(int start, int end, int size),检查某个范围是否有效。
      checkPositionIndexes(3, 6, arr.length);
      运行结果:
    Exception in thread "main" java.lang.IndexOutOfBoundsException: index (6) must not be greater than size (5)
        at com.google.common.base.Preconditions.checkPositionIndex(Preconditions.java:1222)
        at com.google.common.base.Preconditions.checkPositionIndex(Preconditions.java:1204)
        at guavaDemo.Test02.main(Test02.java:22)
    

    上面就是guava中为我们提供的一些最基本的前置条件检查方法。

    接下来我们看看guava给我提供的equals方法和hashcode方法,代码比较简单这里就不详细说明了。

            System.out.println(Objects.equal(null, 'a'));
            System.out.println(Objects.equal(null, null));
            System.out.println(Objects.equal('a', null));
            System.out.println(Objects.equal('a','a'));
            
            String str1="zhaotong1";
            System.out.println(Objects.hashCode(str1));
    

    执行结果:

    false
    true
    false
    true
    -1420540160
    
  • 相关阅读:
    OCS 2007 R2单机测试虚拟环境的搭建(windows server 2008 R2 x64)
    Linq的概念解析
    WCF Data Service与net.tcp承载分析
    codeplex上20个有意思的WPF程序
    各种SmartPhone上的跨平台开源框架的总结
    新开发Apple Store上软件的实施步骤
    VSTO应用程序中加入键盘钩子
    绝非偶然 苹果iPhone领先5年背后的迷思 【推荐】
    Windows上安装Mac OS虚拟机
    Visual Studio 远程调试的步骤
  • 原文地址:https://www.cnblogs.com/sandea/p/8920383.html
Copyright © 2020-2023  润新知