/** * {@inheritDoc} */ @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(type); if (type.isLogical()) { switch (type) { case NOT: sb.append('(').append(left).append(')'); break; case OR: case AND: sb.append('(').append(left).append(',').append(right).append(')'); break; } } else if (type.hasValue()) { sb.append('=').append(value); } else if (type == Type.RECURSIVE) { sb.append("(...)"); } return sb.toString(); }
/** * Utility, combine this expression with another, using disjunction. * The expression will match if either conditions is met. * @param expr the other expression * @return self for chaining */ public Expr or(Expr expr) { if (type == Type.TRUE || type == Type.FALSE || type == NONE) { set(expr); } else if (expr.type != Type.TRUE && expr.type != Type.FALSE && expr.type != Type.NONE) { if (type.isLogical() && !expr.type.isLogical()) { if (type == OR) right.or(expr); else setLogical(Type.OR, expr, new Expr(this)); } else setLogical(Type.OR, new Expr(this), expr); } return this; }
/** * Utility, combine this expression with another, using conjunction. * The expression will only match if both conditions are met. * * @param expr the other expression * @return self for chaining */ public Expr and(Expr expr) { if (type == Type.TRUE) { set(expr); } else if (expr.type != Type.TRUE) { if (type.isLogical() && !expr.type.isLogical()) { if (type == AND) right.and(expr); else setLogical(Type.AND, expr, new Expr(this)); } else { setLogical(Type.AND, new Expr(this), expr); } } return this; }
/** * Set the primitive value of this atom expression. * * @param type the type of expression * @param val the value to check */ public void setPrimitive(Type type, int val) { if (type.hasValue()) { this.type = type; this.value = val; this.left = null; this.right = null; this.query = null; } else { throw new IllegalArgumentException("Value provided for non-value " + "expression type!"); } }
/** * Set the primitive value of this atom expression. * * @param type the type of expression */ public void setPrimitive(Type type) { if (!type.hasValue() && !type.isLogical()) { this.type = type; this.value = -1; this.left = null; this.right = null; this.query = null; } else { throw new IllegalArgumentException("Expression type requires a value!"); } }