Debian Codegraph @

src/ch/poole/openinghoursparser/Util.java

raw symbols SCIP
1 package ch.poole.openinghoursparser;
2
3 /**
4 * Utility methods for ops on OH rules
5 *
6 * @author Simon Poole
7 *
8 * Copyright (c) 2015 Simon Poole
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
11 * documentation files (the "Software"), to deal in the Software without restriction, including without
12 * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
13 * Software, and to permit persons to whom the Software is furnished to do so, subject to the following
14 * conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
20 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
22 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE " OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Locale;
29
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
32
33 public final class Util {
34
35 /**
36 * Default constructor
37 */
38 private Util() {
39 // dummy private constructor
40 }
41
42 /**
43 * find rules that only differs in the days and times objects and can be merged for display puposes
44 *
45 * @param rules rules to check
46 * @return list of list of rules that can be merged
47 */
48 public static List<List<Rule>> getMergeableRules(List<Rule> rules) {
49
50 List<List<Rule>> result = new ArrayList<>();
51
52 List<Rule> copy = new ArrayList<>(rules); // shallow copy for bookkeeping
53
54 while (!copy.isEmpty()) {
55 Rule r = copy.get(0);
56 boolean found = false;
57 for (List<Rule> mergeables : result) {
58 if (!mergeables.isEmpty() && r.isMergeableWith(mergeables.get(0))) {
59 mergeables.add(r);
60 found = true;
61 break;
62 }
63 }
64 if (!found) {
65 List<Rule> m = new ArrayList<>();
66 m.add(r);
67 result.add(m);
68 }
69 copy.remove(0);
70 }
71 return result;
72 }
73
74 /**
75 * Generate an OH string from rules
76 *
77 * @param rules rules to convert to an opening_hours string
78 * @return specification conformant opening_hours string
79 */
80 public static String rulesToOpeningHoursString(@NotNull List<Rule> rules) {
81 return rulesToOpeningHoursString(rules, false);
82 }
83
84 /**
85 * Generate an OH string from rules
86 *
87 * @param rules rules to convert to an opening_hours string
88 * @param debug if true add debug output
89 * @return opening_hours string
90 */
91 private static String rulesToOpeningHoursString(@NotNull List<Rule> rules, boolean debug) {
92 StringBuilder result = new StringBuilder();
93 boolean first = true;
94 for (Rule r : rules) {
95 if (r.isEmpty()) {
96 continue;
97 }
98 if (!first) {
99 if (r.isAdditive()) {
100 result.append(", ");
101 } else if (r.isFallBack()) {
102 result.append(" || ");
103 } else {
104 result.append("; ");
105 }
106 } else {
107 first = false;
108 }
109 result.append(debug ? r.toDebugString() : r.toString());
110 }
111 return result.toString();
112 }
113
114 /**
115 * Generate a debugging output string from rules
116 *
117 * @param rules rules to convert to an opening_hours string
118 * @return specification debugging opening_hours string
119 */
120 public static String rulesToOpeningHoursDebugString(@NotNull List<Rule> rules) {
121 return rulesToOpeningHoursString(rules, true);
122 }
123
124 /**
125 * Capitalize a string
126 *
127 * @param s string to capitalize
128 * @return capitalized string
129 */
130 public static String capitalize(String s) {
131 char[] c = s.toLowerCase(Locale.US).toCharArray();
132 if (c.length > 0) {
133 c[0] = Character.toUpperCase(c[0]);
134 return new String(c);
135 }
136 return s;
137 }
138
139 /**
140 * Convert two letter German weekdays to our standard
141 *
142 * @param s the String with the German weekday
143 * @return the standard weekday string
144 */
145 @Nullable
146 public static String deWeekDays2En(String s) {
147 switch (s.toLowerCase(Locale.US)) {
148 case "mo":
149 return WeekDay.MO.toString();
150 case "di":
151 return WeekDay.TU.toString();
152 case "mi":
153 return WeekDay.WE.toString();
154 case "do":
155 return WeekDay.TH.toString();
156 case "fr":
157 return WeekDay.FR.toString();
158 case "sa":
159 return WeekDay.SA.toString();
160 case "so":
161 return WeekDay.SU.toString();
162 default:
163 return null;
164 }
165 }
166
167 /**
168 * Copy a list, creating copies of its contents
169 *
170 * @param <T> object class
171 * @param l List to copy
172 * @return deep copy of the List or null if it is null
173 */
174 @SuppressWarnings("unchecked")
175 static <T extends Copy<?>> List<T> copyList(List<T> l) {
176 if (l == null) {
177 return null; // NOSONAR
178 }
179 List<T> r = new ArrayList<>(l.size());
180 for (T o : l) {
181 r.add((T) o.copy());
182 }
183 return r;
184 }
185
186 /**
187 * Make a safe comparison catching parse errors
188 *
189 * @param token the current token
190 * @param lower lower bounds
191 * @return true if greater or equals to limit and is an int
192 */
193 static boolean equalsOrMore(@NotNull String token, int lower) {
194 try {
195 int temp = Integer.parseInt(token);
196 return temp >= lower;
197 } catch (NumberFormatException nfe) {
198 return false;
199 }
200 }
201
202 /**
203 * Make a safe comparison catching parse errors
204 *
205 * @param token the current token
206 * @param lower lower bounds
207 * @param upper upper bounds
208 * @return true if in bounds and is an int
209 */
210 static boolean between(@NotNull String token, int lower, int upper) {
211 try {
212 int temp = Integer.parseInt(token);
213 return temp >= lower && temp <= upper;
214 } catch (NumberFormatException nfe) {
215 return false;
216 }
217 }
218
219 /**
220 * Returns {@code true} if the arguments are equal to each other and {@code false} otherwise.
221 *
222 * @param a the first object
223 * @param b the second object
224 * @return true if the arguments are equal
225 * @see java.util.Objects#equals
226 */
227 public static boolean equals(Object a, Object b) {
228 // Objects.equals might not be available on Android
229 return (a == b) || (a != null && a.equals(b));
230 }
231
232 }