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

io.sundr.model.If Maven / Gradle / Ivy

The newest version!
package io.sundr.model;

import java.util.Optional;

public class If implements Statement {

  private final Expression condition;
  private final Statement statement;
  private final Optional elseStatement;

  public If(Expression condition, Statement statement, Optional elseStatement) {
    this.condition = condition;
    this.statement = statement;
    this.elseStatement = elseStatement;
  }

  public If(Expression condition, Statement statement, Statement elseStatement) {
    this(condition, statement, Optional.ofNullable(elseStatement));
  }

  public If(Expression condition, Statement statement) {
    this(condition, statement, Optional.empty());
  }

  public Expression getCondition() {
    return condition;
  }

  public Statement getStatement() {
    return statement;
  }

  public Optional getElseStatement() {
    return elseStatement;
  }

  public String render() {
    StringBuilder sb = new StringBuilder();
    sb.append("if").append(SPACE)
        .append(OP).append(condition.render()).append(CP)
        .append(SPACE).append(OB).append(NEWLINE);

    sb.append(tab(statement.renderStatement()));
    sb.append(CB);
    elseStatement.ifPresent(e -> {
      sb.append(" else ");
      if (e instanceof If) {
        sb.append(e.render());
      } else {
        sb.append(OB);
        sb.append(NEWLINE);
        sb.append(tab(e.renderStatement()));
        sb.append(CB);
      }
    });
    sb.append(NEWLINE);
    return sb.toString();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy