Package org.yaac.server.egql.evaluator.function

Source Code of org.yaac.server.egql.evaluator.function.RoundFunction

package org.yaac.server.egql.evaluator.function;

import java.math.BigDecimal;

import org.yaac.server.egql.evaluator.EvaluationResult;
import org.yaac.server.egql.exception.EGQLException;
import org.yaac.server.egql.processor.ProcessData.ProcessDataRecord;
import org.yaac.shared.ErrorCode;

/**
* @author Max Zhu (thebbsky@gmail.com)
*
*/
public class RoundFunction extends Function {

  /**
   *
   */
  private static final long serialVersionUID = 1L;

  @Override
  public void validate() throws EGQLException {
    FunctionUtil.ensureParamSize(ops, 2);
  }

  @Override
  public EvaluationResult evaluate(ProcessDataRecord record) {
    if (this.ops.isEmpty()) {
      return new EvaluationResult(null).withWarning(ErrorCode.W114);
    } else if (this.ops.size() == 2) {
      EvaluationResult r = ops.get(0).evaluate(record);
      EvaluationResult scale = ops.get(1).evaluate(record);
      if (r.getPayload() instanceof Number && scale.getPayload() instanceof Number) {
        BigDecimal bd = (BigDecimal) r.getPayload();
        BigDecimal scaleBd = (BigDecimal) scale.getPayload();
        return new EvaluationResult(
            bd.setScale(scaleBd.intValue(), BigDecimal.ROUND_HALF_UP), r, scale);
      } else {
        return r.withWarning(ErrorCode.W113);
      }
    } else {
      return ops.get(0).evaluate(record).withWarning(ErrorCode.W114);
    }
  }

}
TOP

Related Classes of org.yaac.server.egql.evaluator.function.RoundFunction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.