package com.googlecode.cqengine.query;
import com.googlecode.cqengine.attribute.Attribute;
import com.googlecode.cqengine.attribute.SelfAttribute;
import com.googlecode.cqengine.attribute.SimpleAttribute;
import com.googlecode.cqengine.query.simple.SimpleQuery;
/**
* Adds literal queries all() and none() to CQEngine query syntax.
* <p/>
* To use these queries, add a static import for {@code LiteralQueryFactory.*}.
*
* @author ngallagher
*/
public class LiteralQueryFactory {
static class LiteralQuery<O> extends SimpleQuery<O, O> {
final boolean literalValue;
public LiteralQuery(Class<O> objectType, boolean literalValue) {
super(new SelfAttribute<O>(objectType, String.valueOf(literalValue)));
this.literalValue = literalValue;
}
@Override
protected boolean matchesSimpleAttribute(SimpleAttribute<O, O> attribute, O object) {
return literalValue;
}
@Override
protected boolean matchesNonSimpleAttribute(Attribute<O, O> attribute, O object) {
return literalValue;
}
@Override
protected int calcHashCode() {
return 1413898556; // chosen randomly
}
}
/**
* Private constructor, not used.
*/
LiteralQueryFactory() {
}
public static <O> Query<O> all(Class<O> objectType) {
return new LiteralQuery<O>(objectType, true);
}
public static <O> Query<O> none(Class<O> objectType) {
return new LiteralQuery<O>(objectType, false);
}
}