番石榴约束发生了什么?
What happened to Guava Constraints?
我最近遇到了创建不接受空值的集合的用例。 Guava Constraints seemed to fit the bill. Unfortunately it has been deprecated in Guava Release 15。 class 在版本 18 中仍然可用,但访问修饰符已设置为 package private。 Javadoc 中没有任何线索取代了它。
那么,如果我仍然对 Guava Constraints 提供的功能感兴趣,我该怎么办?
来自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.
对于迁移,我建议您采用 Guava 代码,将其放入自己的模块中,并将该模块包含在您的项目中。然后,您可以使用 (Collections2|Lists|Iterables|ect).filter
替换使用约束集合作为过滤集合的实例,并使用显式前提条件检查替换其他实例。
如果您的集合看起来可能不包含某些元素作为数据模型中的真实实体,您可以重新实现该特定集合接口(委托给原始集合),并对添加元素的方法。
我最近遇到了创建不接受空值的集合的用例。 Guava Constraints seemed to fit the bill. Unfortunately it has been deprecated in Guava Release 15。 class 在版本 18 中仍然可用,但访问修饰符已设置为 package private。 Javadoc 中没有任何线索取代了它。
那么,如果我仍然对 Guava Constraints 提供的功能感兴趣,我该怎么办?
来自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.
对于迁移,我建议您采用 Guava 代码,将其放入自己的模块中,并将该模块包含在您的项目中。然后,您可以使用 (Collections2|Lists|Iterables|ect).filter
替换使用约束集合作为过滤集合的实例,并使用显式前提条件检查替换其他实例。
如果您的集合看起来可能不包含某些元素作为数据模型中的真实实体,您可以重新实现该特定集合接口(委托给原始集合),并对添加元素的方法。