1

I have found the example of solution in java:

things.stream()

.filter( filtersCollection.stream().reduce(Predicate::or).orElse(t->true) );

I tried to rewrite it using groovy (v2.5.4):

List<System> systems = ... //load from db
WellApplicationType appType = params['appType']
Contract contract = params['contract']
List<Predicate> filterCollection = []

if (appType != null)
    filterCollection.add({SystemRunlife systemRl -> systemRl.getApplicationType() == appType })
if (contract != null)
    filterCollection.add({SystemRunlife systemRl -> systemRl.getContract() == contract})

...

systems.stream()
       .map{system -> system.getSystemRunlife()}     
       .filter(filterCollection.stream().reduce{Predicate.&and}.orElse{t -> false})

It work only if I have 1 element in predicate's list. If I have more when one then I get an error:

No signature of method: Script1$_run_closure3$_closure5.doCall() is applicable for argument types: (Script1$_run_closure1, Script1$_run_closure2) values: [Script1$_run_closure1@7d9367fa, Script1$_run_closure2@70c1c430] Possible solutions: doCall(), doCall(java.lang.Object), findAll(), findAll()

Can someone tell me where is a problem?

UPD:

.filter{systemRl -> filterCollection.stream().allMatch{p -> p.test(systemRl)}}

- is not working too.

No signature of method: Script1.test() is applicable for argument types: (com.borets.wedb.entity.SystemRunlife) values: [com.borets.wedb.entity.SystemRunlife-5c0d826a-7f63-1ef5-7b74-bde707a1d814 [new]]

mihalis
  • 33
  • 6

1 Answers1

0
Predicate<SystemRunlife> filter = { systemRl -> true }
for (Predicate<SystemRunlife> systemRunlifePredicate : filterCollection) {
    filter = (filter & systemRunlifePredicate)
}

I had combined predicates that way. Maybe someone can propose more convineint way.

mihalis
  • 33
  • 6