Debian Codegraph @

src/ch/poole/openinghoursparser/WeekDayRange.java

raw symbols SCIP
1 package ch.poole.openinghoursparser;
2 /**
3 * Container for objects from the opening_hours specification
4 *
5 * @author Simon Poole
6 *
7 * Copyright (c) 2015 Simon Poole
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
10 * documentation files (the "Software"), to deal in the Software without restriction, including without
11 * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
12 * Software, and to permit persons to whom the Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE " OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Locale;
28
29 import org.jetbrains.annotations.NotNull;
30
31 public class WeekDayRange extends Element {
32
33 WeekDay startDay = null;
34 WeekDay endDay = null;
35 List<Nth> nths = null;
36 int offset = 0;
37
38 /**
39 * Default constructor
40 */
41 public WeekDayRange() {
42 // empty
43 }
44
45 /**
46 * Construct a new WeekDayRange with the same contents
47 *
48 * @param wdr original WeekDayRange
49 */
50 public WeekDayRange(@NotNull WeekDayRange wdr) {
51 startDay = wdr.startDay; // enum
52 endDay = wdr.endDay; // wdr
53 nths = (wdr.nths != null ? Util.copyList(wdr.nths) : null);
54 offset = wdr.offset;
55 }
56
57 @Override
58 public String toString() {
59 StringBuilder b = new StringBuilder();
60 b.append(startDay);
61 if (endDay != null) {
62 b.append("-");
63 b.append(endDay);
64 } else if (nths != null && !nths.isEmpty()) {
65 b.append("[");
66 for (Nth n : nths) {
67 b.append(n.toString());
68 if (!n.equals(nths.get(nths.size() - 1))) {
69 b.append(",");
70 }
71 }
72 b.append("]");
73 if (offset != 0) {
74 if (offset > 0) {
75 b.append(" +");
76 } else {
77 b.append(" -");
78 }
79 b.append(String.format(Locale.US, "%d", Math.abs(offset)));
80 b.append(" day");
81 if (Math.abs(offset) > 1) {
82 b.append("s");
83 }
84 }
85 }
86 return b.toString();
87 }
88
89 @Override
90 public String toDebugString() {
91 return getClass().getSimpleName() + ":" + toString();
92 }
93
94 @Override
95 public boolean equals(Object other) {
96 if (this == other) {
97 return true;
98 }
99 if (other instanceof WeekDayRange) {
100 WeekDayRange o = (WeekDayRange) other;
101 return Util.equals(startDay, o.startDay)
102 && Util.equals(endDay, o.endDay)
103 && Util.equals(nths, o.nths)
104 && offset == o.offset;
105 }
106 return false;
107 }
108
109 @Override
110 public int hashCode() {
111 int result = 1;
112 result = 37 * result + (startDay == null ? 0 : startDay.hashCode());
113 result = 37 * result + (endDay == null ? 0 : endDay.hashCode());
114 result = 37 * result + (nths == null ? 0 : nths.hashCode());
115 result = 37 * result + offset;
116 return result;
117 }
118
119 /**
120 * @return the startDay
121 */
122 public WeekDay getStartDay() {
123 return startDay;
124 }
125
126 /**
127 * @return the endDay
128 */
129 public WeekDay getEndDay() {
130 return endDay;
131 }
132
133 /**
134 * @return the nths
135 */
136 public List<Nth> getNths() {
137 return nths;
138 }
139
140 /**
141 * Set the day the range starts on
142 *
143 * @param day the day to set
144 */
145 public void setStartDay(WeekDay day) {
146 this.startDay = day;
147 }
148
149 /**
150 * Set the day the range starts on
151 *
152 * @param day the day to set
153 */
154 public void setStartDay(String day) {
155 if (day == null || "".equals(day)) {
156 this.startDay = null;
157 return;
158 }
159 this.startDay = WeekDay.getValue(day);
160 }
161
162 /**
163 * Set the day the range ends on
164 *
165 * @param day the day to set
166 */
167 public void setEndDay(WeekDay day) {
168 this.endDay = day;
169 }
170
171 /**
172 * Set the day the range ends on
173 *
174 * @param day the day to set
175 */
176 public void setEndDay(String day) {
177 this.endDay = WeekDay.getValue(day);
178 }
179
180 /**
181 * @param nths the nths to set
182 */
183 public void setNths(List<Nth> nths) {
184 this.nths = nths;
185 }
186
187 /**
188 * Add a occurrence in the month for a week day
189 *
190 * @param nth the n'th occurrence
191 */
192 public void add(Nth nth) {
193 if (nths == null) {
194 nths = new ArrayList<>();
195 }
196 nths.add(nth);
197 }
198
199 /**
200 * @return the offset
201 */
202 public int getOffset() {
203 return offset;
204 }
205
206 /**
207 * @param offset the offset to set
208 */
209 public void setOffset(int offset) {
210 this.offset = offset;
211 }
212
213 @Override
214 public WeekDayRange copy() {
215 return new WeekDayRange(this);
216 }
217 }