validating - Using Guava Preconditions

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangbingfengf98/article/details/88933732

Java disables assertions by default, it's usually better to use a different library that's always validating method arguments.

Google's Guava library incorporates a nice set of precondition tests that are not only easy to use, but also descriptively well-named. Here we see simple useages of all of them. The library designers recommend we import the preconditions statically:

// validating/GuavaPreconditions.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Demonstrating Guava Preconditions

import static com.google.common.base.Preconditions.*;

import java.util.function.*;

public class GuavaPreconditions {
  static void test(Consumer<String> c, String s) {
    try {
      System.out.println(s);
      c.accept(s);
      System.out.println("Success");
    } catch (Exception e) {
      String type = e.getClass().getSimpleName();
      String msg = e.getMessage();
      System.out.println(type + (msg == null ? "" : ": " + msg));
    }
  }

  public static void main(String[] args) {
    test(s -> s = checkNotNull(s), "X");
    test(s -> s = checkNotNull(s), null);
    test(s -> s = checkNotNull(s, "s was null"), null);
    test(s -> s = checkNotNull(s, "s was null, %s %s", "arg2", "arg3"), null);
    System.out.println("--start checkArgument--");
    test(s -> checkArgument(s == "Fozzie"), "Fozzie");
    test(s -> checkArgument(s == "Fozzie"), "X");
    test(s -> checkArgument(s == "Fozzie"), null);
    test(s -> checkArgument(s == "Fozzie", "Bear Left!"), null);
    test(s -> checkArgument(s == "Fozzie", "Bear Left! %s Right!", "Frog"), null);
    System.out.println("--end checkArgument--");
    System.out.println("--start checkState--");
    test(s -> checkState(s.length() > 6), "Mortimer");
    test(s -> checkState(s.length() > 6), "Mort");
    test(s -> checkState(s.length() > 6), null);
    System.out.println("--end checkState--");
    System.out.println("--start checkElementIndex--");
    test(s -> checkElementIndex(6, s.length()), "Robert");
    test(s -> checkElementIndex(6, s.length()), "Bob");
    test(s -> checkElementIndex(6, s.length()), null);
    System.out.println("--end checkElementIndex--");
    System.out.println("--start checkPositionIndex--");
    test(s -> checkPositionIndex(6, s.length()), "Robert");
    test(s -> checkPositionIndex(6, s.length()), "Bob");
    test(s -> checkPositionIndex(6, s.length()), null);
    System.out.println("--end checkPositionIndex--");
    System.out.println("--start checkPositionIndexes--");
    test(s -> checkPositionIndexes(0, 6, s.length()), "Hieronymus");
    test(s -> checkPositionIndexes(0, 10, s.length()), "Hieronymus");
    test(s -> checkPositionIndexes(0, 11, s.length()), "Hieronymus");
    test(s -> checkPositionIndexes(-1, 6, s.length()), "Hieronymus");
    test(s -> checkPositionIndexes(7, 6, s.length()), "Hieronymus");
    test(s -> checkPositionIndexes(0, 6, s.length()), null);
  }
}
/* My Output
X
Success
null
NullPointerException
null
NullPointerException: s was null
null
NullPointerException: s was null, arg2 arg3
--start checkArgument--
Fozzie
Success
X
IllegalArgumentException
null
IllegalArgumentException

null
IllegalArgumentException: Bear Left!
null
IllegalArgumentException: Bear Left! Frog Right!
--end checkArgument--
--start checkState--
Mortimer
Success
Mort
IllegalStateException
null
NullPointerException
--end checkState--
--start checkElementIndex--
Robert
IndexOutOfBoundsException: index (6) must be less than size (6)
Bob
IndexOutOfBoundsException: index (6) must be less than size (3)
null
NullPointerException
--end checkElementIndex--
--start checkPositionIndex--
Robert
Success
Bob
IndexOutOfBoundsException: index (6) must not be greater than size (3)
null
NullPointerException
--end checkPositionIndex--
--start checkPositionIndexes--
Hieronymus
Success
Hieronymus
Success
Hieronymus
IndexOutOfBoundsException: end index (11) must not be greater than size (10)
Hieronymus
IndexOutOfBoundsException: start index (-1) must not be negative
Hieronymus
IndexOutOfBoundsException: end index (6) must not be less than start index (7)
null
NullPointerException
*/

Use checkNotNull() in a construtor to prevent object construction containing null values:

// validating/NonNullConstruction.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

import static com.google.common.base.Preconditions.*;

public class NonNullConstruction {
  private Integer n;
  private String s;

  NonNullConstruction(Integer n, String s) {
    this.n = checkNotNull(n);
    this.s = checkNotNull(s);
  }

  public static void main(String[] args) {
    NonNullConstruction nnc = new NonNullConstruction(3, "Trousers");
    // NonNullConstruction nnc = new NonNullConstruction(null, "Trousers");// error
  }
}

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/validating/GuavaPreconditions.java

3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/validating/NonNullConstruction.java

4. https://github.com/google/guava/blob/master/guava/src/com/google/common/base/Preconditions.java

猜你喜欢

转载自blog.csdn.net/wangbingfengf98/article/details/88933732