PageRenderTime 36ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 76 lines | 44 code | 11 blank | 21 comment | 8 complexity | 70b28b34ec1ecad3a1750c814e6bef4a 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 org.pentaho.reporting.libraries.formula.EvaluationException;
  19. import org.pentaho.reporting.libraries.formula.FormulaContext;
  20. import org.pentaho.reporting.libraries.formula.LibFormulaErrorValue;
  21. import org.pentaho.reporting.libraries.formula.LocalizationContext;
  22. import org.pentaho.reporting.libraries.formula.function.Function;
  23. import org.pentaho.reporting.libraries.formula.function.ParameterCallback;
  24. import org.pentaho.reporting.libraries.formula.lvalues.TypeValuePair;
  25. import org.pentaho.reporting.libraries.formula.typing.TypeRegistry;
  26. import org.pentaho.reporting.libraries.formula.typing.coretypes.DateTimeType;
  27. import org.pentaho.reporting.libraries.formula.util.DateUtil;
  28. /**
  29. * Creation-Date: 04.11.2006, 18:59:11
  30. *
  31. * @author Thomas Morgner
  32. */
  33. public class DateFunction implements Function
  34. {
  35. private static final long serialVersionUID = 4956151361696995668L;
  36. public DateFunction()
  37. {
  38. }
  39. public String getCanonicalName()
  40. {
  41. return "DATE";
  42. }
  43. public TypeValuePair evaluate(final FormulaContext context,
  44. final ParameterCallback parameters) throws EvaluationException
  45. {
  46. if (parameters.getParameterCount() != 3)
  47. {
  48. throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
  49. }
  50. final TypeRegistry typeRegistry = context.getTypeRegistry();
  51. final Number n1 = typeRegistry.convertToNumber(parameters.getType(0), parameters.getValue(0));
  52. final Number n2 = typeRegistry.convertToNumber(parameters.getType(1), parameters.getValue(1));
  53. final Number n3 = typeRegistry.convertToNumber(parameters.getType(2), parameters.getValue(2));
  54. if (n1 == null || n2 == null || n3 == null)
  55. {
  56. throw EvaluationException.getInstance(
  57. LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
  58. }
  59. final LocalizationContext localizationContext = context
  60. .getLocalizationContext();
  61. final java.sql.Date date = DateUtil.createDate(n1.intValue(),
  62. n2.intValue(), n3.intValue(), localizationContext);
  63. return new TypeValuePair(DateTimeType.DATE_TYPE, date);
  64. }
  65. }