Please ref to the demo code:
git clone https://github.com/AlphaWang/guava-demo.git
Many of Guava’s utilities are designed to fail fast in the presence of null rather than allow nulls to be used.
com.alphawang.guava.ch1.optional.Test1_FailFast
1 2 3 4 5 6 7 8 | |
Additionally, Guava provides a number of facilities both to make using null easier, when you must, and to help you avoid using null.
Null object pattern
see http://www.tutorialspoint.com/design_pattern/null_object_pattern.htm
- Consider if there is a natural “null object” that can be used.
If it’s an enum, add a constant to mean whatever you’re expecting null to mean here.
example 1, java.math.RoundingMode has an UNNECESSARY value to indicate “do no rounding, and throw an exception if rounding would be necessary.”
example 2: use Unit.NONE instead of null.
1 2 3 4 5 6 7 8 9 | |
- Never return null.
For example, return a empty list instead of null.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Optional
com.alphawang.guava.ch1.optional.Test2_Optional
Optional<T> is a way of replacing a nullable T reference with a non-null value.
Besides the increase in readability that comes from giving null a name, the biggest advantage of Optional is its idiot-proof-ness. It forces you to actively think about the absent case if you want your program to compile at all, since you have to actively unwrap the Optional and address that case.
there are two main use cases of Optional.
- remind your client that the returned value can be null, can force he to check null case: api:
1 2 3 4 | |
client:
1 2 3 | |
- return a default value if the object is null:
1
| |
Enums
Enums#getIfPresent can help to get an Optional
1 2 3 4 5 6 7 8 9 10 11 | |
Objects
com.alphawang.guava.ch1.optional.Test3_Objects
MoreObjects.firstNonNull is an alternative of Optional.or, both can return a default value if the object is null.
1
| |
Preconditions
com.alphawang.guava.ch1.optional.Test4_Preconditions
Of course we can fail fast in the presence of null, just like guava code. Guava provides Preconditions.checkArgument and Preconditions.checkNotNull to do this.
1 2 3 4 5 6 7 | |
Reference
- Guava User Guide : https://code.google.com/p/guava-libraries/wiki/GuavaExplained
- Getting Started with Google Guava : http://pan.baidu.com/s/1o6LZJf0