简体   繁体   English

Google Guava:谓词assertTrue

[英]Google Guava: Predicate assertTrue

I am looking for a method to assure all objects contained in a Collect ( List ) return a specified value from a predicate. 我正在寻找一种方法来确保CollectList )中包含的所有对象从谓词返回指定的值。

Pseudo Code: 伪代码:

Collections.assertTrue(List<Ballons>, isBluePredicate)

I do not see this being possible with the current API (maybe I am not looking in the proper place) 我认为当前的API无法做到这一点(也许我找不到合适的位置)

If this does exist would the function exit when it first encounters a false value? 如果确实存在,则函数在首次遇到错误值时会退出吗?

The Iterables.all(Iterable, Predicate) method is the normal way to do this; Iterables.all(Iterable,Predicate)方法是执行此操作的常规方法; the assertion would need to be your own. 断言必须是你自己的。

Guava itself does not have any asserts. 番石榴本身没有任何主张。 If I understand the question right you should be able to use Iterables.all 如果我理解正确的问题,那么您应该可以使用Iterables.all

assert Iterables.all(balloonsList, isBlue) : "some aren't blue";

Documentation does not clarify whether the predicate is evaluated for rest of elements if one results in false, but I suspect they won't be. 文档没有阐明如果一个结果为假,则是否对谓词是否对其余元素进行评估,但我怀疑不会。 You can trivially check the source. 您可以轻松检查源。

If you do need it to be evaluated for all elements then you should use filter and check the size of the result. 如果确实需要对所有元素进行评估,则应使用filter并检查结果的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM