Debian Codegraph @

src/ch/poole/openinghoursparser/DateRange.java

raw symbols SCIP
1 package ch.poole.openinghoursparser;
2
3 import org.jetbrains.annotations.NotNull;
4 import org.jetbrains.annotations.Nullable;
5
6 /**
7 * Container for objects from the opening_hours specification
8 *
9 * @author Simon Poole
10 *
11 * Copyright (c) 2015, 2016, 2017, 2018, 2019 Simon Poole
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
14 * documentation files (the "Software"), to deal in the Software without restriction, including without
15 * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
16 * Software, and to permit persons to whom the Software is furnished to do so, subject to the following
17 * conditions:
18 *
19 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
20 * of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
23 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
25 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE " OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 */
28 public class DateRange extends Element {
29
30 DateWithOffset startDate = null;
31 DateWithOffset endDate = null;
32 int interval = 0;
33
34 /**
35 * Default constructor
36 */
37 public DateRange() {
38 // empty
39 }
40
41 /**
42 * Construct a new DateRange with the same content
43 *
44 * @param dr original DateRange
45 */
46 public DateRange(@NotNull DateRange dr) {
47 startDate = dr.startDate != null ? dr.startDate.copy() : null;
48 endDate = dr.endDate != null ? dr.endDate.copy() : null;
49 interval = dr.interval;
50 }
51
52 @Override
53 public String toString() {
54 checkStartDate();
55 StringBuilder b = new StringBuilder();
56 b.append(startDate.toString());
57
58 if (endDate != null && !endDate.isUndefined()) {
59 b.append("-");
60 b.append(endDate.toString());
61 }
62 if (interval > 0) {
63 b.append("/");
64 b.append(interval);
65 }
66 return b.toString();
67 }
68
69 /**
70 *
71 */
72 private void checkStartDate() {
73 if (startDate == null) {
74 throw new IllegalStateException("range must have a start date");
75 }
76 }
77
78 @Override
79 public String toDebugString() {
80 checkStartDate();
81 StringBuilder b = new StringBuilder();
82 b.append(getClass().getSimpleName() + ":");
83 b.append(startDate.toDebugString());
84
85 if (endDate != null && !endDate.isUndefined()) {
86 b.append("-");
87 b.append(endDate.toDebugString());
88 }
89 if (interval > 0) {
90 b.append("/");
91 b.append(interval);
92 }
93 return b.toString();
94 }
95
96 @Override
97 public boolean equals(Object other) {
98 if (this == other) {
99 return true;
100 }
101 if (other instanceof DateRange) {
102 DateRange o = (DateRange) other;
103 return Util.equals(startDate, o.startDate) && Util.equals(endDate, o.endDate) && interval == o.interval;
104 }
105 return false;
106 }
107
108 @Override
109 public int hashCode() {
110 int result = 1;
111 result = 37 * result + (startDate == null ? 0 : startDate.hashCode());
112 result = 37 * result + (endDate == null ? 0 : endDate.hashCode());
113 result = 37 * result + interval;
114 return result;
115 }
116
117 /**
118 * Get the start date of the range
119 *
120 * @return the startDate
121 */
122 @NotNull
123 public DateWithOffset getStartDate() {
124 checkStartDate();
125 return startDate;
126 }
127
128 /**
129 * Get the end date of the range
130 *
131 * @return the endDate, null if not present
132 */
133 @Nullable
134 public DateWithOffset getEndDate() {
135 return endDate;
136 }
137
138 /**
139 * Get the interval for the date range
140 *
141 * Note: currently the opening hours specification is quiet on what units this is supposed to be in
142 *
143 * @return the interval, 0 if no interval
144 */
145 public int getInterval() {
146 return interval;
147 }
148
149 /**
150 * Set the start date of the range
151 *
152 * @param startDate the startDate to set, can not be null
153 */
154 public void setStartDate(@NotNull DateWithOffset startDate) {
155 if (startDate == null) { // NOSONAR
156 throw new NullPointerException("startDate");
157 }
158 this.startDate = startDate;
159 }
160
161 /**
162 * Set the end date of the range
163 *
164 * @param endDate the end date of the range to set, null if there is no end date
165 */
166 public void setEndDate(@Nullable DateWithOffset endDate) {
167 this.endDate = endDate;
168 }
169
170 /**
171 * Set an interval for the date range
172 *
173 * Note: currently the opening hours specification is quiet on what units this is supposed to be in
174 *
175 * @param interval the interval to set
176 */
177 public void setInterval(int interval) {
178 this.interval = interval;
179 }
180
181 @Override
182 public DateRange copy() {
183 return new DateRange(this);
184 }
185 }