Debian Codegraph @

src/ch/poole/openinghoursparser/YearRange.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 YearRange extends Element {
33
34 public static final int FIRST_VALID_YEAR = 1900;
35 /**
36 * years are defined as positive integers greater than 1900 (in the spec non-inclusive) however that might change
37 * one day
38 */
39 public static final int UNDEFINED_YEAR = Integer.MIN_VALUE;
40 int startYear = UNDEFINED_YEAR;
41 int endYear = UNDEFINED_YEAR;
42 int interval = 0;
43 boolean openEnded = false;
44
45 /**
46 * Default constructor
47 */
48 public YearRange() {
49 // empty
50 }
51
52 /**
53 * Construct a new YearRange with the same contents
54 *
55 * @param yr original YearRange
56 */
57 public YearRange(@NotNull YearRange yr) {
58 startYear = yr.startYear;
59 endYear = yr.endYear;
60 interval = yr.interval;
61 openEnded = yr.openEnded;
62 }
63
64 @Override
65 public String toString() {
66 StringBuilder b = new StringBuilder();
67 b.append(String.format(Locale.US, "%04d", startYear));
68 if (endYear != UNDEFINED_YEAR) {
69 b.append("-");
70 b.append(String.format(Locale.US, "%04d", endYear));
71 if (interval > 0) {
72 b.append("/");
73 b.append(interval);
74 }
75 } else if (openEnded) {
76 b.append("+");
77 }
78 return b.toString();
79 }
80
81 @Override
82 public boolean equals(Object other) {
83 if (this == other) {
84 return true;
85 }
86 if (other instanceof YearRange) {
87 YearRange o = (YearRange) other;
88 if (startYear == o.startYear && endYear == o.endYear && interval == o.interval && openEnded == o.openEnded) {
89 return true;
90 }
91 }
92 return false;
93 }
94
95 @Override
96 public int hashCode() {
97 int result = 1;
98 result = 37 * result + startYear;
99 result = 37 * result + endYear;
100 result = 37 * result + interval;
101 result = 37 * result + (openEnded ? 0 : 1);
102 return result;
103 }
104
105 /**
106 * @return the startYear
107 */
108 public int getStartYear() {
109 return startYear;
110 }
111
112 /**
113 * @return the endYear
114 */
115 public int getEndYear() {
116 return endYear;
117 }
118
119 /**
120 * @return the interval
121 */
122 public int getInterval() {
123 return interval;
124 }
125
126 /**
127 * Set the start of the year range
128 *
129 * @param start the year to set
130 */
131 public void setStartYear(int start) {
132 if (start != UNDEFINED_YEAR && start < FIRST_VALID_YEAR) {
133 throw new IllegalArgumentException(tr("invalid_year_number", Integer.toString(start)));
134 }
135 this.startYear = start;
136 }
137
138 /**
139 * Set the end of the year range
140 *
141 * @param end the year to set
142 */
143 public void setEndYear(int end) {
144 if (end != UNDEFINED_YEAR && end < FIRST_VALID_YEAR) {
145 throw new IllegalArgumentException(tr("invalid_year_number", Integer.toString(end)));
146 }
147 this.endYear = end;
148 }
149
150 /**
151 * @param interval the interval to set
152 */
153 public void setInterval(int interval) {
154 this.interval = interval;
155 }
156
157 /**
158 * Check if this is open ended
159 *
160 * @return true if open ended
161 */
162 public boolean isOpenEnded() {
163 return openEnded;
164 }
165
166 /**
167 * Set if this range is open ended
168 *
169 * @param openEnded if true the range is open ended
170 */
171 public void setOpenEnded(boolean openEnded) {
172 this.openEnded = openEnded;
173 }
174
175 @Override
176 public YearRange copy() {
177 return new YearRange(this);
178 }
179 }