2016-08-29 07:27:53 -06:00
|
|
|
// validating/GuavaPreconditions.java
|
2016-08-15 17:10:52 -06:00
|
|
|
// (c)2016 MindView LLC: see Copyright.txt
|
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-08-15 17:10:52 -06:00
|
|
|
// Demonstrating Guava Preconditions
|
2016-08-31 12:30:03 -06:00
|
|
|
import java.util.function.*;
|
2016-08-15 17:10:52 -06:00
|
|
|
import static com.google.common.base.Preconditions.*;
|
|
|
|
|
|
|
|
public class GuavaPreconditions {
|
2016-08-31 12:30:03 -06:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2016-08-15 17:10:52 -06:00
|
|
|
public static void main(String[] args) {
|
2016-09-23 13:23:35 -06:00
|
|
|
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(
|
2016-08-31 12:30:03 -06:00
|
|
|
s, "s was null, %s %s", "arg2", "arg3"), null);
|
|
|
|
|
2016-09-23 13:23:35 -06:00
|
|
|
test(s -> checkArgument(s == "Fozzie"), "Fozzie");
|
|
|
|
test(s -> checkArgument(s == "Fozzie"), "X");
|
|
|
|
test(s -> checkArgument(s == "Fozzie"), null);
|
|
|
|
test(s -> checkArgument(
|
2016-08-31 12:30:03 -06:00
|
|
|
s == "Fozzie", "Bear Left!"), null);
|
2016-09-23 13:23:35 -06:00
|
|
|
test(s -> checkArgument(
|
2016-08-31 12:30:03 -06:00
|
|
|
s == "Fozzie", "Bear Left! %s Right!", "Frog"),
|
|
|
|
null);
|
|
|
|
|
2016-09-23 13:23:35 -06:00
|
|
|
test(s -> checkState(s.length() > 6), "Mortimer");
|
|
|
|
test(s -> checkState(s.length() > 6), "Mort");
|
|
|
|
test(s -> checkState(s.length() > 6), null);
|
2016-08-31 12:30:03 -06:00
|
|
|
|
2016-09-23 13:23:35 -06:00
|
|
|
test(s ->
|
2016-08-31 12:30:03 -06:00
|
|
|
checkElementIndex(6, s.length()), "Robert");
|
2016-09-23 13:23:35 -06:00
|
|
|
test(s ->
|
2016-08-31 12:30:03 -06:00
|
|
|
checkElementIndex(6, s.length()), "Bob");
|
2016-09-23 13:23:35 -06:00
|
|
|
test(s ->
|
2016-08-31 12:30:03 -06:00
|
|
|
checkElementIndex(6, s.length()), null);
|
|
|
|
|
2016-09-23 13:23:35 -06:00
|
|
|
test(s ->
|
2016-08-31 12:30:03 -06:00
|
|
|
checkPositionIndex(6, s.length()), "Robert");
|
2016-09-23 13:23:35 -06:00
|
|
|
test(s ->
|
2016-08-31 12:30:03 -06:00
|
|
|
checkPositionIndex(6, s.length()), "Bob");
|
2016-09-23 13:23:35 -06:00
|
|
|
test(s ->
|
2016-08-31 12:30:03 -06:00
|
|
|
checkPositionIndex(6, s.length()), null);
|
|
|
|
|
2016-09-23 13:23:35 -06:00
|
|
|
test(s -> checkPositionIndexes(
|
2016-08-31 12:30:03 -06:00
|
|
|
0, 6, s.length()), "Hieronymus");
|
2016-09-23 13:23:35 -06:00
|
|
|
test(s -> checkPositionIndexes(
|
2016-08-31 12:30:03 -06:00
|
|
|
0, 10, s.length()), "Hieronymus");
|
2016-09-23 13:23:35 -06:00
|
|
|
test(s -> checkPositionIndexes(
|
2016-08-31 12:30:03 -06:00
|
|
|
0, 11, s.length()), "Hieronymus");
|
2016-09-23 13:23:35 -06:00
|
|
|
test(s -> checkPositionIndexes(
|
2016-08-31 12:30:03 -06:00
|
|
|
-1, 6, s.length()), "Hieronymus");
|
2016-09-23 13:23:35 -06:00
|
|
|
test(s -> checkPositionIndexes(
|
2016-08-31 12:30:03 -06:00
|
|
|
7, 6, s.length()), "Hieronymus");
|
2016-09-23 13:23:35 -06:00
|
|
|
test(s -> checkPositionIndexes(
|
2016-08-31 12:30:03 -06:00
|
|
|
0, 6, s.length()), null);
|
2016-08-15 17:10:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Output:
|
2016-08-31 12:30:03 -06:00
|
|
|
X
|
|
|
|
Success
|
|
|
|
null
|
|
|
|
NullPointerException
|
|
|
|
null
|
|
|
|
NullPointerException: s was null
|
|
|
|
null
|
|
|
|
NullPointerException: s was null, arg2 arg3
|
|
|
|
Fozzie
|
|
|
|
Success
|
|
|
|
X
|
|
|
|
IllegalArgumentException
|
|
|
|
null
|
|
|
|
IllegalArgumentException
|
|
|
|
null
|
|
|
|
IllegalArgumentException: Bear Left!
|
|
|
|
null
|
|
|
|
IllegalArgumentException: Bear Left! Frog Right!
|
|
|
|
Mortimer
|
|
|
|
Success
|
|
|
|
Mort
|
|
|
|
IllegalStateException
|
|
|
|
null
|
|
|
|
NullPointerException
|
|
|
|
Robert
|
|
|
|
IndexOutOfBoundsException: index (6) must be less than size (6)
|
|
|
|
Bob
|
|
|
|
IndexOutOfBoundsException: index (6) must be less than size (3)
|
|
|
|
null
|
|
|
|
NullPointerException
|
|
|
|
Robert
|
|
|
|
Success
|
|
|
|
Bob
|
|
|
|
IndexOutOfBoundsException: index (6) must not be greater than size (3)
|
|
|
|
null
|
|
|
|
NullPointerException
|
|
|
|
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
|
2016-08-15 17:10:52 -06:00
|
|
|
*/
|