Guava 简明教程

Guava - Preconditions Class

Precondition 提供静态方法来检查方法或构造函数是否使用正确的参数调用。它检查前提条件。其方法在失败时抛出 IllegalArgumentException。

Preconditions provide static methods to check that a method or a constructor is invoked with proper parameter or not. It checks the pre-conditions. Its methods throw IllegalArgumentException on failure.

Class Declaration

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

Following is the declaration for com.google.common.base.Preconditions class −

@GwtCompatible
public final class Preconditions
   extends Object

Class Methods

Sr.No

Method & Description

1

static void checkArgument(boolean expression) Ensures the truth of an expression involving one or more parameters to the calling method.

2

static void checkArgument(boolean expression, Object errorMessage) Ensures the truth of an expression involving one or more parameters to the calling method.

3

static void checkArgument(boolean expression, String errorMessageTemplate, Object. errorMessageArgs) Ensures the truth of an expression involving one or more parameters to the calling method.

4

static int checkElementIndex(int index, int size) Ensures that index specifies a valid element in an array, list or a string of size.

5

static int checkElementIndex(int index, int size, String desc) Ensures that index specifies a valid element in an array, list, or a string of size.

6

static <T> T checkNotNull(T reference) Ensures that an object reference passed as a parameter to the calling method is not null.

7

static <T> T checkNotNull(T reference, Object errorMessage) Ensures that an object reference passed as a parameter to the calling method is not null.

8

static <T> T checkNotNull(T reference, String errorMessageTemplate, Object…​ errorMessageArgs) Ensures that an object reference passed as a parameter to the calling method is not null.

9

static int checkPositionIndex(int index, int size) Ensures that index specifies a valid position in an array, list or a string of size.

10

static int checkPositionIndex(int index, int size, String desc) Ensures that index specifies a valid position in an array, list or a string of size.

11

static void checkPositionIndexes(int start, int end, int size) Ensures that start and end specify a valid positions in an array, list or a string of size, and are in order.

12

static void checkState(boolean expression) Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.

13

static void checkState(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.

14

static void checkState(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.

Methods Inherited

此类从以下类继承方法 −

This class inherits methods from the following class −

  1. java.lang.Object

Example of Preconditions Class

使用任意你选择的编辑器在 C:/> Guava. 中创建以下 Java 程序

Create the following java program using any editor of your choice in say C:/> Guava.

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;
   }
}

Verify the Result

按照如下方式使用 javac 编译器编译类 −

Compile the class using javac compiler as follows −

C:\Guava>javac GuavaTester.java

现在运行 GuavaTester 以查看结果。

Now run the GuavaTester to see the result.

C:\Guava>java GuavaTester

查看结果。

See the result.

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)