Groovy-findAll闭包中的复杂过滤逻辑
这里棒极了。我需要使用复杂的逻辑过滤一组POGO,我正在努力理解如何正确使用Groovy-findAll闭包中的复杂过滤逻辑,groovy,closures,Groovy,Closures,这里棒极了。我需要使用复杂的逻辑过滤一组POGO,我正在努力理解如何正确使用findAll闭包 @Canonical class Widget { String name Boolean fizzbuzz } ... Set<Widget> filter(Integer isHidden, Set<Widget> inputWidgets) { inputWidgets.findAll { widget -> Foobar
findAll
闭包
@Canonical
class Widget {
String name
Boolean fizzbuzz
}
...
Set<Widget> filter(Integer isHidden, Set<Widget> inputWidgets) {
inputWidgets.findAll { widget ->
Foobar foobar = FoobarHelper.getFoobarByWidget(widget)
// Logic:
// If isHidden is 0 and no foorbar exists for the given widget,
// then we want to add this widget to the collection being
// returned...
if(isHidden.intValue() == 0 && !foobar) {
// Add this widget to collection being returned
???
}
// ...but if isHidden is 1 and a foobar does exist for the given
// widget, then we want to add this widget to the collection as
// well.
else if(isHidden.intValue() == 1 && foobar) {
// Add this widget to collection being returned
???
}
}
}
@Canonical
类小部件{
字符串名
布尔嘶嘶声
}
...
设置过滤器(整数isHidden,设置inputWidgets){
inputWidgets.findAll{widget->
Foobar Foobar=FoobarHelper.getFoobarByWidget(小部件)
//逻辑:
//如果isHidden为0且给定小部件不存在foorbar,
//然后我们想将这个小部件添加到正在创建的集合中
//返回。。。
如果(isHidden.intValue()==0&&!foobar){
//将此小部件添加到要返回的集合
???
}
//…但如果isHidden为1且给定的
//widget,然后我们想将这个widget添加到集合中
//嗯。
else if(ishiden.intValue()==1&&foobar){
//将此小部件添加到要返回的集合
???
}
}
}
因此,过滤逻辑也是:
- 如果
和ishiden==0
返回FoobarHelper.getFoobarByWidget(…)
,则将当前的null
小部件添加到要返回的集合中;及
- 如果
和ishiden==1
返回非FoobarHelper.getFoobarByWidget(…)
,则将当前的null
小部件添加到要返回的集合中
我认为我的逻辑实现是正确的,我只是在为如何将当前的
小部件添加到return集合(其中有???)而挣扎有什么想法吗?看起来应该是:
@Canonical
class Widget {
String name
Boolean fizzbuzz
}
...
Set<Widget> filter(Integer isHidden, Set<Widget> inputWidgets) {
inputWidgets.findAll { widget ->
Foobar foobar = FoobarHelper.getFoobarByWidget(widget)
(isHidden.intValue() == 0 && !foobar) || (isHidden.intValue() == 1 && foobar)
}
}
您正在检查这两个条件。满足其中任一条件的所有元素都将添加到返回的集合中
(isHidden.intValue() == 0 && !foobar) || (isHidden.intValue() == 1 && foobar)