Preconditions: Make the precondition judgment of method calls easier.
Guava provides several utility methods for precondition judgment in the Preconditions class, and we recommend [statically importing these methods in Eclipse] three variants of each method:
- The checkArgument() method is used to check whether the incoming value is true.
boolean flag=false;
checkArgument(flag);
operation result:
Exception in thread "main" java.lang.IllegalArgumentException
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:108)
at guavaDemo.Test02.main(Test02.java:9)
Of course, this method has many overloaded methods, here we introduce a demonstration:
int max=1,min=2;//我们期待max是大于min的
checkArgument(max>min,"max的值小于min的值");
operation result:
Exception in thread "main" java.lang.IllegalArgumentException: max的值小于min的值
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:122)
at guavaDemo.Test02.main(Test02.java:12)
- The checkNotNull(T) method is used to check whether the value of T is null.
String str=null;
checkNotNull(str);
operation result:
Exception in thread "main" java.lang.NullPointerException
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:770)
at guavaDemo.Test02.main(Test02.java:15)
- checkState(boolean) Check the state of the object.
String str=null;
checkState(str.isEmpty());
operation result:
Exception in thread "main" java.lang.NullPointerException
at guavaDemo.Test02.main(Test02.java:17)
- checkElementIndex(int index, int size), check whether the index value of a list, string, or array is legal.
int[] arr=new int[5];
checkElementIndex(5, arr.length);
operation result:
Exception in thread "main" java.lang.IndexOutOfBoundsException: index (5) must be less than size (5)
at com.google.common.base.Preconditions.checkElementIndex(Preconditions.java:1177)
at com.google.common.base.Preconditions.checkElementIndex(Preconditions.java:1159)
at guavaDemo.Test02.main(Test02.java:20)
- checkPositionIndex(int index, int size), check whether the position is valid, the following example still uses the array defined in the above example.
checkPositionIndex(5, arr.length);
The 5 position exists and works fine.
checkPositionIndex(6, arr.length);
6 position does not exist, throws an exception.
Exception in thread "main" java.lang.IndexOutOfBoundsException: index (6) must not be greater than size (5)
at com.google.common.base.Preconditions.checkPositionIndex(Preconditions.java:1222)
at com.google.common.base.Preconditions.checkPositionIndex(Preconditions.java:1204)
at guavaDemo.Test02.main(Test02.java:22)
- checkPositionIndexes(int start, int end, int size), check if a range is valid.
checkPositionIndexes(3, 6, arr.length);
operation result:
Exception in thread "main" java.lang.IndexOutOfBoundsException: index (6) must not be greater than size (5)
at com.google.common.base.Preconditions.checkPositionIndex(Preconditions.java:1222)
at com.google.common.base.Preconditions.checkPositionIndex(Preconditions.java:1204)
at guavaDemo.Test02.main(Test02.java:22)
The above are some of the most basic precondition checking methods provided to us in guava.
Next, let's take a look at the equals method and hashcode method provided by guava. The code is relatively simple and will not be explained in detail here.
System.out.println(Objects.equal(null, 'a'));
System.out.println(Objects.equal(null, null));
System.out.println(Objects.equal('a', null));
System.out.println(Objects.equal('a','a'));
String str1="zhaotong1";
System.out.println(Objects.hashCode(str1));
Results of the:
false
true
false
true
-1420540160