Frames | No Frames |
1: /** 2: * ========================================= 3: * LibFormula : a free Java formula library 4: * ========================================= 5: * 6: * Project Info: http://reporting.pentaho.org/libformula/ 7: * 8: * (C) Copyright 2006-2007, 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: WeekDayFunction.java,v 1.1 2007/05/20 21:47:52 mimil Exp $ 28: * ------------ 29: * (C) Copyright 2006-2007, by Pentaho Corporation. 30: */ 31: package org.jfree.formula.function.datetime; 32: 33: import java.util.Calendar; 34: import java.util.Date; 35: 36: import org.jfree.formula.EvaluationException; 37: import org.jfree.formula.FormulaContext; 38: import org.jfree.formula.LibFormulaErrorValue; 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.NumberType; 44: import org.jfree.formula.util.DateUtil; 45: 46: /** 47: * This function extracts the day of week from a date. <p/> The returned value 48: * depends of the Type passed as second argument using the following table:<br/> 49: * <TABLE> 50: * <TR> 51: * <TH>Day of Week</TH> 52: * <TH>Type=1 Result</TH> 53: * <TH>Type=2 Result</TH> 54: * <TH>Type=3 Result</TH> 55: * </TR> 56: * <TR> 57: * <TD>Sunday</TD> 58: * <TD> 1</TD> 59: * <TD> 7</TD> 60: * <TD> 6</TD> 61: * </TR> 62: * <TR> 63: * <TD>Monday</TD> 64: * <TD> 2</TD> 65: * <TD> 1</TD> 66: * <TD> 0</TD> 67: * </TR> 68: * <TR> 69: * <TD>Tuesday</TD> 70: * <TD> 3</TD> 71: * <TD> 2</TD> 72: * <TD> 1</TD> 73: * </TR> 74: * <TR> 75: * <TD>Wednesday</TD> 76: * <TD> 4</TD> 77: * <TD> 3</TD> 78: * <TD> 2</TD> 79: * </TR> 80: * <TR> 81: * <TD>Thursday</TD> 82: * <TD> 5</TD> 83: * <TD> 4</TD> 84: * <TD> 3</TD> 85: * </TR> 86: * <TR> 87: * <TD>Friday</TD> 88: * <TD> 6</TD> 89: * <TD> 5</TD> 90: * <TD> 4</TD> 91: * </TR> 92: * <TR> 93: * <TD>Saturday</TD> 94: * <TD> 7</TD> 95: * <TD> 6</TD> 96: * <TD> 5</TD> 97: * </TR> 98: * </TABLE> 99: * 100: * @author Cedric Pronzato 101: */ 102: public class WeekDayFunction implements Function 103: { 104: public WeekDayFunction() 105: { 106: } 107: 108: public String getCanonicalName() 109: { 110: return "WEEKDAY"; 111: } 112: 113: public TypeValuePair evaluate(final FormulaContext context, 114: final ParameterCallback parameters) throws EvaluationException 115: { 116: if (parameters.getParameterCount() > 2) 117: { 118: throw new EvaluationException(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE); 119: } 120: 121: final TypeRegistry typeRegistry = context.getTypeRegistry(); 122: 123: final Date d = typeRegistry.convertToDate(parameters.getType(0), parameters 124: .getValue(0)); 125: int type = 1; // default is Type 1 126: if (parameters.getParameterCount() == 2) 127: { 128: final Number n = typeRegistry.convertToNumber(parameters.getType(1), 129: parameters.getValue(1)); 130: if (n == null) 131: { 132: throw new EvaluationException( 133: LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE); 134: } 135: type = n.intValue(); 136: if (type < 1 || type > 3) 137: { 138: throw new EvaluationException( 139: LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE); 140: } 141: } 142: 143: if (d == null) 144: { 145: throw new EvaluationException( 146: LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE); 147: } 148: 149: final Calendar gc = DateUtil.createCalendar(d, context 150: .getLocalizationContext()); 151: 152: int dayOfWeek = gc.get(Calendar.DAY_OF_WEEK); 153: // in java Sunday = 1 (= Type 1 of openformula) 154: return new TypeValuePair(NumberType.GENERIC_NUMBER, new Integer( 155: convertType(dayOfWeek, type))); 156: } 157: 158: public int convertType(int currentDayOfWeek, int type) 159: { 160: if (type == 1) 161: { 162: return currentDayOfWeek; 163: } 164: else if (type == 2) 165: { 166: final int i = ((currentDayOfWeek + 6) % 8); 167: if(i == 7) 168: { 169: return i; 170: } 171: else 172: { 173: return i + 1; 174: } 175: } 176: else 177: { 178: return (currentDayOfWeek + 5) % 7; 179: } 180: } 181: }