番石榴约束发生了什么? [英] What happened to Guava Constraints?

查看:85
本文介绍了番石榴约束发生了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了一个用例,以创建一个不接受空值的集合。似乎番石榴约束符合要求。不幸的是,它已在 Guava版本15 中弃用。该类在版本18中仍然可用,但是访问修饰符已设置为package private。 Javadoc中没有线索来代替它。

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.

因此,仍然对Guava Constraints提供的功能感兴趣,我该怎么办?

So, being still interested in the functionality that Guava Constraints provided, what am I to do?

推荐答案

来自约束(番石榴:Java的Google Core库-最新)


已弃用。使用前提条件进行基本检查。代替
受约束的集合,我们鼓励您明确检查先决条件
,而不是将工作留给集合
的实现。对于拒绝null的特定情况,请考虑
不可变的集合。该课程计划在Guava
16.0中删除。

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.

对于迁移,我建议您使用Guava代码,将其放置在其自己的模块中,并将其包含在您的项目中。然后,您可以使用(Collections2 | Lists | Iterables | ect).filter 替换使用约束集合作为筛选集合的实例,并使用显式前提条件检查替换其他实例。

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.

这篇关于番石榴约束发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