Package com.meidusa.amoeba.sqljep.function

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

/*****************************************************************************
      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 static java.util.Calendar.DAY_OF_YEAR;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.Calendar;

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 class DayOfYear 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 Integer dayOfYear(Comparable<?>  param, Calendar cal) throws ParseException {
    if (param == null) {
      return null;
    }
    if (param instanceof Timestamp || param instanceof Date) {
      java.util.Date ts = (java.util.Date)param;
      cal.setTimeInMillis(ts.getTime());
      return new Integer(cal.get(DAY_OF_YEAR));
    }
    throw new ParseException(WRONG_TYPE+" weekofyear("+param.getClass()+")");
  }

  public Comparable<?> getResult(Comparable<?>... comparables)
      throws ParseException {
    return (dayOfYear(comparables[0], JepRuntime.getCalendar()));
  }
}
TOP

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

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.