1
2 package hoplugins.commons.utils;
3
4 import java.sql.Timestamp;
5
6 import java.util.Calendar;
7 import java.util.Date;
8 import java.util.GregorianCalendar;
9
10 /***
11 * <code>HTCalendar</code> is a class for calculating the Hattrick season and week number for a
12 * given date. Dates before October 15, 2000 can not be calculated using this calendar and will
13 * return both 0 for season and week.<br>
14 * Note:<br>
15 * October 15, 2000 was the start of Swedish season 11 and the first season for the first
16 * non-Swedish leagues that were created, namely:<br>
17 *
18 * <ul>
19 * <li>
20 * Argentina
21 * </li>
22 * <li>
23 * Germany
24 * </li>
25 * <li>
26 * Italy
27 * </li>
28 * <li>
29 * France
30 * </li>
31 * <li>
32 * England
33 * </li>
34 * <li>
35 * USA
36 * </li>
37 * <li>
38 * Mexico
39 * </li>
40 * </ul>
41 *
42 *
43 * @author <a href=mailto:nethyperon@users.sourceforge.net>Boy van der Werf</a>
44 */
45 public class HTCalendar {
46 /*** Number of weeks in a HT season */
47 private static final int WEEK_IN_SEASON = 16;
48
49 /*** Minimum number of elapsed week */
50 private static final int MIN_ELAPSED_WEEKS = 177;
51
52 /*** Start date of Swedich HT season 10 */
53 private static final Calendar HT_START = new GregorianCalendar(2000, 9, 15,
54 0, 0);
55
56 /*** Correction factor for number of seasons */
57 private static final int SE_CORRECTION = 10;
58
59 /***
60 * Calendar representing the date for which the Hattrick season and week needs to be
61 * calculated.
62 */
63 private Calendar calendar;
64
65 /*** Calculated total Hattrick weeks */
66 private int elapsedWeeks;
67 private int firstDayOfTheWeek = Calendar.SUNDAY;
68 private int seasonCorrection;
69
70 /***
71 * Constructs a HTCalendar.
72 */
73 HTCalendar() {
74 }
75
76 /***
77 * Gets the calculated Hattrick season or 0 if the date is before the league's first season.
78 *
79 * @return The calculated Hattrick season.
80 */
81 public final int getHTSeason() {
82 if (elapsedWeeks < MIN_ELAPSED_WEEKS) {
83 return 0;
84 }
85 else {
86 final int season = ((elapsedWeeks - 1) / WEEK_IN_SEASON)
87 - this.seasonCorrection;
88
89 if (season > 0) {
90 return season;
91 }
92 else {
93 return 0;
94 }
95 }
96 }
97
98 /***
99 * Gets the calculated Hattrick week. or 0 if the date is before the league's first season.
100 *
101 * @return The calculated Hattrick week.
102 */
103 public final int getHTWeek() {
104 if (getHTSeason() == 0) {
105 return 0;
106 }
107 else {
108 final int week = elapsedWeeks
109 - ((elapsedWeeks / WEEK_IN_SEASON) * WEEK_IN_SEASON);
110
111 if (week != 0) {
112 return week;
113 }
114 else {
115 return WEEK_IN_SEASON;
116 }
117 }
118 }
119
120 /***
121 * Sets the date for which to calculate the Hattrick season and week.
122 *
123 * @param time Date for which to calculate the Hattrick season and week.
124 */
125 public final void setTime(Timestamp time) {
126 setTime(new Date(time.getTime()));
127 }
128
129 /***
130 * Sets the date for which to calculate the Hattrick season and week.
131 *
132 * @param time Date for which to calculate the Hattrick season and week.
133 */
134 public final void setTime(Date time) {
135 final Calendar cal = new GregorianCalendar();
136
137 cal.setTime(time);
138 setTime(cal);
139 }
140
141 /***
142 * Sets the date for which to calculate the Hattrick season and week.
143 *
144 * @param cal Date for which to calculate the Hattrick season and week.
145 */
146 public final void setTime(Calendar cal) {
147
148 this.calendar = cal;
149 calculate();
150 }
151
152 /***
153 * Gets the date for which the Hattrick season and week is calculated.
154 *
155 * @return Date for which the Hattrick season and week is calculated.
156 */
157 public final Date getTime() {
158 return this.calendar.getTime();
159 }
160
161 /***
162 * Initializes the HTCalendar for flip-over point.
163 *
164 * @param marker Calendar representing a day on which the week ends/starts
165 */
166 final void initialize(Calendar marker) {
167 this.firstDayOfTheWeek = marker.get(Calendar.DAY_OF_WEEK);
168 HT_START.set(Calendar.HOUR_OF_DAY, marker.get(Calendar.HOUR_OF_DAY));
169 HT_START.set(Calendar.MINUTE, marker.get(Calendar.MINUTE));
170 }
171
172 /***
173 * Sets the season correction factor for the local league.
174 *
175 * @param correction TODO Missing Constructuor Parameter Documentation
176 */
177 final void setSeasonCorrection(int correction) {
178 this.seasonCorrection = correction;
179 }
180
181 /***
182 * Calculates the Hattrick season and week.
183 */
184 private void calculate() {
185 final Calendar cal = new GregorianCalendar();
186
187 cal.setTime(this.calendar.getTime());
188
189 final Calendar workCal = new GregorianCalendar();
190
191 workCal.setTime(HT_START.getTime());
192
193 int dayOfMonth = workCal.get(Calendar.DAY_OF_MONTH);
194
195 dayOfMonth += (this.firstDayOfTheWeek - 1);
196
197 if (this.firstDayOfTheWeek >= Calendar.FRIDAY) {
198 dayOfMonth -= 7;
199 }
200
201 workCal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
202
203 for (elapsedWeeks = 0; workCal.before(cal) || workCal.equals(cal);
204 elapsedWeeks++) {
205 workCal.add(Calendar.WEEK_OF_YEAR, 1);
206 }
207
208 elapsedWeeks += (WEEK_IN_SEASON * SE_CORRECTION);
209 }
210 }