问题
I recently came across the use case to create a collection that does not accept null values. Guava Constraints seemed to fit the bill. Unfortunately it has been deprecated in Guava Release 15. The class is still available in Release 18, but the access modifier has been set to package private. There are no clues in the Javadoc what has replaced it.
So, being still interested in the functionality that Guava Constraints provided, what am I to do?
回答1:
From Constraints (Guava: Google Core Libraries for Java - latest)
Deprecated. Use Preconditions for basic checks. In place of constrained collections, we encourage you to check your preconditions explicitly instead of leaving that work to the collection implementation. For the specific case of rejecting null, consider the immutable collections. This class is scheduled for removal in Guava 16.0.
For migration I would suggest you take the Guava code, place it in its own module and include that module it in your project. Then you can work to replace the instances where you use the constrained collections as filtered collections with (Collections2|Lists|Iterables|ect).filter
and the other instances with explicit precondition checks.
If it appears you have a collection that may not include certain elements as a bonafide entity in your data model you could re-implement that specific collections interface (do delegate to the original collection) with the appropriate precondition checks on the methods that add elements.
来源:https://stackoverflow.com/questions/33806628/what-happened-to-guava-constraints