PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms 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. package org.pentaho.reporting.libraries.formula.function.datetime;
  18. import java.util.Calendar;
  19. import java.util.Date;
  20. import java.math.BigDecimal;
  21. import org.pentaho.reporting.libraries.formula.EvaluationException;
  22. import org.pentaho.reporting.libraries.formula.FormulaContext;
  23. import org.pentaho.reporting.libraries.formula.LibFormulaErrorValue;
  24. import org.pentaho.reporting.libraries.formula.function.Function;
  25. import org.pentaho.reporting.libraries.formula.function.ParameterCallback;
  26. import org.pentaho.reporting.libraries.formula.lvalues.TypeValuePair;
  27. import org.pentaho.reporting.libraries.formula.typing.TypeRegistry;
  28. import org.pentaho.reporting.libraries.formula.typing.coretypes.NumberType;
  29. import org.pentaho.reporting.libraries.formula.util.DateUtil;
  30. /**
  31. * This function extracts the day of week from a date. <p/> The returned value
  32. * depends of the Type passed as second argument using the following table:<br/>
  33. * <TABLE>
  34. * <TR>
  35. * <TH>Day of Week</TH>
  36. * <TH>Type=1 Result</TH>
  37. * <TH>Type=2 Result</TH>
  38. * <TH>Type=3 Result</TH>
  39. * </TR>
  40. * <TR>
  41. * <TD>Sunday</TD>
  42. * <TD> 1</TD>
  43. * <TD> 7</TD>
  44. * <TD> 6</TD>
  45. * </TR>
  46. * <TR>
  47. * <TD>Monday</TD>
  48. * <TD> 2</TD>
  49. * <TD> 1</TD>
  50. * <TD> 0</TD>
  51. * </TR>
  52. * <TR>
  53. * <TD>Tuesday</TD>
  54. * <TD> 3</TD>
  55. * <TD> 2</TD>
  56. * <TD> 1</TD>
  57. * </TR>
  58. * <TR>
  59. * <TD>Wednesday</TD>
  60. * <TD> 4</TD>
  61. * <TD> 3</TD>
  62. * <TD> 2</TD>
  63. * </TR>
  64. * <TR>
  65. * <TD>Thursday</TD>
  66. * <TD> 5</TD>
  67. * <TD> 4</TD>
  68. * <TD> 3</TD>
  69. * </TR>
  70. * <TR>
  71. * <TD>Friday</TD>
  72. * <TD> 6</TD>
  73. * <TD> 5</TD>
  74. * <TD> 4</TD>
  75. * </TR>
  76. * <TR>
  77. * <TD>Saturday</TD>
  78. * <TD> 7</TD>
  79. * <TD> 6</TD>
  80. * <TD> 5</TD>
  81. * </TR>
  82. * </TABLE>
  83. *
  84. * @author Cedric Pronzato
  85. */
  86. public class WeekDayFunction implements Function
  87. {
  88. private static final long serialVersionUID = -825027235225096201L;
  89. public WeekDayFunction()
  90. {
  91. }
  92. public String getCanonicalName()
  93. {
  94. return "WEEKDAY";
  95. }
  96. public TypeValuePair evaluate(final FormulaContext context,
  97. final ParameterCallback parameters) throws EvaluationException
  98. {
  99. if (parameters.getParameterCount() > 2)
  100. {
  101. throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
  102. }
  103. final TypeRegistry typeRegistry = context.getTypeRegistry();
  104. final Date d = typeRegistry.convertToDate(parameters.getType(0), parameters.getValue(0));
  105. int type = 1; // default is Type 1
  106. if (parameters.getParameterCount() == 2)
  107. {
  108. final Number n = typeRegistry.convertToNumber(parameters.getType(1), parameters.getValue(1));
  109. if (n == null)
  110. {
  111. throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
  112. }
  113. type = n.intValue();
  114. if (type < 1 || type > 3)
  115. {
  116. throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
  117. }
  118. }
  119. if (d == null)
  120. {
  121. throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
  122. }
  123. final Calendar gc = DateUtil.createCalendar(d, context.getLocalizationContext());
  124. final int dayOfWeek = gc.get(Calendar.DAY_OF_WEEK);
  125. // in java Sunday = 1 (= Type 1 of openformula)
  126. final int result = convertType(dayOfWeek, type);
  127. //noinspection UnpredictableBigDecimalConstructorCall
  128. return new TypeValuePair(NumberType.GENERIC_NUMBER, new BigDecimal((double)result));
  129. }
  130. public int convertType(final int currentDayOfWeek, final int type)
  131. {
  132. if (type == 1)
  133. {
  134. return currentDayOfWeek;
  135. }
  136. else if (type == 2)
  137. {
  138. final int i = ((currentDayOfWeek + 6) % 8);
  139. if(i == 7)
  140. {
  141. return i;
  142. }
  143. else
  144. {
  145. return i + 1;
  146. }
  147. }
  148. else
  149. {
  150. return (currentDayOfWeek + 5) % 7;
  151. }
  152. }
  153. }