Debian Codegraph @

src/ch/poole/openinghoursparser/TimeSpan.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 TimeSpan extends Element {
33 private static final int HOURS_24 = 1440;
34 public static final int UNDEFINED_TIME = Integer.MIN_VALUE;
35 public static final int MIN_TIME = 0;
36 public static final int MAX_TIME = HOURS_24;
37 public static final int MAX_EXTENDED_TIME = 2880;
38
39 int start = UNDEFINED_TIME;
40 VariableTime startEvent = null;
41 int end = UNDEFINED_TIME;
42 VariableTime endEvent = null;
43 boolean openEnded = false;
44 int interval = 0; // minutes
45
46 /**
47 * Default constructor
48 */
49 public TimeSpan() {
50 // empty
51 }
52
53 /**
54 * Construct a new TimeSpan with the same contents
55 *
56 * @param ts the original TimeSpan
57 */
58 public TimeSpan(@NotNull TimeSpan ts) {
59 start = ts.start;
60 startEvent = ts.startEvent != null ? ts.startEvent.copy() : null;
61 end = ts.end;
62 endEvent = ts.endEvent != null ? ts.endEvent.copy() : null;
63 openEnded = ts.openEnded;
64 interval = ts.interval;
65 }
66
67 @Override
68 public String toString() {
69 StringBuilder b = new StringBuilder();
70 if (startEvent != null) {
71 b.append(startEvent.toString());
72 } else {
73 appendTime(b, start);
74 }
75 if (endEvent != null) {
76 b.append("-");
77 b.append(endEvent.toString());
78 } else if (end != UNDEFINED_TIME) {
79 b.append("-");
80 // output as normal time if time span is less than 24 hours
81 int tempEnd = start != UNDEFINED_TIME && (end - start) < HOURS_24 && end > HOURS_24 ? end - HOURS_24 : end;
82 appendTime(b, tempEnd);
83 }
84 if (openEnded) {
85 b.append("+");
86 }
87 if (interval != 0) { // output only the full format
88 b.append("/");
89 appendTime(b, interval);
90
91 }
92 return b.toString();
93 }
94
95 /**
96 * Append a minute value as a time to the StringBuilder
97 *
98 * @param b the StringBuilder
99 * @param minutes the number of minutes
100 */
101 private void appendTime(@NotNull StringBuilder b, int minutes) {
102 b.append(String.format(Locale.US, "%02d", minutes / 60));
103 b.append(":");
104 b.append(String.format(Locale.US, "%02d", minutes % 60));
105 }
106
107 @Override
108 public boolean equals(Object other) {
109 if (this == other) {
110 return true;
111 }
112 if (other instanceof TimeSpan) {
113 TimeSpan o = (TimeSpan) other;
114 return start == o.start && Util.equals(startEvent, o.startEvent) && end == o.end && Util.equals(endEvent, o.endEvent) && openEnded == o.openEnded
115 && interval == o.interval;
116 }
117 return false;
118 }
119
120 @Override
121 public int hashCode() {
122 int result = 1;
123 result = 37 * result + start;
124 result = 37 * result + (startEvent == null ? 0 : startEvent.hashCode());
125 result = 37 * result + end;
126 result = 37 * result + (endEvent == null ? 0 : endEvent.hashCode());
127 result = 37 * result + (openEnded ? 0 : 1);
128 result = 37 * result + interval;
129 return result;
130 }
131
132 /**
133 * @return the start time in minutes
134 */
135 public int getStart() {
136 return start;
137 }
138
139 /**
140 * @return the startEvent
141 */
142 public VariableTime getStartEvent() {
143 return startEvent;
144 }
145
146 /**
147 * @return the end time in minutes
148 */
149 public int getEnd() {
150 return end;
151 }
152
153 /**
154 * @return the endEvent
155 */
156 public VariableTime getEndEvent() {
157 return endEvent;
158 }
159
160 /**
161 * @return the openEnded
162 */
163 public boolean isOpenEnded() {
164 return openEnded;
165 }
166
167 /**
168 * @return the interval
169 */
170 public int getInterval() {
171 return interval;
172 }
173
174 /**
175 * Set the start of range value
176 *
177 * @param s the start value to set
178 */
179 public void setStart(int s) {
180 if (invalidTime(s)) {
181 throw new IllegalArgumentException(tr("invalid_time", s));
182 }
183 this.start = s;
184 }
185
186 /**
187 * Check if time is valid/invalid
188 *
189 * @param t the time value
190 * @return true if invalid
191 */
192 private boolean invalidTime(int t) {
193 return t != UNDEFINED_TIME && (t < MIN_TIME || t > MAX_EXTENDED_TIME);
194 }
195
196 /**
197 * @param startEvent the startEvent to set
198 */
199 public void setStartEvent(VariableTime startEvent) {
200 this.startEvent = startEvent;
201 }
202
203 /**
204 * Set the end of range value
205 *
206 * @param e the end value to set
207 */
208 public void setEnd(int e) {
209 if (invalidTime(e)) {
210 throw new IllegalArgumentException(tr("invalid_time", e));
211 }
212 this.end = e;
213 }
214
215 /**
216 * @param endEvent the endEvent to set
217 */
218 public void setEndEvent(VariableTime endEvent) {
219 this.endEvent = endEvent;
220 }
221
222 /**
223 * @param openEnded the openEnded to set
224 */
225 public void setOpenEnded(boolean openEnded) {
226 this.openEnded = openEnded;
227 }
228
229 /**
230 * @param interval the interval to set
231 */
232 public void setInterval(int interval) {
233 this.interval = interval;
234 }
235
236 @Override
237 public TimeSpan copy() {
238 return new TimeSpan(this);
239 }
240 }