PageRenderTime 19ms CodeModel.GetById 7ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/libformula-1.1.3/source/org/pentaho/reporting/libraries/formula/function/datetime/WeekDayFunction.java

#
Java | 166 lines | 79 code | 13 blank | 74 comment | 16 complexity | 0f7d0115fcce45f5af4b302f5d9330b0 MD5 | raw file
Possible License(s): LGPL-2.1
  1/*
  2 * This program is free software; you can redistribute it and/or modify it under the
  3 * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
  4 * Foundation.
  5 *
  6 * You should have received a copy of the GNU Lesser General Public License along with this
  7 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
  8 * or from the Free Software Foundation, Inc.,
  9 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 10 *
 11 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 12 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 13 * See the GNU Lesser General Public License for more details.
 14 *
 15 * Copyright (c) 2006 - 2009 Pentaho Corporation and Contributors.  All rights reserved.
 16 */
 17
 18package org.pentaho.reporting.libraries.formula.function.datetime;
 19
 20import java.util.Calendar;
 21import java.util.Date;
 22import java.math.BigDecimal;
 23
 24import org.pentaho.reporting.libraries.formula.EvaluationException;
 25import org.pentaho.reporting.libraries.formula.FormulaContext;
 26import org.pentaho.reporting.libraries.formula.LibFormulaErrorValue;
 27import org.pentaho.reporting.libraries.formula.function.Function;
 28import org.pentaho.reporting.libraries.formula.function.ParameterCallback;
 29import org.pentaho.reporting.libraries.formula.lvalues.TypeValuePair;
 30import org.pentaho.reporting.libraries.formula.typing.TypeRegistry;
 31import org.pentaho.reporting.libraries.formula.typing.coretypes.NumberType;
 32import org.pentaho.reporting.libraries.formula.util.DateUtil;
 33
 34/**
 35 * This function extracts the day of week from a date. <p/> The returned value
 36 * depends of the Type passed as second argument using the following table:<br/>
 37 * <TABLE>
 38 * <TR>
 39 * <TH>Day of Week</TH>
 40 * <TH>Type=1 Result</TH>
 41 * <TH>Type=2 Result</TH>
 42 * <TH>Type=3 Result</TH>
 43 * </TR>
 44 * <TR>
 45 * <TD>Sunday</TD>
 46 * <TD> 1</TD>
 47 * <TD> 7</TD>
 48 * <TD> 6</TD>
 49 * </TR>
 50 * <TR>
 51 * <TD>Monday</TD>
 52 * <TD> 2</TD>
 53 * <TD> 1</TD>
 54 * <TD> 0</TD>
 55 * </TR>
 56 * <TR>
 57 * <TD>Tuesday</TD>
 58 * <TD> 3</TD>
 59 * <TD> 2</TD>
 60 * <TD> 1</TD>
 61 * </TR>
 62 * <TR>
 63 * <TD>Wednesday</TD>
 64 * <TD> 4</TD>
 65 * <TD> 3</TD>
 66 * <TD> 2</TD>
 67 * </TR>
 68 * <TR>
 69 * <TD>Thursday</TD>
 70 * <TD> 5</TD>
 71 * <TD> 4</TD>
 72 * <TD> 3</TD>
 73 * </TR>
 74 * <TR>
 75 * <TD>Friday</TD>
 76 * <TD> 6</TD>
 77 * <TD> 5</TD>
 78 * <TD> 4</TD>
 79 * </TR>
 80 * <TR>
 81 * <TD>Saturday</TD>
 82 * <TD> 7</TD>
 83 * <TD> 6</TD>
 84 * <TD> 5</TD>
 85 * </TR>
 86 * </TABLE>
 87 * 
 88 * @author Cedric Pronzato
 89 */
 90public class WeekDayFunction implements Function
 91{
 92  private static final long serialVersionUID = -825027235225096201L;
 93
 94  public WeekDayFunction()
 95  {
 96  }
 97
 98  public String getCanonicalName()
 99  {
100    return "WEEKDAY";
101  }
102
103  public TypeValuePair evaluate(final FormulaContext context,
104      final ParameterCallback parameters) throws EvaluationException
105  {
106    if (parameters.getParameterCount() > 2)
107    {
108      throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
109    }
110
111    final TypeRegistry typeRegistry = context.getTypeRegistry();
112
113    final Date d = typeRegistry.convertToDate(parameters.getType(0), parameters.getValue(0));
114    int type = 1; // default is Type 1
115    if (parameters.getParameterCount() == 2)
116    {
117      final Number n = typeRegistry.convertToNumber(parameters.getType(1), parameters.getValue(1));
118      if (n == null)
119      {
120        throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
121      }
122      type = n.intValue();
123      if (type < 1 || type > 3)
124      {
125        throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
126      }
127    }
128
129    if (d == null)
130    {
131      throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
132    }
133
134    final Calendar gc = DateUtil.createCalendar(d, context.getLocalizationContext());
135
136    final int dayOfWeek = gc.get(Calendar.DAY_OF_WEEK);
137    // in java Sunday = 1 (= Type 1 of openformula)
138    final int result = convertType(dayOfWeek, type);
139    //noinspection UnpredictableBigDecimalConstructorCall
140    return new TypeValuePair(NumberType.GENERIC_NUMBER, new BigDecimal((double)result));
141  }
142
143  public int convertType(final int currentDayOfWeek, final int type)
144  {
145    if (type == 1)
146    {
147      return currentDayOfWeek;
148    }
149    else if (type == 2)
150    {
151      final int i = ((currentDayOfWeek + 6) % 8);
152      if(i == 7)
153      {
154        return i;
155      }
156      else
157      {
158        return i + 1;
159      }
160    }
161    else
162    {
163      return (currentDayOfWeek + 5) % 7;
164    }
165  }
166}