Building Query in for loop

53 views
Skip to first unread message

Kometa

unread,
Jan 17, 2015, 5:47:14 AM1/17/15
to cqengine...@googlegroups.com
Hey,

I wanted to use query with a different number of parameters according to the input from the user.

That's what I have done:

 
Collection<Query> queries = new ArrayList<Query>();
for(int num : numbers){
    queries
.add(equal(MyNewResultSet.FEATURES, num));
}

Query query2 = and(queries);

but it does say that the argument in the "and" is incorrect. Is there a way to build a list of arguments for "and"?

Thanks

Niall Gallagher

unread,
Jan 17, 2015, 7:28:01 AM1/17/15
to cqengine...@googlegroups.com
Hi Kometa,

You need at least two child queries for and(). And in case you have no features to query, you need to decide then if the resulting query should match no objects, or all items.

You can write a helper method like this:

static Query<Car> queryFeatures(Collection<Query<Car>> features) {
switch (features.size()) {
case 0: return none(Car.class); // or all(Car.class) -- depends on your application needs
case 1: return features.iterator().next();
default: return and(features);
}
}
Hope that helps,
Niall

--
-- You received this message because you are subscribed to the "cqengine-discuss" group.
http://groups.google.com/group/cqengine-discuss
---
You received this message because you are subscribed to the Google Groups "cqengine-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cqengine-discu...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kometa

unread,
Jan 17, 2015, 7:36:57 AM1/17/15
to cqengine...@googlegroups.com
Hello Niall,

Yes you did help me! It was not the issue that there was only one argument. The problem was in the queries variable type.

I set it to :
Collection<Query> queries = new ArrayList<Query>();
Should be:
Collection<Query<MyNewResultSet>> queries = new ArrayList<Query<MyNewResultSet>>();

but thanks for a hint!

Kometa

Niall Gallagher

unread,
Jan 17, 2015, 7:38:15 AM1/17/15
to cqengine...@googlegroups.com
No problem, glad it's working :)
Reply all
Reply to author
Forward
0 new messages