Debian Codegraph @

src/ch/poole/openinghoursparser/RuleModifier.java

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