private SerializablePredicate<HString> generate(Expression e) { if (e.isInstance(ValueExpression.class)) { return s -> s.contains(e.as(ValueExpression.class).value); } else if (e.isInstance(PrefixExpression.class)) { PrefixExpression pe = e.as(PrefixExpression.class); if (pe.operator.getType().isInstance(Types.NOT)) { return negate(generate(pe.right)); } else if (pe.operator.getType().isInstance(Types.FIELD)) { final AttributeType attributeType = com.davidbracewell.hermes.Types.attribute( pe.operator.getText().substring(1, pe.operator.getText().length() - 2)); final SerializablePredicate<HString> predicate = generate(pe.right); return hString -> hString.document().contains(attributeType) && predicate.test( Fragments.string(hString.document().get(attributeType).asString())); } return generate(pe.right); } BinaryOperatorExpression boe = e.as(BinaryOperatorExpression.class); SerializablePredicate<HString> left = generate(boe.left); SerializablePredicate<HString> right = generate(boe.right); return boe.operator.getType().isInstance(Operator.AND) ? and(left, right) : or(left, right); }
SerializablePredicate<HString> or( SerializablePredicate<HString> l, SerializablePredicate<HString> r) { return (hString -> l.test(hString) || r.test(hString)); }
SerializablePredicate<HString> and( SerializablePredicate<HString> l, SerializablePredicate<HString> r) { return (hString -> l.test(hString) && r.test(hString)); }
SerializablePredicate<HString> negate(SerializablePredicate<HString> p) { return (hString -> !p.test(hString)); }