Debian Codegraph @

src/ch/poole/openinghoursparser/Rule.java

raw symbols SCIP
1 package ch.poole.openinghoursparser;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6
7 import org.jetbrains.annotations.NotNull;
8 import org.jetbrains.annotations.Nullable;
9
10 /**
11 * Container for objects from the opening_hours specification
12 *
13 * @author Simon Poole
14 *
15 * Copyright (c) 2015, 2016, 2017, 2018, 2019 Simon Poole
16 *
17 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
18 * documentation files (the "Software"), to deal in the Software without restriction, including without
19 * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
20 * Software, and to permit persons to whom the Software is furnished to do so, subject to the following
21 * conditions:
22 *
23 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
27 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE " OR THE USE OR OTHER
30 * DEALINGS IN THE SOFTWARE.
31 */
32 public class Rule extends Element {
33 boolean fallBack = false;
34 boolean additive = false;
35 boolean twentyfourseven = false;
36 boolean colonForClarification = false;
37
38 String comment = null;
39 // year range list
40 List<YearRange> years = null;
41 // week list
42 List<WeekRange> weeks = null;
43 // month day list
44 List<DateRange> dates = null;
45 // holiday list
46 List<Holiday> holidays = null;
47 // day list
48 List<WeekDayRange> days = null;
49 // times
50 List<TimeSpan> times = null;
51 // modifier
52 RuleModifier modifier = null;
53
54 /**
55 * Default constructor
56 */
57 public Rule() {
58 // empty
59 }
60
61 /**
62 * Copy constructor
63 *
64 * @param r the rule to copy
65 */
66 public Rule(@NotNull Rule r) {
67 fallBack = r.fallBack;
68 additive = r.additive;
69 twentyfourseven = r.twentyfourseven;
70 colonForClarification = r.colonForClarification;
71 comment = r.comment;
72 years = Util.copyList(r.years);
73 weeks = Util.copyList(r.weeks);
74 dates = Util.copyList(r.dates);
75 holidays = Util.copyList(r.holidays);
76 days = Util.copyList(r.days);
77 times = Util.copyList(r.times);
78 modifier = r.modifier != null ? r.modifier.copy() : null;
79 }
80
81 @Override
82 public String toString() {
83 StringBuilder b = new StringBuilder();
84 if (comment != null) {
85 b.append("\"" + comment + "\":"); // we only accept comments in quotes so only print them out this way
86 }
87 if (twentyfourseven) {
88 if (b.length() > 0) {
89 b.append(" ");
90 }
91 b.append("24/7");
92 } else {
93 printList(false, true, b, "", years);
94 printList(false, true, b, "week ", weeks);
95 printList(false, true, b, "", dates);
96 if (colonForClarification) {
97 b.append(":");
98 }
99 boolean addSpace = true;
100 // holiday handling is painful
101 List<Holiday> holidaysTemp = new ArrayList<>();
102 if (holidays != null) {
103 // first those that are not WD equivalent
104 for (Holiday h : holidays) {
105 if (!h.getUseAsWeekDay()) {
106 holidaysTemp.add(h);
107 }
108 }
109 if (!holidaysTemp.isEmpty()) {
110 printList(false, b.length() > 0, b, "", holidaysTemp);
111 holidaysTemp.clear();
112 }
113 // now equivalent to WD that are before the WDs
114 for (Holiday h : holidays) {
115 if (h.getUseAsWeekDay() && !h.getAfterWeekDays()) {
116 holidaysTemp.add(h);
117 }
118 }
119
120 if (!holidaysTemp.isEmpty()) {
121 printList(false, b.length() > 0, b, "", holidaysTemp);
122 if (days != null && !days.isEmpty()) {
123 b.append(",");
124 addSpace = false;
125 }
126 holidaysTemp.clear();
127 }
128 }
129 // print the week days
130 printList(false, addSpace, b, "", days);
131 if (holidays != null) {
132 // now holidays that were after the weekdays
133 for (Holiday h : holidays) {
134 if (h.getAfterWeekDays()) {
135 holidaysTemp.add(h);
136 }
137 }
138 if (!holidaysTemp.isEmpty()) {
139 if (days != null && !days.isEmpty()) {
140 b.append(",");
141 addSpace = false;
142 }
143 printList(false, addSpace, b, "", holidaysTemp);
144 }
145 }
146 printList(false, true, b, "", times);
147 }
148 if (modifier != null) {
149 if (b.length() > 0) {
150 b.append(" ");
151 }
152 b.append(modifier.toString());
153 }
154 return b.toString();
155 }
156
157 @Override
158 public String toDebugString() {
159 StringBuilder b = new StringBuilder();
160 if (comment != null) {
161 b.append("\"" + comment + "\":"); // we only accept comments in quotes so only print them out this way
162 }
163 if (twentyfourseven) {
164 if (b.length() > 0) {
165 b.append(" ");
166 }
167 b.append("24/7");
168 } else {
169 printList(true, true, b, "", years);
170 printList(true, true, b, "week ", weeks);
171 printList(true, true, b, "", dates);
172 printList(true, true, b, "", holidays);
173 boolean holidaysAsWeekDays = false;
174 if (holidays != null && !holidays.isEmpty() && holidays.get(holidays.size() - 1).getUseAsWeekDay() && days != null && !days.isEmpty()) {
175 b.append(",");
176 holidaysAsWeekDays = true;
177 }
178 printList(true, !holidaysAsWeekDays, b, "", days);
179 printList(true, true, b, "", times);
180 }
181 if (modifier != null) {
182 if (b.length() > 0) {
183 b.append(" ");
184 }
185 b.append(modifier.toDebugString());
186 }
187 return b.toString();
188 }
189
190 /**
191 * Output a human readable list of objects
192 *
193 * @param <T> the object class
194 * @param debug if true produce debug output
195 * @param addSpace add a space
196 * @param b the StringBuilder to append to
197 * @param prefix a prefix
198 * @param list the list of objects
199 */
200 <T extends Element> void printList(boolean debug, boolean addSpace, @NotNull StringBuilder b, @NotNull String prefix, @Nullable List<T> list) {
201 if (list != null) {
202 if (addSpace && b.length() > 0) {
203 b.append(" ");
204 }
205 b.append(prefix);
206 Iterator<T> iter = list.iterator();
207 while (iter.hasNext()) {
208 b.append(debug ? iter.next().toDebugString() : iter.next().toString());
209 if (iter.hasNext()) {
210 b.append(",");
211 }
212 }
213 }
214 }
215
216 @Override
217 public boolean equals(Object other) {
218 if (other instanceof Rule) {
219 Rule o = (Rule) other;
220 return fallBack == o.fallBack && additive == o.additive && Util.equals(comment, o.comment) && twentyfourseven == o.twentyfourseven
221 && Util.equals(years, o.years) && Util.equals(weeks, o.weeks) && Util.equals(dates, o.dates) && Util.equals(holidays, o.holidays)
222 && Util.equals(days, o.days) && Util.equals(times, o.times) && Util.equals(modifier, o.modifier);
223 }
224 return false;
225 }
226
227 @Override
228 public int hashCode() {
229 int result = 1;
230 result = 37 * result + (fallBack ? 0 : 1);
231 result = 37 * result + (additive ? 0 : 1);
232 result = 37 * result + (comment == null ? 0 : comment.hashCode());
233 result = 37 * result + (twentyfourseven ? 0 : 1);
234 result = 37 * result + (years == null ? 0 : years.hashCode());
235 result = 37 * result + (weeks == null ? 0 : weeks.hashCode());
236 result = 37 * result + (dates == null ? 0 : dates.hashCode());
237 result = 37 * result + (holidays == null ? 0 : holidays.hashCode());
238 result = 37 * result + (days == null ? 0 : days.hashCode());
239 result = 37 * result + (times == null ? 0 : times.hashCode());
240 result = 37 * result + (modifier == null ? 0 : modifier.hashCode());
241 return result;
242 }
243
244 /**
245 * Returns true if the input only differs in the days and times objects Current considers comments significant
246 *
247 * @param r rule to test against
248 * @return true if r can be merged with this rule
249 */
250 public boolean isMergeableWith(Rule r) {
251 return this.equals(r) || (!twentyfourseven && Util.equals(comment, r.comment) && Util.equals(years, r.years) && Util.equals(weeks, r.weeks)
252 && Util.equals(dates, r.dates) && Util.equals(modifier, r.modifier));
253 }
254
255 /**
256 * @return the fallBack
257 */
258 public boolean isFallBack() {
259 return fallBack;
260 }
261
262 /**
263 * @return true if this rule does not overwrite rules for days with previous rules
264 */
265 public boolean isAdditive() {
266 return additive;
267 }
268
269 /**
270 * @return the comment
271 */
272 public String getComment() {
273 return comment;
274 }
275
276 /**
277 * @return the twentyfourseven
278 */
279 public boolean isTwentyfourseven() {
280 return twentyfourseven;
281 }
282
283 /**
284 * @param twentyfourseven the twentyfourseven to set
285 */
286 public void setTwentyfourseven(boolean twentyfourseven) {
287 this.twentyfourseven = twentyfourseven;
288 }
289
290 /**
291 * Get a List of YearRange objects
292 *
293 * @return a List of YearRange objects or null if none exist
294 */
295 @Nullable
296 public List<YearRange> getYears() {
297 return years;
298 }
299
300 /**
301 * Get a List of WeekRange objects
302 *
303 * @return a List of WeekRange objects or null if none exist
304 */
305 @Nullable
306 public List<WeekRange> getWeeks() {
307 return weeks;
308 }
309
310 /**
311 * Get a List of DateRange objects
312 *
313 * @return a List of DateRange objects or null if none exist
314 */
315 @Nullable
316 public List<DateRange> getDates() {
317 return dates;
318 }
319
320 /**
321 * Get a List of Holiday objects
322 *
323 * @return a List of Holiday objects or null if none exists
324 */
325 @Nullable
326 public List<Holiday> getHolidays() {
327 return holidays;
328 }
329
330 /**
331 * Get a List of WeekDayRange objects
332 *
333 * @return a List of WeekDayRange objects or null if none exists
334 */
335 @Nullable
336 public List<WeekDayRange> getDays() {
337 return days;
338 }
339
340 /**
341 * Get a List of TimeSpan objects
342 *
343 * @return a List of TimeSpan objects or null if none exists
344 */
345 @Nullable
346 public List<TimeSpan> getTimes() {
347 return times;
348 }
349
350 /**
351 * Get the RuleModifier
352 *
353 * @return the modifier or null if none set
354 */
355 @Nullable
356 public RuleModifier getModifier() {
357 return modifier;
358 }
359
360 /**
361 * Set the fallback flag for this rule
362 *
363 * @param fallBack if true this is a fallback rule
364 */
365 public void setFallBack(boolean fallBack) {
366 this.fallBack = fallBack;
367 }
368
369 /**
370 * Set the additive flag for this rule
371 *
372 * @param additive if true this is an additive rule
373 */
374 public void setAdditive(boolean additive) {
375 this.additive = additive;
376 }
377
378 /**
379 * Set the comment for this rule
380 *
381 * @param comment the comment to set
382 */
383 public void setComment(@Nullable String comment) {
384 this.comment = comment;
385 }
386
387 /**
388 * Set the List of YearRange objects
389 *
390 * @param years the List of YearRange objects or null
391 */
392 public void setYears(@Nullable List<YearRange> years) {
393 this.years = years;
394 }
395
396 /**
397 * Set the List of WeekRange objects
398 *
399 * @param weeks the List of WeekRange objects or null
400 */
401 public void setWeeks(@Nullable List<WeekRange> weeks) {
402 this.weeks = weeks;
403 }
404
405 /**
406 * Set the list of DateRange objects
407 *
408 * @deprecated Use setDates instead
409 *
410 * @param monthdays a List of DateRange objects or null
411 */
412 @Deprecated
413 public void setMonthdays(@Nullable List<DateRange> monthdays) {
414 this.dates = monthdays;
415 }
416
417 /**
418 * Set the list of DateRange objects
419 *
420 * @param dates a List of DateRange objects or null
421 */
422 public void setDates(@Nullable List<DateRange> dates) {
423 this.dates = dates;
424 }
425
426 /**
427 * Set the List of Holiday objects
428 *
429 * @param holidays the List of Holiday objects or null
430 */
431 public void setHolidays(@Nullable List<Holiday> holidays) {
432 this.holidays = holidays;
433 }
434
435 /**
436 * Set the list of WeekDayRange objects
437 *
438 * @param days a List of WeekDayRange objects or null
439 */
440 public void setDays(@Nullable List<WeekDayRange> days) {
441 this.days = days;
442 }
443
444 /**
445 * Set the list of TimeSpan objects
446 *
447 * @param times a List of TimeSpan objects or null
448 */
449 public void setTimes(@Nullable List<TimeSpan> times) {
450 this.times = times;
451 }
452
453 /**
454 * @param modifier the modifier to set
455 */
456 public void setModifier(RuleModifier modifier) {
457 this.modifier = modifier;
458 }
459
460 /**
461 * Check if this rule actually contains something
462 *
463 * @return true if empty
464 */
465 public boolean isEmpty() {
466 // not done in one expression so that it remains legible
467 if (twentyfourseven) {
468 return false;
469 }
470 if (comment != null && !"".equals(comment)) {
471 return false;
472 }
473 if (years != null && !years.isEmpty()) {
474 return false;
475 }
476 if (weeks != null && !weeks.isEmpty()) {
477 return false;
478 }
479 if (dates != null && !dates.isEmpty()) {
480 return false;
481 }
482 if (holidays != null && !holidays.isEmpty()) {
483 return false;
484 }
485 if (days != null && !days.isEmpty()) {
486 return false;
487 }
488 if (times != null && !times.isEmpty()) {
489 return false;
490 }
491 return modifier == null;
492 }
493
494 @Override
495 public Rule copy() {
496 return new Rule(this);
497 }
498 }