Skip to content

Instantly share code, notes, and snippets.

@devinrsmith
Created August 5, 2016 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devinrsmith/946e658369f3b4f9432d5385ee0adf1e to your computer and use it in GitHub Desktop.
Save devinrsmith/946e658369f3b4f9432d5385ee0adf1e to your computer and use it in GitHub Desktop.
issue describing CQ compound class cast exception
import com.googlecode.cqengine.ConcurrentIndexedCollection;
import com.googlecode.cqengine.IndexedCollection;
import com.googlecode.cqengine.attribute.SimpleAttribute;
import com.googlecode.cqengine.index.compound.CompoundIndex;
import com.googlecode.cqengine.query.option.QueryOptions;
import com.googlecode.cqengine.resultset.ResultSet;
import static com.googlecode.cqengine.query.QueryFactory.and;
import static com.googlecode.cqengine.query.QueryFactory.equal;
import static com.googlecode.cqengine.query.QueryFactory.in;
import static com.googlecode.cqengine.query.QueryFactory.or;
/**
* Created by dsmith on 8/5/16.
*/
public class CQCompoundIssue {
public static final SimpleAttribute<MyObject, String> A = new SimpleAttribute<MyObject, String>() {
@Override
public String getValue(MyObject object, QueryOptions queryOptions) {
return object.a;
}
};
public static final SimpleAttribute<MyObject, String> B = new SimpleAttribute<MyObject, String>() {
@Override
public String getValue(MyObject object, QueryOptions queryOptions) {
return object.b;
}
};
public static class MyObject {
private final String a;
private final String b;
public MyObject(String a, String b) {
this.a = a;
this.b = b;
}
}
public static void main(String[] args) {
IndexedCollection<MyObject> index = new ConcurrentIndexedCollection<>();
index.addIndex(CompoundIndex.onAttributes(A, B));
// breaks with ClassCastException
final ResultSet<MyObject> retrieve = index.retrieve(and(equal(A, "1"), in(B, "2", "3")));
// works
//final ResultSet<MyObject> retrieve = index.retrieve(or(and(equal(A, "1"), equal(B, "2")), and(equal(A, "1"), equal(B, "3"))));
try {
retrieve.iterator().forEachRemaining(o -> {});
} finally {
retrieve.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment