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