Guava – Preconditions类

Guava – Preconditions 类

Preconditions提供了静态方法来检查一个方法或构造函数是否被适当的参数所调用。它检查预设条件。它的方法在失败时抛出IllegalArgumentException.

Preconditions 类声明

以下是com.google.common.base.Preconditions类的声明 −

@GwtCompatible
public final class Preconditions
   extends Object

Preconditions 类函数

序号 函数和描述
1 static void checkArgument(boolean expression)
确保涉及一个或多个参数的表达式对调用方法的真实性.
2 static void checkArgument(boolean expression, Object errorMessage)
确保涉及一个或多个参数的表达式对调用方法的真实性.
3 static void checkArgument(boolean expression, String errorMessageTemplate, Object. errorMessageArgs)
确保涉及一个或多个参数的表达式对调用方法的真实性.
4 static int checkElementIndex(int index, int size)
确保索引在一个数组、列表或大小的字符串中指定一个有效元素.
5 static int checkElementIndex(int index, int size, String desc)
确保索引在数组、列表或大小的字符串中指定一个有效元素.
6 static <T> T checkNotNull(T reference)
确保作为参数传递给调用方法的对象引用不是空的.
7 static <T> T checkNotNull(T reference, Object errorMessage)
确保作为参数传递给调用方法的对象引用不是空的.
8 static <T> T checkNotNull(T reference, String errorMessageTemplate, Object... errorMessageArgs)
确保作为参数传递给调用方法的对象引用不是空的.
9 static int checkPositionIndex(int index, int size)
确保索引在一个数组、列表或大小的字符串中指定一个有效的位置.
10 static int checkPositionIndex(int index, int size, String desc)
确保索引在一个数组、列表或大小的字符串中指定一个有效的位置.
11 static void checkPositionIndexes(int start, int end, int size)
确保start和end在数组、列表或大小字符串中指定有效的位置,并且是按顺序排列的.
12 static void checkState(boolean expression)
确保涉及调用实例状态的表达式的真实性,但不涉及调用方法的任何参数.
13 static void checkState(boolean expression, Object errorMessage)
确保涉及调用实例状态的表达式的真实性,但不涉及调用方法的任何参数.
14 static void checkState(boolean expression, String errorMessageTemplate, Object... errorMessageArgs)
确保涉及调用实例状态的表达式的真实性,但不涉及调用方法的任何参数.

Preconditions 类集成

该类继承了以下类的方法 −

  • java.lang.Object

Preconditions 类示例

C:/ >Guava中使用你选择的任何编辑器创建以下java程序

GuavaTester.java

import com.google.common.base.Preconditions;

public class GuavaTester {

   public static void main(String args[]) {
      GuavaTester guavaTester = new GuavaTester();

      try {
         System.out.println(guavaTester.sqrt(-3.0));
      } catch(IllegalArgumentException e) {
         System.out.println(e.getMessage());
      }

      try {
         System.out.println(guavaTester.sum(null,3));
      } catch(NullPointerException e) {
         System.out.println(e.getMessage());
      }

      try {
         System.out.println(guavaTester.getValue(6));
      } catch(IndexOutOfBoundsException e) {
         System.out.println(e.getMessage());
      }
   }

   public double sqrt(double input) throws IllegalArgumentException {
      Preconditions.checkArgument(input > 0.0,
         "Illegal Argument passed: Negative value %s.", input);
      return Math.sqrt(input);
   }

   public int sum(Integer a, Integer b) {
      a = Preconditions.checkNotNull(a, "Illegal Argument passed: First parameter is Null.");
      b = Preconditions.checkNotNull(b, "Illegal Argument passed: Second parameter is Null.");

      return a+b;
   }

   public int getValue(int input) {
      int[] data = {1,2,3,4,5};
      Preconditions.checkElementIndex(input,data.length, "Illegal Argument passed: Invalid index.");
      return 0;
   }
}

验证结果

使用javac编译器编译该类,如下所示 −

C:\Guava>javac GuavaTester.java

现在运行GuavaTester来看看结果.

