1 /* ====================================================================
2 * The Apache Software License, Version 1.1
3 *
4 * Copyright (c) 2003 Digital Clash LLC. All rights
5 * reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * ChronicJ team (http://www.chronicj.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
25 *
26 * 4. The names "ChronicJ" and "Digital Clash" not be used to endorse or
27 * promote products derived from this software without prior written
28 * permission. For written permission, please contact
29 * info@digitalclash.com.
30 *
31 * 5. Products derived from this software may not be called "ChronicJ",
32 * "Digital Clash", nor may "ChronicJ" or "Digital Clash" appear in
33 * their name, without prior written permission of Digital Clash LLC.
34 *
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 * ====================================================================
48 * This product includes software developed by the by the
49 * Apache Software Foundation (http://www.apache.org/).
50 * ====================================================================
51 */
52 package org.chronicj.impl;
53
54 import org.chronicj.TimePoint;
55
56 import java.util.Calendar;
57
58
59 /***
60 * <p>
61 * Implements support for temporal expressions of the form: "last Friday of the
62 * month" or "first Tuesday of the month". Expressions syntax is specified in
63 * the constructor call ({@link #DayInMonthTE(int, int)}).
64 * </p>
65 *
66 * <p>
67 * This class is based directly on patterns described in a paper by Martin
68 * Fowler which can be found <a
69 * href="http://martinfowler.com/apsupp/recurring.pdf">here</a>.
70 * </p>
71 *
72 * @author <a href="mlipper@US-ABP.com">Matthew Lipper</a>
73 *
74 * @see <a href="http://martinfowler.com/apsupp/recurring.pdf">Recurring Events
75 * for Calendars</a> by Martin Fowler
76 */
77 public class DayInMonthTE extends AbstractTemporalExpression {
78 private boolean ignoreWeek;
79 private int count;
80 private int dayIndex;
81
82 /***
83 * Creates a temporal expression using day of the week 1 to 7 and day of week
84 * in month which may be either positive or negative indicating whether
85 * count is from beginning or end of the month, respectively. For example,
86 * if the first day of the week is set to Sunday (set in {@link
87 * java.util.GregorianCalendar}):<br>
88 *
89 * <p>
90 * <code>TemporalExpression anExpression = new DayInMonthTE(3,2); //example 1</code><br>
91 * <code>TemporalExpression anotherExpression = new DayInMonthTE(6,-1);
92 * //example 2 </code>
93 * </p>
94 * Example 1 creates the expression "the second Tuesday" of the month.
95 * Example 2 creates the expression "the last Friday" of the month.
96 *
97 * @param dayIndex the day of the week numbered from 1 to 7
98 * @param count the week number from the beginning of the month if positive
99 * or the the week number from the end of the month if negative.
100 */
101 public DayInMonthTE(int dayIndex, int count) {
102 super();
103 this.dayIndex = dayIndex;
104 this.count = count;
105 ignoreWeek = false;
106 }
107
108 /***
109 * Creates a temporal expression using day of the week 1 to 7. For
110 * example:<br>
111 *
112 * <p>
113 * <code>TemporalExpression sunday = new DayInMonthTE(1); //Sunday</code><br>
114 * <code>TemporalExpression saturday = new DayInMonthTE(7);
115 * //Saturday</code><code>TemporalExpression thursday = new DayInMonthTE(5);
116 * //Thursday</code>
117 * </p>
118 *
119 * @param dayIndex the day of the week numbered from 1 to 7
120 */
121 public DayInMonthTE(int dayIndex) {
122 this(dayIndex, 1);
123 ignoreWeek = true;
124 }
125
126 /***
127 * Does the value of the supplied argument match this expression? If this
128 * instance was created with {@link DayInMonthTE#DayInMonthTE(int, int)}
129 * constructor: <br/
130 * > true = day of week matches and week of month matches, e.g. 3rd Tuesday
131 * of the month. If this instance was created with {@link
132 * DayInMonthTE#DayInMonthTE(int)} constructor: <br/
133 * > true = day of week matches, e.g. Friday.
134 *
135 * @param aTimePoint the TimePoint against which to check
136 *
137 * @return boolean true if the given TimePoint occurs during the time this
138 * expression describes.
139 *
140 * @see org.chronicj.TemporalExpression
141 */
142 public boolean includes(TimePoint aTimePoint) {
143 if (ignoreWeek) {
144 return dayMatches(aTimePoint);
145 }
146
147 return dayMatches(aTimePoint) && weekMatches(aTimePoint);
148 }
149
150 private boolean dayMatches(TimePoint aTimePoint) {
151 return aTimePoint.getDayOfWeek() == dayIndex;
152 }
153
154 private int daysLeftInMonth(TimePoint aTimePoint) {
155 int actualMaximum = aTimePoint.getCalendar().getActualMaximum(Calendar.DAY_OF_MONTH);
156
157 return actualMaximum - aTimePoint.getDayOfMonth();
158 }
159
160 private boolean weekFromEndMatches(TimePoint aTimePoint) {
161 int daysFromMonthEnd = daysLeftInMonth(aTimePoint) + 1;
162
163 return weekInMonth(daysFromMonthEnd) == Math.abs(count);
164 }
165
166 private boolean weekFromStartMatches(TimePoint aTimePoint) {
167 return this.weekInMonth(aTimePoint.getDayOfMonth()) == count;
168 }
169
170 private int weekInMonth(int dayNumber) {
171 return ((dayNumber - 1) / 7) + 1;
172 }
173
174 private boolean weekMatches(TimePoint aTimePoint) {
175 if (count > 0) {
176 return weekFromStartMatches(aTimePoint);
177 } else {
178 return weekFromEndMatches(aTimePoint);
179 }
180 }
181 }
This page was automatically generated by Maven