PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 80 lines | 45 code | 11 blank | 24 comment | 4 complexity | 4a5dd152e18565acca5a04de1e947838 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.math.BigDecimal;
  19. import org.pentaho.reporting.libraries.formula.EvaluationException;
  20. import org.pentaho.reporting.libraries.formula.FormulaContext;
  21. import org.pentaho.reporting.libraries.formula.LibFormulaErrorValue;
  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.NumberType;
  27. import org.pentaho.reporting.libraries.formula.util.NumberUtil;
  28. /**
  29. * This function extracts the minute (0 through 59) from a time.
  30. *
  31. * @author Cedric Pronzato
  32. */
  33. public class MinuteFunction implements Function
  34. {
  35. private static final BigDecimal MINUTES_PER_DAY = new BigDecimal(24.0 * 60);
  36. private static final BigDecimal MINUTES = new BigDecimal(60.0);
  37. private static final BigDecimal HOURS = new BigDecimal(24.0);
  38. public MinuteFunction()
  39. {
  40. }
  41. public String getCanonicalName()
  42. {
  43. return "MINUTE";
  44. }
  45. public TypeValuePair evaluate(final FormulaContext context,
  46. final ParameterCallback parameters) throws EvaluationException
  47. {
  48. if (parameters.getParameterCount() != 1)
  49. {
  50. throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
  51. }
  52. final TypeRegistry typeRegistry = context.getTypeRegistry();
  53. final Number n = typeRegistry.convertToNumber(parameters.getType(0), parameters.getValue(0));
  54. if (n == null)
  55. {
  56. throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
  57. }
  58. // calculation is as follows
  59. // time * 24 so that we get the full hours (which we remove later)
  60. final BigDecimal bd = NumberUtil.getAsBigDecimal(n);
  61. final BigDecimal hours = bd.multiply(HOURS);
  62. final BigDecimal dayAndHoursAsInt = new BigDecimal(NumberUtil.performIntRounding(hours).intValue());
  63. final BigDecimal minutesFraction = hours.subtract(dayAndHoursAsInt);
  64. // Multiply the minutes with 60 to get the minutes as ints
  65. final BigDecimal minutes = minutesFraction.multiply(MINUTES);
  66. final BigDecimal minutesAsInt = minutes.setScale(0, BigDecimal.ROUND_HALF_UP);
  67. return new TypeValuePair(NumberType.GENERIC_NUMBER, minutesAsInt);
  68. }
  69. }