Debian Codegraph @

src/ch/poole/openinghoursparser/WeekRange.java

raw symbols SCIP
1 package ch.poole.openinghoursparser;
2
3 import static ch.poole.openinghoursparser.I18n.tr;
4
5 import java.util.Locale;
6
7 /**
8 * Container for objects from the opening_hours specification
9 *
10 * @author Simon Poole
11 *
12 * Copyright (c) 2015 Simon Poole
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
15 * documentation files (the "Software"), to deal in the Software without restriction, including without
16 * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
17 * Software, and to permit persons to whom the Software is furnished to do so, subject to the following
18 * conditions:
19 *
20 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
21 * of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
24 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
26 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE " OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 */
29
30 public class WeekRange extends Element {
31 /**
32 * Valid week number are from 1 to 53
33 */
34 public static final int UNDEFINED_WEEK = Integer.MIN_VALUE;
35 public static final int MIN_WEEK = 1;
36 public static final int MAX_WEEK = 53;
37 int startWeek = UNDEFINED_WEEK;
38 int endWeek = UNDEFINED_WEEK;
39 int interval = 0;
40
41 /**
42 * Default constructor
43 */
44 public WeekRange() {
45 // empty
46 }
47
48 /**
49 * Construct a new WeekRange with the same contents
50 *
51 * @param wr the original WeekRange
52 */
53 public WeekRange(WeekRange wr) {
54 startWeek = wr.startWeek;
55 endWeek = wr.endWeek;
56 interval = wr.interval;
57 }
58
59 @Override
60 public String toString() {
61 StringBuilder b = new StringBuilder();
62 b.append(String.format(Locale.US, "%02d", startWeek));
63 if (endWeek != UNDEFINED_WEEK) {
64 b.append("-");
65 b.append(String.format(Locale.US, "%02d", endWeek));
66 if (interval > 0) {
67 b.append("/");
68 b.append(interval);
69 }
70 }
71 return b.toString();
72 }
73
74 @Override
75 public boolean equals(Object other) {
76 if (this == other) {
77 return true;
78 }
79 if (other instanceof WeekRange) {
80 WeekRange o = (WeekRange) other;
81 if (startWeek == o.startWeek && endWeek == o.endWeek && interval == o.interval) {
82 return true;
83 }
84 }
85 return false;
86 }
87
88 @Override
89 public int hashCode() {
90 int result = 1;
91 result = 37 * result + startWeek;
92 result = 37 * result + endWeek;
93 result = 37 * result + interval;
94 return result;
95 }
96
97 /**
98 * @return the startWeek
99 */
100 public int getStartWeek() {
101 return startWeek;
102 }
103
104 /**
105 * @return the endWeek
106 */
107 public int getEndWeek() {
108 return endWeek;
109 }
110
111 /**
112 * @return the interval
113 */
114 public int getInterval() {
115 return interval;
116 }
117
118 /**
119 * @param start the startWeek to set
120 */
121 public void setStartWeek(int start) {
122 if (start != UNDEFINED_WEEK && (start < MIN_WEEK || start > MAX_WEEK)) {
123 throw new IllegalArgumentException(tr("invalid_week_number", start));
124 }
125 this.startWeek = start;
126 }
127
128 /**
129 * @param end the endWeek to set
130 */
131 public void setEndWeek(int end) {
132 if (end != UNDEFINED_WEEK && (end < MIN_WEEK || end > MAX_WEEK)) {
133 throw new IllegalArgumentException(tr("invalid_week_number", end));
134 }
135 this.endWeek = end;
136 }
137
138 /**
139 * @param interval the interval to set
140 */
141 public void setInterval(int interval) {
142 this.interval = interval;
143 }
144
145 @Override
146 public WeekRange copy() {
147 return new WeekRange(this);
148 }
149 }