Frames | No Frames |
1: /** 2: * ========================================= 3: * LibFormula : a free Java formula library 4: * ========================================= 5: * 6: * Project Info: http://jfreereport.pentaho.org/libformula 7: * 8: * (C) Copyright 2006, by Pentaho Corporation and Contributors. 9: * 10: * This library is free software; you can redistribute it and/or modify it under the terms 11: * of the GNU Lesser General Public License as published by the Free Software Foundation; 12: * either version 2.1 of the License, or (at your option) any later version. 13: * 14: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 15: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 16: * See the GNU Lesser General Public License for more details. 17: * 18: * You should have received a copy of the GNU Lesser General Public License along with this 19: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, 20: * Boston, MA 02111-1307, USA. 21: * 22: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 23: * in the United States and other countries.] 24: * 25: * 26: * ------------ 27: * $Id: TimeFunction.java,v 1.3 2007/05/21 18:57:59 mimil Exp $ 28: * ------------ 29: * (C) Copyright 2006, by Pentaho Corporation. 30: */ 31: package org.jfree.formula.function.datetime; 32: 33: import java.sql.Time; 34: 35: import org.jfree.formula.EvaluationException; 36: import org.jfree.formula.FormulaContext; 37: import org.jfree.formula.LibFormulaErrorValue; 38: import org.jfree.formula.LocalizationContext; 39: import org.jfree.formula.function.Function; 40: import org.jfree.formula.function.ParameterCallback; 41: import org.jfree.formula.lvalues.TypeValuePair; 42: import org.jfree.formula.typing.TypeRegistry; 43: import org.jfree.formula.typing.coretypes.DateTimeType; 44: import org.jfree.formula.util.DateUtil; 45: 46: /** 47: * This fonction constructs a time from hours, minutes, and seconds. 48: * 49: * @author Cedric Pronzato 50: */ 51: public class TimeFunction implements Function 52: { 53: public TimeFunction() 54: { 55: } 56: 57: public String getCanonicalName() 58: { 59: return "TIME"; 60: } 61: 62: public TypeValuePair evaluate(FormulaContext context, 63: ParameterCallback parameters) throws EvaluationException 64: { 65: if (parameters.getParameterCount() != 3) 66: { 67: throw new EvaluationException(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE); 68: } 69: Number n1; 70: Number n2; 71: Number n3; 72: try 73: { 74: final TypeRegistry typeRegistry = context.getTypeRegistry(); 75: n1 = typeRegistry.convertToNumber(parameters.getType(0), parameters 76: .getValue(0)); 77: n2 = typeRegistry.convertToNumber(parameters.getType(1), parameters 78: .getValue(1)); 79: n3 = typeRegistry.convertToNumber(parameters.getType(2), parameters 80: .getValue(2)); 81: } catch (NumberFormatException e) 82: { 83: throw new EvaluationException( 84: LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE); 85: } 86: 87: if (n1 == null || n2 == null || n3 == null) 88: { 89: throw new EvaluationException( 90: LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE); 91: } 92: final int hours = n1.intValue(); 93: final int minutes = n2.intValue(); 94: final int seconds = n3.intValue(); 95: 96: final LocalizationContext localizationContext = context 97: .getLocalizationContext(); 98: final Time time = DateUtil.createTime(hours, minutes, seconds, 99: localizationContext); 100: 101: return new TypeValuePair(DateTimeType.TIME_TYPE, time); 102: } 103: }