C:\Guava>java GuavaTester

请看结果:

Illegal Argument passed: Negative value -3.0.
Illegal Argument passed: First parameter is Null.
Illegal Argument passed: Invalid index. (6) must be less than size (5)

赞(1)

评论 抢沙发

评论前必须登录!

 

Guava 教程
Guava 教程Guava - 概述Guava - 环境搭建Guava - Optional类Guava - Preconditions类Guava - Ordering类Guava - Objects类Guava - Range类Guava - Throwables类Guava - Collections 工具类Guava - Caching 工具类Guava - Multiset接口Guava - bimapGuava - MultiMap
Guava Ints类
Guava Ints类Guava Ints - concat() 函数Guava Ints - lastIndexOf() 函数Guava Ints - contains() 函数Guava Ints - asList() 函数Guava Ints - max() 函数Guava Ints - min() 函数Guava Ints - join() 函数Guava Ints - toArray() 函数Guava Ints - indexOf() 函数
Guava Chars类
Guava Chars类Guava - Chars.compare()方法与实例Guava - Chars.hashCode()方法与实例Guava - Chars.min()方法与实例Guava - Chars.max()方法与实例Guava - Chars.toArray()方法与实例Guava - Chars.concat()方法与实例Guava - Chars.contains()方法与实例Guava - Chars.lastIndexOf()方法与实例Guava - Chars.join()方法与实例Guava - Chars.indexOf()方法与实例Guava - Chars.asList()方法与实例
Guava LongMath类
Guava LongMath类Guava - LongMath.binomial方法与实例Guava - LongMath.log2方法与实例Guava - LongMath.pow方法与实例Guava - LongMath.isPowerOfTwo方法与实例Guava - LongMath.gcd方法与实例Guava - LongMath.mean方法与实例Guava - LongMath.sqrt方法与实例Guava - LongMath.mod方法与实例Guava - LongMath.log10方法与实例Guava - LongMath.checkedMultiply方法与实例Guava - LongMath.checkedAdd方法与实例Guava - LongMath.checkedPow方法与实例Guava - LongMath.divide方法与实例
Guava Longs类
Guava Longs类Guava - Longs.compare()方法与实例Guava - Longs.hashCode()方法与实例Guava - Longs.factorial方法与实例Guava - Longs.contains()方法与实例Guava - Longs.min()方法与实例Guava - Longs.max()方法与实例Guava - Longs.toArray()方法与实例Guava - Longs.lastIndexOf()方法与实例Guava - Longs.join()方法与实例Guava - Longs.asList()方法与实例Guava - Longs.checkedSubtract()方法与实例Guava - Longs.concat()方法与实例Guava - Longs.indexOf()方法与实例
Guava Booleans类
Guava Booleans类Guava - Booleans.asList()方法及示例Guava - Booleans.compare()方法及示例Guava - Booleans.concat()方法及示例Guava - Booleans.contains()方法及示例Guava - Booleans.countTrue()方法及示例Guava - Booleans.hashCode()方法及示例Guava - Booleans.indexOf()方法及示例Guava - Booleans.join()方法及示例Guava - Booleans.lastIndexOf()方法及示例Guava - Booleans.toArray()方法及示例
Guava Shorts类
Guava Shorts类Guava - Shorts.toArray()方法及实例Guava - Shorts.min()方法及实例Guava - Shorts.max()方法及实例Guava - Shorts.lastIndexOf()方法及实例Guava - Shorts.join()方法及实例Guava - Shorts.indexOf()方法及实例Guava - Shorts.hashCode()方法及实例Guava - Shorts.contains()方法及实例Guava - Shorts.concat()方法及实例Guava - Shorts.asList()方法及实例
Guava IntMath类
Guava IntMath类Guava - IntMath.checkedAdd()方法及实例Guava - IntMath.checkedMultiply()方法及实例Guava - IntMath.checkedPow()方法及实例Guava - IntMath.checkedSubtract()方法及实例Guava - IntMath.divide()方法及实例Guava - IntMath.log10()方法及实例