📜  Guava-前提条件课程

📅  最后修改于: 2020-11-16 06:51:15             🧑  作者: Mango


前提条件提供静态方法来检查是否使用适当的参数调用了方法或构造函数。它检查前提条件。其方法在失败时引发IllegalArgumentException。

类声明

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

@GwtCompatible
public final class Preconditions
   extends Object

类方法

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 intcheckElementIndex(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 checkNotNull(T reference)

Ensures that an object reference passed as a parameter to the calling method is not null.

7

static 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 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 intcheckPositionIndex(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.

继承的方法

此类从以下类继承方法-

  • java.lang.Object

前提条件类示例

使用您选择的任何编辑器在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)