Package com.meidusa.amoeba.sqljep.function

Source Code of com.meidusa.amoeba.sqljep.function.UMinus

/*****************************************************************************
      SQLJEP - Java SQL Expression Parser 0.2
      November 1 2006
         (c) Copyright 2006, Alexey Gaidukov
      SQLJEP Author: Alexey Gaidukov

      SQLJEP is based on JEP 2.24 (http://www.singularsys.com/jep/)
           (c) Copyright 2002, Nathan Funk
      See LICENSE.txt for license information.
*****************************************************************************/

package com.meidusa.amoeba.sqljep.function;

import java.math.BigDecimal;

import com.meidusa.amoeba.sqljep.function.PostfixCommand;
import com.meidusa.amoeba.sqljep.ASTFunNode;
import com.meidusa.amoeba.sqljep.JepRuntime;
import com.meidusa.amoeba.sqljep.ParseException;
public final class UMinus extends PostfixCommand {
  final public int getNumberOfParameters() {
    return 1;
  }
 
  public Comparable<?>[] evaluate(ASTFunNode node, JepRuntime runtime) throws ParseException {
    node.childrenAccept(runtime.ev, null);
    Comparable<?>  param = runtime.stack.pop();
    return new Comparable<?>[]{param};
  }
 
  public static Comparable<?>  umin(Comparable<?>  param) throws ParseException {
    if (param == null) {
      return null;
    }
    if (param instanceof String) {
      param = parse((String)param);
    }
    if (param instanceof BigDecimal) {    // BigInteger is not supported
      return ((BigDecimal)param).negate();
    }
    if (param instanceof Double || param instanceof Float) {
      return new Double(-((Number)param).doubleValue());
    }
    if (param instanceof Number) {    // Long, Integer, Short, Byte
      return new Long(-((Number)param).longValue());
    }
    throw new ParseException(WRONG_TYPE+" "+param.getClass());
  }

  public Comparable<?> getResult(Comparable<?>... comparables)
      throws ParseException {
    return umin(comparables[0]);
  }
}

TOP

Related Classes of com.meidusa.amoeba.sqljep.function.UMinus

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.