Guava - Preconditions Class



Preconditions class 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
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 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 −

  • java.lang.Object

Example - Checking Arguments

GuavaTester.java

package com.tutorialspoint;

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

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

Output

Run the GuavaTester and verify the output −

Illegal Argument passed: Negative value -3.0.

Example - Checking Non-Null Values

GuavaTester.java

package com.tutorialspoint;

import com.google.common.base.Preconditions;

public class GuavaTester {

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

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

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

Output

Run the GuavaTester and verify the output −

Illegal Argument passed: First parameter is Null.

Example - Checking Element Index

GuavaTester.java

package com.tutorialspoint;

import com.google.common.base.Preconditions;

public class GuavaTester {

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

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

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

Output

Run the GuavaTester and verify the output −

Illegal Argument passed: Invalid index. (6) must be less than size (5)
Advertisements