English 中文(简体)
Guava - Preconditions Class
  • 时间:2024-12-22

Guava - Preconditions Class


Previous Page Next Page  

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

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

@GwtCompatible
pubpc 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 calpng method.

2

static void checkArgument(boolean expression, Object errorMessage)

Ensures the truth of an expression involving one or more parameters to the calpng method.

3

static void checkArgument(boolean expression, String errorMessageTemplate, Object. errorMessageArgs)

Ensures the truth of an expression involving one or more parameters to the calpng method.

4

static int checkElementIndex(int index, int size)

Ensures that index specifies a vapd element in an array, pst or a string of size.

5

static int checkElementIndex(int index, int size, String desc)

Ensures that index specifies a vapd element in an array, pst, or a string of size.

6

static <T> T checkNotNull(T reference)

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

7

static <T> T checkNotNull(T reference, Object errorMessage)

Ensures that an object reference passed as a parameter to the calpng 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 calpng method is not null.

9

static int checkPositionIndex(int index, int size)

Ensures that index specifies a vapd position in an array, pst or a string of size.

10

static int checkPositionIndex(int index, int size, String desc)

Ensures that index specifies a vapd position in an array, pst or a string of size.

11

static void checkPositionIndexes(int start, int end, int size)

Ensures that start and end specify a vapd positions in an array, pst 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 calpng instance, but not involving any parameters to the calpng method.

13

static void checkState(boolean expression, Object errorMessage)

Ensures the truth of an expression involving the state of the calpng instance, but not involving any parameters to the calpng method.

14

static void checkState(boolean expression, String errorMessageTemplate, Object... errorMessageArgs)

Ensures the truth of an expression involving the state of the calpng instance, but not involving any parameters to the calpng method.

Methods Inherited

This class inherits methods from the following class −

    java.lang.Object

Example of Preconditions Class

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

GuavaTester.java

import com.google.common.base.Preconditions;

pubpc class GuavaTester {

   pubpc 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());
      }
   }

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

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

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

Verify the Result

Compile the class using javac compiler as follows −

C:Guava>javac GuavaTester.java

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: Invapd index. (6) must be less than size (5)
Advertisements