private void verifyArguments(Operation<?> operation) { List<Expression<?>> arguments = operation.getArgs(); for (int i = 1; i < arguments.size(); ++i) { if (!(arguments.get(i) instanceof Constant<?>) && !(arguments.get(i) instanceof ParamExpression<?>) && !(arguments.get(i) instanceof PhraseElement) && !(arguments.get(i) instanceof TermElement)) { throw new IllegalArgumentException("operand was of unsupported type " + arguments.get(i).getClass().getName()); } } }
private void verifyArguments(Operation<?> operation) { List<Expression<?>> arguments = operation.getArgs(); for (int i = 1; i < arguments.size(); ++i) { if (!(arguments.get(i) instanceof Constant<?>) && !(arguments.get(i) instanceof ParamExpression<?>) && !(arguments.get(i) instanceof PhraseElement) && !(arguments.get(i) instanceof TermElement)) { throw new IllegalArgumentException("operand was of unsupported type " + arguments.get(i).getClass().getName()); } } }
private void verifyArguments(Operation<?> operation) { List<Expression<?>> arguments = operation.getArgs(); for (int i = 1; i < arguments.size(); ++i) { if (!(arguments.get(i) instanceof Constant<?>) && !(arguments.get(i) instanceof ParamExpression<?>) && !(arguments.get(i) instanceof PhraseElement) && !(arguments.get(i) instanceof TermElement)) { throw new IllegalArgumentException( "operand was of unsupported type " + arguments.get(i).getClass().getName()); } } }
@Override public Void visit(Operation<?> expr, List<Path<?>> paths) { visit(expr.getArgs(), paths); return null; }
@Override public Void visit(Operation<?> expr, QueryMetadata context) { visit(expr.getArgs(), context); return null; }
@Override public Path<?> visit(Operation<?> expr, Void context) { return visit(expr.getArgs()); }
@Override public Integer visit(Operation<?> expr, Void context) { int result = expr.getOperator().name().hashCode(); return 31 * result + expr.getArgs().hashCode(); }
@Override public final boolean equals(Object o) { if (o == this) { return true; } else if (o instanceof Operation<?>) { Operation<?> op = (Operation<?>) o; return op.getOperator() == operator && op.getArgs().equals(args) && op.getType().equals(getType()); } else { return false; } }
@Override public Expression<?> visit(Operation<?> expr, C context) { ImmutableList<Expression<?>> args = visit(expr.getArgs(), context); if (args.equals(expr.getArgs())) { return expr; } else if (expr instanceof Predicate) { return ExpressionUtils.predicate(expr.getOperator(), args); } else { return ExpressionUtils.operation(expr.getType(), expr.getOperator(), args); } }
@Override public Void visit(Operation<?> expr, Void context) { visitOperation(expr.getType(), expr.getOperator(), expr.getArgs()); return null; }
@Override public Set<Expression<?>> visit(Operation<?> expr, Set<Expression<?>> known) { if (expr.getOperator() == Ops.ALIAS) { known = add(known, expr.getArg(1)); } for (Expression<?> arg : expr.getArgs()) { known = arg.accept(this, known); } return known; }
@Override public String visit(Operation<?> o, Templates templates) { final Template template = templates.getTemplate(o.getOperator()); if (template != null) { final int precedence = templates.getPrecedence(o.getOperator()); final StringBuilder builder = new StringBuilder(); for (Template.Element element : template.getElements()) { final Object rv = element.convert(o.getArgs()); if (rv instanceof Expression) { if (precedence > -1 && rv instanceof Operation) { if (precedence < templates.getPrecedence(((Operation<?>) rv).getOperator())) { builder.append("("); builder.append(((Expression<?>) rv).accept(this, templates)); builder.append(")"); continue; } } builder.append(((Expression<?>) rv).accept(this, templates)); } else { builder.append(rv.toString()); } } return builder.toString(); } else { return "unknown operation with operator " + o.getOperator().name() + " and args " + o.getArgs(); } }
@SuppressWarnings("rawtypes") @Override public Expression<?> visit(Operation<?> expr, Context context) { Expression<?>[] args = new Expression<?>[expr.getArgs().size()]; for (int i = 0; i < args.length; i++) { Context c = new Context(); args[i] = expr.getArg(i).accept(this, c); context.add(c); } if (context.replace) { if (expr.getType().equals(Boolean.class)) { Predicate predicate = ExpressionUtils.predicate(expr.getOperator(), args); return !context.paths.isEmpty() ? exists(context, predicate) : predicate; } else { return ExpressionUtils.operation(expr.getType(), expr.getOperator(), args); } } else { return expr; } }
@SuppressWarnings("unchecked") @Test public void various() { Expression[] args = new Expression[]{new StringPath("x"), new StringPath("y")}; List<Operation<?>> operations = new ArrayList<Operation<?>>(); // paths.add(new ArrayOperation(String[].class, "p")); operations.add(new BooleanOperation(Ops.EQ, args)); operations.add(new ComparableOperation(String.class, Ops.SUBSTR_1ARG, args)); operations.add(new DateOperation(Date.class, Ops.DateTimeOps.CURRENT_DATE, args)); operations.add(new DateTimeOperation(Date.class,Ops.DateTimeOps.CURRENT_TIMESTAMP, args)); operations.add(new EnumOperation(ExampleEnum.class,Ops.ALIAS, args)); operations.add(new NumberOperation(Integer.class,Ops.ADD, args)); operations.add(new SimpleOperation(String.class,Ops.TRIM, args)); operations.add(new StringOperation(Ops.CONCAT, args)); operations.add(new TimeOperation(Time.class,Ops.DateTimeOps.CURRENT_TIME, args)); for (Operation<?> operation : operations) { Operation<?> other = ExpressionUtils.operation(operation.getType(), operation.getOperator(), ImmutableList.copyOf(operation.getArgs())); assertEquals(operation.toString(), operation.accept(ToStringVisitor.DEFAULT, Templates.DEFAULT)); assertEquals(operation.hashCode(), other.hashCode()); assertEquals(operation, other); assertNotNull(operation.getOperator()); assertNotNull(operation.getArgs()); assertNotNull(operation.getType()); } }
if (op.getArg(0) instanceof Operation) { usedOperators.add(((Operation<?>) op.getArg(0)).getOperator()); } else if (op.getArgs().size() > 1 && op.getArg(1) instanceof Operation) { usedOperators.add(((Operation<?>) op.getArg(1)).getOperator());
@Override public Set<RelationalPath<?>> visit(Operation<?> expr, Set<RelationalPath<?>> known) { for (Expression<?> arg : expr.getArgs()) { known = arg.accept(this, known); } return known; }
@Override public Void visit(Operation<?> expr, List<Path<?>> paths) { visit(expr.getArgs(), paths); return null; }
@Override public Integer visit(Operation<?> expr, Void context) { int result = expr.getOperator().name().hashCode(); return 31 * result + expr.getArgs().hashCode(); }
@Override public Void visit(Operation<?> expr, Void context) { visitOperation(expr.getType(), expr.getOperator(), expr.getArgs()); return null; }
@Override public Set<Expression<?>> visit(Operation<?> expr, Set<Expression<?>> known) { if (expr.getOperator() == Ops.ALIAS) { known = add(known, expr.getArg(1)); } for (Expression<?> arg : expr.getArgs()) { known = arg.accept(this, known); } return known; }