001: /*
002: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.mandarax.lib.date;
019:
020: import java.util.Calendar;
021:
022: import org.mandarax.kernel.Function;
023: import org.mandarax.kernel.Predicate;
024:
025: /**
026: * Class serving predicates and functions needed for date manipulations.
027: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
028: * @version 3.4 <7 March 05>
029: * @since 1.4
030: */
031: public class DateArithmetic {
032:
033: // constants
034: public static final String[] MONTHS = { "January", "February",
035: "March", "April", "May", "June", "July", "August",
036: "September", "October", "November", "December" };
037: public static final String[] DAYS = {
038: // indexing starts with 1 !!!
039: null, "Sunday", "Monday", "Tuesday", "Wednesday",
040: "Thursday", "Friday", "Saturday" };
041:
042: // class representing the diff function - returns the difference of two calendars in milli seconds as int value
043: public static class Diff extends BinaryDateFunction1 {
044:
045: public String getName() {
046: return "diff";
047: };
048:
049: public int compute(Calendar c1, Calendar c2) {
050: Long l = new Long(c1.getTimeInMillis()
051: + c1.get(Calendar.ZONE_OFFSET)
052: - c2.getTimeInMillis()
053: + c2.get(Calendar.ZONE_OFFSET));
054: return l.intValue(); // return difference in milli seconds as int
055: };
056: };
057:
058: // returns the day of the week (starting with sunday = 0)
059: public static class Day extends UnaryDateFunction1 {
060:
061: public String getName() {
062: return "day of month";
063: };
064:
065: public int compute(Calendar c) {
066: return c.get(Calendar.DAY_OF_MONTH);
067: };
068: };
069:
070: // returns the day of the week by its (english) name (sunday, monday)
071: public static class DayByName extends UnaryDateFunction2 {
072:
073: public String getName() {
074: return "day by name";
075: }
076:
077: public String compute(Calendar c) {
078: return DAYS[c.get(Calendar.DAY_OF_WEEK)];
079: }
080: };
081:
082: // returns the hours (0..23)
083: public static class Hours extends UnaryDateFunction1 {
084:
085: public String getName() {
086: return "hour";
087: }
088:
089: public int compute(Calendar c) {
090: return c.get(Calendar.HOUR);
091: }
092: };
093:
094: // returns the minutes (0..59)
095: public static class Minutes extends UnaryDateFunction1 {
096:
097: public String getName() {
098: return "minute";
099: }
100:
101: public int compute(Calendar c) {
102: return c.get(Calendar.MINUTE);
103: }
104: }
105:
106: // returns the month (0..11)
107: public static class Month extends UnaryDateFunction1 {
108:
109: public String getName() {
110: return "month";
111: }
112:
113: public int compute(Calendar c) {
114: return c.get(Calendar.MONTH);
115: }
116: }
117:
118: // returns the month by its (english) name (january, february, ..)
119: public static class MonthByName extends UnaryDateFunction2 {
120:
121: public String getName() {
122: return "month by name";
123: }
124:
125: public String compute(Calendar c) {
126: return MONTHS[c.get(Calendar.MONTH)];
127: }
128: };
129:
130: // returns the seconds (0..59)
131: public static class Seconds extends UnaryDateFunction1 {
132:
133: public String getName() {
134: return "second";
135: }
136:
137: public int compute(Calendar c) {
138: return c.get(Calendar.SECOND);
139: }
140: };
141:
142: // returns the year (0..11)
143: public static class Year extends UnaryDateFunction1 {
144:
145: public String getName() {
146: return "year";
147: }
148:
149: public int compute(Calendar c) {
150: return c.get(Calendar.YEAR);
151: }
152: };
153:
154: // compares dates (>)
155: public static class After extends BinaryDatePredicate {
156:
157: public String getName() {
158: return "after";
159: }
160:
161: public boolean compute(Calendar c1, Calendar c2) {
162: return c1.after(c2);
163: }
164: };
165:
166: // compares dates (>=)
167: // added by Adrian
168: public static class After_Or_Equals extends BinaryDatePredicate {
169:
170: public String getName() {
171: return "after_or_equals";
172: }
173:
174: public boolean compute(Calendar c1, Calendar c2) {
175: if (c1.equals(c2))
176: return true;
177: else if (c1.after(c2))
178: return true;
179: else
180: return false;
181:
182: }
183: };
184:
185: // compares dates (<)
186: public static class Before extends BinaryDatePredicate {
187:
188: public String getName() {
189: return "before";
190: }
191:
192: public boolean compute(Calendar c1, Calendar c2) {
193: return c1.before(c2);
194: }
195: };
196:
197: // compares dates (<=)
198: // added by Adrian
199: public static class Before_Or_Equals extends BinaryDatePredicate {
200:
201: public String getName() {
202: return "before_or_equals";
203: }
204:
205: public boolean compute(Calendar c1, Calendar c2) {
206: if (c1.equals(c2))
207: return true;
208: else if (c1.before(c2))
209: return true;
210: else
211: return false;
212: }
213: };
214:
215: // compares dates
216: public static class Equals extends BinaryDatePredicate {
217:
218: public String getName() {
219: return "equals (dates)";
220: }
221:
222: public boolean compute(Calendar c1, Calendar c2) {
223: return c1.equals(c2);
224: }
225: };
226:
227: // unary functions
228: public static final Function DAY = new DateArithmetic.Day();
229: public static final Function DAY_BY_NAME = new DateArithmetic.DayByName();
230: public static final Function HOURS = new DateArithmetic.Hours();
231: public static final Function MINUTES = new DateArithmetic.Minutes();
232: public static final Function MONTH = new DateArithmetic.Month();
233: public static final Function MONTH_BY_NAME = new DateArithmetic.MonthByName();
234: public static final Function SECONDS = new DateArithmetic.Seconds();
235: public static final Function YEAR = new DateArithmetic.Year();
236:
237: // binary functions
238: public static final Function DIFF = new DateArithmetic.Diff();
239:
240: // binary predicates
241: public static final Predicate AFTER = new DateArithmetic.After();
242: public static final Predicate AFTER_OR_EQUALS = new DateArithmetic.After_Or_Equals();
243: public static final Predicate BEFORE = new DateArithmetic.Before();
244: public static final Predicate BEFORE_OR_EQUALS = new DateArithmetic.Before_Or_Equals();
245: public static final Predicate EQUALS = new DateArithmetic.Equals();
246:
247: // all functions and predicates
248: public static final Function[] ALL_FUNCTIONS = { DIFF, YEAR, MONTH,
249: MONTH_BY_NAME, DAY, DAY_BY_NAME, HOURS, MINUTES, SECONDS };
250: public static final Predicate[] ALL_PREDICATES = { EQUALS, BEFORE,
251: AFTER, BEFORE_OR_EQUALS, AFTER_OR_EQUALS };
252:
253: }
|