What happened to Guava Constraints? [closed]

一笑奈何 提交于 2020-01-14 14:42:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!