Compare instance of predicates, not exact class

We want the same behavior for any instance of the boolean
predicates, not just for the exact boolean classes.

Change-Id: I2d1168c7a69e03db4b8dda3ddf60275a603c3e27
This commit is contained in:
Nasser Grainawi
2013-06-27 19:25:36 -06:00
committed by Dave Borowitz
parent ec4418b3e8
commit e12cf645de
3 changed files with 14 additions and 20 deletions

View File

@@ -54,11 +54,11 @@ public class QueryBuilder {
public static Query toQuery(Predicate<ChangeData> p)
throws QueryParseException {
if (p.getClass() == AndPredicate.class) {
if (p instanceof AndPredicate) {
return booleanQuery(p, MUST);
} else if (p.getClass() == OrPredicate.class) {
} else if (p instanceof OrPredicate) {
return booleanQuery(p, SHOULD);
} else if (p.getClass() == NotPredicate.class) {
} else if (p instanceof NotPredicate) {
if (p.getChild(0) instanceof TimestampRangePredicate) {
return notTimestampQuery(
(TimestampRangePredicate<ChangeData>) p.getChild(0));

View File

@@ -159,17 +159,17 @@ public abstract class QueryRewriter<T> {
}
protected Predicate<T> replaceGenericNodes(final Predicate<T> in) {
if (in.getClass() == NotPredicate.class) {
if (in instanceof NotPredicate) {
return not(replaceGenericNodes(in.getChild(0)));
} else if (in.getClass() == AndPredicate.class) {
} else if (in instanceof AndPredicate) {
List<Predicate<T>> n = new ArrayList<Predicate<T>>(in.getChildCount());
for (Predicate<T> c : in.getChildren()) {
n.add(replaceGenericNodes(c));
}
return and(n);
} else if (in.getClass() == OrPredicate.class) {
} else if (in instanceof OrPredicate) {
List<Predicate<T>> n = new ArrayList<Predicate<T>>(in.getChildCount());
for (Predicate<T> c : in.getChildren()) {
n.add(replaceGenericNodes(c));

View File

@@ -65,15 +65,15 @@ public class IndexRewriteImpl implements IndexRewrite {
public static EnumSet<Change.Status> getPossibleStatus(Predicate<ChangeData> in) {
if (in instanceof ChangeStatusPredicate) {
return EnumSet.of(((ChangeStatusPredicate) in).getStatus());
} else if (in.getClass() == NotPredicate.class) {
} else if (in instanceof NotPredicate) {
return EnumSet.complementOf(getPossibleStatus(in.getChild(0)));
} else if (in.getClass() == OrPredicate.class) {
} else if (in instanceof OrPredicate) {
EnumSet<Change.Status> s = EnumSet.noneOf(Change.Status.class);
for (int i = 0; i < in.getChildCount(); i++) {
s.addAll(getPossibleStatus(in.getChild(i)));
}
return s;
} else if (in.getClass() == AndPredicate.class) {
} else if (in instanceof AndPredicate) {
EnumSet<Change.Status> s = EnumSet.allOf(Change.Status.class);
for (int i = 0; i < in.getChildCount(); i++) {
s.retainAll(getPossibleStatus(in.getChild(i)));
@@ -120,7 +120,7 @@ public class IndexRewriteImpl implements IndexRewrite {
if (in instanceof IndexPredicate) {
return in;
}
if (!isRewritePossible(in)) {
if (!isBoolean(in)) {
return null;
}
int n = in.getChildCount();
@@ -194,9 +194,7 @@ public class IndexRewriteImpl implements IndexRewrite {
if (p instanceof IndexPredicate) {
return !((IndexPredicate<ChangeData>) p).isIndexOnly();
}
if (p instanceof AndPredicate
|| p instanceof OrPredicate
|| p instanceof NotPredicate) {
if (isBoolean(p)) {
for (int i = 0; i < p.getChildCount(); i++) {
if (!allNonIndexOnly(p.getChild(i))) {
return false;
@@ -217,12 +215,8 @@ public class IndexRewriteImpl implements IndexRewrite {
}
}
private static boolean isRewritePossible(Predicate<ChangeData> p) {
if (p.getClass() != AndPredicate.class
&& p.getClass() != OrPredicate.class
&& p.getClass() != NotPredicate.class) {
return false;
}
return p.getChildCount() > 0;
private static boolean isBoolean(Predicate<ChangeData> p) {
return p instanceof AndPredicate || p instanceof OrPredicate
|| p instanceof NotPredicate;
}
}