All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.codetrans.expression.BinaryExpressionModel Maven / Gradle / Ivy

package io.vertx.codetrans.expression;

import io.vertx.codetrans.CodeBuilder;
import io.vertx.codetrans.CodeWriter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Julien Viet
 */
public class BinaryExpressionModel extends ExpressionModel {

  final ExpressionModel left;
  final String op;
  final ExpressionModel right;

  public BinaryExpressionModel(CodeBuilder builder, ExpressionModel left, String op, ExpressionModel right) {
    super(builder);
    this.left = left;
    this.op = op;
    this.right = right;
  }

  @Override
  public boolean isStringDecl() {
    return op.equals("+") && (left.isStringDecl() || right.isStringDecl());
  }

  public ExpressionModel getLeft() {
    return left;
  }

  public String getOp() {
    return op;
  }

  public ExpressionModel getRight() {
    return right;
  }

  @Override
  public void render(CodeWriter writer) {
    if (op.equals("+") && (left.isStringDecl() || right.isStringDecl())) {
      ArrayList parts = new ArrayList<>(); collectParts(parts);
      writer.renderStringLiteral(parts);
    } else {
      writer.renderBinary(this);
    }
  }

  void collectParts(List parts) {
    if (left.isStringDecl()) {
      left.collectParts(parts);
    } else {
      parts.add(left);
    }
    if (right.isStringDecl()) {
      right.collectParts(parts);
    } else {
      parts.add(right);
    }
  }
}