Debian Codegraph @

src/ch/poole/openinghoursparser/DateWithOffset.java

raw symbols SCIP
1 package ch.poole.openinghoursparser;
2
3 import org.jetbrains.annotations.NotNull;
4
5 import static ch.poole.openinghoursparser.I18n.tr;
6
7 import java.util.Locale;
8
9 /**
10 * Container for objects from the opening_hours specification
11 *
12 * @author Simon Poole
13 *
14 * Copyright (c) 2015 Simon Poole
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
17 * documentation files (the "Software"), to deal in the Software without restriction, including without
18 * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
19 * Software, and to permit persons to whom the Software is furnished to do so, subject to the following
20 * conditions:
21 *
22 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
23 * of the Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
26 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
28 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE " OR THE USE OR OTHER
29 * DEALINGS IN THE SOFTWARE.
30 */
31
32 public class DateWithOffset extends Element {
33
34 private static final int MAX_NTH = 5;
35 public static final int UNDEFINED_MONTH_DAY = Integer.MIN_VALUE;
36 public static final int MIN_MONTH_DAY = 1;
37 public static final int MAX_MONTH_DAY = 31;
38
39 boolean openEnded = false;
40 int year = YearRange.UNDEFINED_YEAR;
41 Month month = null;
42 int day = UNDEFINED_MONTH_DAY;
43 WeekDay nthWeekDay = null;
44 int nth = 0;
45 boolean weekDayOffsetPositive = true;
46 WeekDay weekDayOffset = null;
47 int dayOffset = 0;
48
49 VarDate varDate = null;
50
51 /**
52 * Default constructor
53 */
54 public DateWithOffset() {
55 // empty
56 }
57
58 /**
59 * Construct a new DateWithOffset with the same content
60 *
61 * @param dwo original DateWithOffset
62 */
63 public DateWithOffset(@NotNull DateWithOffset dwo) {
64 openEnded = dwo.openEnded;
65 year = dwo.year;
66 month = dwo.month; // enum
67 day = dwo.day;
68 nthWeekDay = dwo.nthWeekDay; // enum
69 nth = dwo.nth;
70 weekDayOffsetPositive = dwo.weekDayOffsetPositive;
71 weekDayOffset = dwo.weekDayOffset;
72 dayOffset = dwo.dayOffset;
73 varDate = dwo.varDate; // enum
74 }
75
76 /**
77 * Check if we don't have a month day number or equivalent
78 *
79 * @return true if the day is undefined
80 */
81 public boolean undefinedDay() {
82 return day == UNDEFINED_MONTH_DAY && nthWeekDay == null && weekDayOffset == null && varDate == null;
83 }
84
85 /**
86 * Check for an undefined year value
87 *
88 * @return true if the year is undefined
89 */
90 public boolean undefinedYear() {
91 return year == YearRange.UNDEFINED_YEAR;
92 }
93
94 @Override
95 public String toString() {
96 StringBuilder b = new StringBuilder();
97 if (!undefinedYear()) {
98 b.append(year);
99 }
100 if (month != null) {
101 addSpaceIfYearisPresent(b);
102 b.append(month);
103 }
104 if (nthWeekDay != null) {
105 if (month != null) {
106 b.append(" ");
107 }
108 b.append(nthWeekDay);
109 b.append('[');
110 b.append(nth);
111 b.append(']');
112 }
113 if (day != UNDEFINED_MONTH_DAY) {
114 if (!undefinedYear() || month != null) {
115 b.append(" ");
116 }
117 b.append(String.format(Locale.US, "%02d", day));
118 } else if (varDate != null) {
119 addSpaceIfYearisPresent(b);
120 b.append(varDate);
121 }
122 // offsets
123 if (weekDayOffset != null) {
124 if (weekDayOffsetPositive) {
125 b.append("+");
126 } else {
127 b.append("-");
128 }
129 b.append(weekDayOffset);
130 }
131 if (dayOffset != 0) {
132 b.append(dayOffset > 0 ? " +" : " -");
133 b.append(String.format(Locale.US, "%d", Math.abs(dayOffset)));
134 b.append(" day");
135 if (Math.abs(dayOffset) > 1) {
136 b.append("s");
137 }
138 }
139 if (openEnded) {
140 b.append("+");
141 }
142 return b.toString();
143 }
144
145 /**
146 * Add a space to separate a year value from the following data
147 *
148 * @param b the StringBuilder
149 */
150 private void addSpaceIfYearisPresent(@NotNull StringBuilder b) {
151 if (!undefinedYear()) {
152 b.append(" ");
153 }
154 }
155
156 @Override
157 public boolean equals(Object other) {
158 if (other instanceof DateWithOffset) {
159 DateWithOffset o = (DateWithOffset) other;
160 return openEnded == o.openEnded && year == o.year && Util.equals(month, o.month) && Util.equals(nthWeekDay, o.nthWeekDay) && nth == o.nth
161 && day == o.day && weekDayOffsetPositive == o.weekDayOffsetPositive && Util.equals(weekDayOffset, o.weekDayOffset)
162 && dayOffset == o.dayOffset && Util.equals(varDate, o.varDate);
163 }
164 return false;
165 }
166
167 @Override
168 public int hashCode() {
169 int result = 1;
170 result = 37 * result + (openEnded ? 0 : 1);
171 result = 37 * result + year;
172 result = 37 * result + (month == null ? 0 : month.hashCode());
173 result = 37 * result + (nthWeekDay == null ? 0 : nthWeekDay.hashCode());
174 result = 37 * result + nth;
175 result = 37 * result + day;
176 result = 37 * result + (weekDayOffsetPositive ? 0 : 1);
177 result = 37 * result + (weekDayOffset == null ? 0 : weekDayOffset.hashCode());
178 result = 37 * result + dayOffset;
179 result = 37 * result + (varDate == null ? 0 : varDate.hashCode());
180 return result;
181 }
182
183 /**
184 * Check if this is open ended
185 *
186 * @return true if open ended
187 */
188 public boolean isOpenEnded() {
189 return openEnded;
190 }
191
192 /**
193 * @return the year
194 */
195 public int getYear() {
196 return year;
197 }
198
199 /**
200 * @return the month
201 */
202 public Month getMonth() {
203 return month;
204 }
205
206 /**
207 * @return the day
208 */
209 public int getDay() {
210 return day;
211 }
212
213 /**
214 * @return the weekDayOffsetPositive
215 */
216 public boolean isWeekDayOffsetPositive() {
217 return weekDayOffsetPositive;
218 }
219
220 /**
221 * @return the weekDayOffset
222 */
223 public WeekDay getWeekDayOffset() {
224 return weekDayOffset;
225 }
226
227 /**
228 * @return the dayOffset
229 */
230 public int getDayOffset() {
231 return dayOffset;
232 }
233
234 /**
235 * @return the varDate
236 */
237 public VarDate getVarDate() {
238 return varDate;
239 }
240
241 /**
242 * @param varDate the varDate to set
243 */
244 public void setVarDate(VarDate varDate) {
245 this.varDate = varDate;
246 }
247
248 /**
249 * Set a variable date
250 *
251 * @param date the var date
252 */
253 public void setVarDate(@NotNull String date) {
254 this.varDate = VarDate.getValue(date);
255 }
256
257 /**
258 * @param openEnded the openEnded to set
259 */
260 public void setOpenEnded(boolean openEnded) {
261 this.openEnded = openEnded;
262 }
263
264 /**
265 * Set the year value
266 *
267 * @param year the year to set
268 */
269 public void setYear(int year) {
270 if (year != YearRange.UNDEFINED_YEAR && year < YearRange.FIRST_VALID_YEAR) {
271 throw new IllegalArgumentException(tr("earlier_than", Integer.toString(year), Integer.toString(YearRange.FIRST_VALID_YEAR)));
272 }
273 this.year = year;
274 }
275
276 /**
277 * @param month the month to set
278 */
279 public void setMonth(Month month) {
280 this.month = month;
281 }
282
283 /**
284 * Set a month
285 *
286 * @param month the month
287 */
288 public void setMonth(String month) {
289 if (month == null || "".equals(month)) {
290 this.month = null;
291 return;
292 }
293 this.month = Month.getValue(month);
294 }
295
296 /**
297 * Set the month day number
298 *
299 * @param day the day to set
300 */
301 public void setDay(int day) {
302 if (day != UNDEFINED_MONTH_DAY && (day < MIN_MONTH_DAY || day > MAX_MONTH_DAY)) {
303 throw new IllegalArgumentException(tr("invalid_month_day", day));
304 }
305 this.day = day;
306 }
307
308 /**
309 * Set date specification via occurrence in month
310 *
311 * @param day week day to use
312 * @param nth the occurrence in the month positive number between -5 and 5, negative numbers are last occurrence
313 * (-1), before last (-2) and so on
314 */
315 public void setNth(String day, int nth) {
316 setNth(WeekDay.getValue(day), nth);
317 }
318
319 /**
320 * Set date specification via occurrence in month
321 *
322 * @param day WeekDay to use
323 * @param nth the occurrence in the month positive number between -5 and 5, negative numbers are last occurrence
324 * (-1), before last (-2) and so on
325 */
326 public void setNth(WeekDay day, int nth) {
327 if (nth < -MAX_NTH || nth > MAX_NTH) {
328 throw new IllegalArgumentException(tr("invalid_occurrence", nth));
329 }
330 nthWeekDay = day;
331 this.nth = nth;
332 }
333
334 /**
335 * Get the WeekDay for date specification via occurrence in month
336 *
337 * @return the WeekDay or null if not set
338 */
339 public WeekDay getNthWeekDay() {
340 return nthWeekDay;
341 }
342
343 /**
344 * Get the occurrence value for date specification via occurrence in month
345 *
346 * @return a integer between -5 and 5, valid only if the WeekDay has been set
347 */
348 public int getNth() {
349 return nth;
350 }
351
352 /**
353 * @param weekDayOffsetPositive the weekDayOffsetPositive to set
354 */
355 public void setWeekDayOffsetPositive(boolean weekDayOffsetPositive) {
356 this.weekDayOffsetPositive = weekDayOffsetPositive;
357 }
358
359 /**
360 * @param weekDayOffset the weekDayOffset to set
361 */
362 public void setWeekDayOffset(WeekDay weekDayOffset) {
363 this.weekDayOffset = weekDayOffset;
364 }
365
366 /**
367 * @param weekDayOffset the weekDayOffset to set
368 */
369 public void setWeekDayOffset(String weekDayOffset) {
370 this.weekDayOffset = WeekDay.getValue(weekDayOffset);
371 }
372
373 /**
374 * @param dayOffset the dayOffset to set
375 */
376 public void setDayOffset(int dayOffset) {
377 this.dayOffset = dayOffset;
378 }
379
380 /**
381 * Check if this object is undefined
382 *
383 * @return true if undefined
384 */
385 boolean isUndefined() {
386 return year == YearRange.UNDEFINED_YEAR && month == null && day == DateWithOffset.UNDEFINED_MONTH_DAY && varDate == null;
387 }
388
389 @Override
390 public DateWithOffset copy() {
391 return new DateWithOffset(this);
392 }
393 }