Debian Codegraph @

src/ch/poole/openinghoursparser/Holiday.java

raw symbols SCIP
1 package ch.poole.openinghoursparser;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Locale;
6
7 import org.jetbrains.annotations.NotNull;
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 public class Holiday extends Element {
32 public enum Type {
33 PH, SH;
34
35 /**
36 * Get list of all values as Strings
37 *
38 * @return a List with all values
39 */
40 public static List<String> nameValues() {
41 List<String> result = new ArrayList<>();
42 for (Type t : values()) {
43 result.add(t.toString());
44 }
45 return result;
46 }
47 }
48
49 Type type = null;
50 int offset = 0;
51 boolean useAsWeekDay = true;
52 boolean afterWeekDays = false;
53
54 /**
55 * Default constructor
56 */
57 public Holiday() {
58 // empty
59 }
60
61 /**
62 * Construct a new Holiday with the same content
63 *
64 * @param h original Holiday
65 */
66 public Holiday(@NotNull Holiday h) {
67 type = h.type; // enum
68 offset = h.offset;
69 useAsWeekDay = h.useAsWeekDay;
70 }
71
72 @Override
73 public String toString() {
74 StringBuilder b = new StringBuilder();
75 if (type != null) {
76 b.append(type.toString());
77 if (offset != 0) {
78 b.append(offset > 0 ? " +" : " -");
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 boolean equals(Object other) {
91 if (this == other) {
92 return true;
93 }
94 if (other instanceof Holiday) {
95 Holiday o = (Holiday) other;
96 return Util.equals(type, o.type) && offset == o.offset;
97 }
98 return false;
99 }
100
101 @Override
102 public int hashCode() {
103 int result = 1;
104 result = 37 * result + (type == null ? 0 : type.hashCode());
105 result = 37 * result + offset;
106 return result;
107 }
108
109 /**
110 * @return the type
111 */
112 public Type getType() {
113 return type;
114 }
115
116 /**
117 * @return the offset
118 */
119 public int getOffset() {
120 return offset;
121 }
122
123 /**
124 * @param type the type to set
125 */
126 public void setType(Type type) {
127 this.type = type;
128 }
129
130 /**
131 * @param offset the offset to set
132 */
133 public void setOffset(int offset) {
134 this.offset = offset;
135 }
136
137 /**
138 * Set how this holiday should be used
139 *
140 * @param useAsWeekDay if true the holiday is a weekday equivalent
141 */
142 public void setUseAsWeekDay(boolean useAsWeekDay) {
143 this.useAsWeekDay = useAsWeekDay;
144 }
145
146 /**
147 * Get the the "use as weekday" value
148 *
149 * @return true if this should be used as a weekday equivalent
150 */
151 public boolean getUseAsWeekDay() {
152 return useAsWeekDay;
153 }
154
155 /**
156 * Set the flag that the holidays should be shown after the weekdays
157 *
158 * This is necessary due to a specification change
159 *
160 * @param afterWeekDays the value to set
161 */
162 public void setAfterWeekDays(boolean afterWeekDays) {
163 this.afterWeekDays = afterWeekDays;
164 }
165
166 /**
167 * Check if the holiday was originally after the week days
168 *
169 * @return true if the holiday was after the week days
170 */
171 public boolean getAfterWeekDays() {
172 return afterWeekDays;
173 }
174
175 @Override
176 public Holiday copy() {
177 return new Holiday(this);
178 }
179 }