From 42d1821b0db3ada0fd9b3cd0da609b99c10d6354 Mon Sep 17 00:00:00 2001 From: simonpoole Date: Wed, 10 Jan 2024 00:24:59 +0100 Subject: [PATCH] Minor code simplifications and improvements --- .../ch/poole/openinghoursparser/TimeSpan.java | 39 ++++++++++++----- .../ch/poole/openinghoursparser/Util.java | 43 ++++++++++--------- 2 files changed, 50 insertions(+), 32 deletions(-) diff --git a/src/main/java/ch/poole/openinghoursparser/TimeSpan.java b/src/main/java/ch/poole/openinghoursparser/TimeSpan.java index 391ce2d..e3f9949 100644 --- a/src/main/java/ch/poole/openinghoursparser/TimeSpan.java +++ b/src/main/java/ch/poole/openinghoursparser/TimeSpan.java @@ -70,9 +70,7 @@ public String toString() { if (startEvent != null) { b.append(startEvent.toString()); } else { - b.append(String.format(Locale.US, "%02d", start / 60)); - b.append(":"); - b.append(String.format(Locale.US, "%02d", start % 60)); + appendTime(b, start); } if (endEvent != null) { b.append("-"); @@ -81,22 +79,31 @@ public String toString() { b.append("-"); // output as normal time if time span is less than 24 hours int tempEnd = start != UNDEFINED_TIME && (end - start) < HOURS_24 && end > HOURS_24 ? end - HOURS_24 : end; - b.append(String.format(Locale.US, "%02d", tempEnd / 60)); - b.append(":"); - b.append(String.format(Locale.US, "%02d", end % 60)); + appendTime(b, tempEnd); } if (openEnded) { b.append("+"); } if (interval != 0) { // output only the full format b.append("/"); - b.append(String.format(Locale.US, "%02d", interval / 60)); - b.append(":"); - b.append(String.format(Locale.US, "%02d", interval % 60)); + appendTime(b, interval); + } return b.toString(); } + /** + * Append a minute value as a time to the StringBuilder + * + * @param b the StringBuilder + * @param minutes the number of minutes + */ + private void appendTime(@NotNull StringBuilder b, int minutes) { + b.append(String.format(Locale.US, "%02d", minutes / 60)); + b.append(":"); + b.append(String.format(Locale.US, "%02d", minutes % 60)); + } + @Override public boolean equals(Object other) { if (this == other) { @@ -170,12 +177,22 @@ public int getInterval() { * @param s the start value to set */ public void setStart(int s) { - if (s != UNDEFINED_TIME && (s < MIN_TIME || s > MAX_EXTENDED_TIME)) { + if (invalidTime(s)) { throw new IllegalArgumentException(tr("invalid_time", s)); } this.start = s; } + /** + * Check if time is valid/invalid + * + * @param t the time value + * @return true if invalid + */ + private boolean invalidTime(int t) { + return t != UNDEFINED_TIME && (t < MIN_TIME || t > MAX_EXTENDED_TIME); + } + /** * @param startEvent the startEvent to set */ @@ -189,7 +206,7 @@ public void setStartEvent(VariableTime startEvent) { * @param e the end value to set */ public void setEnd(int e) { - if (e != UNDEFINED_TIME && (e < MIN_TIME || e > MAX_EXTENDED_TIME)) { + if (invalidTime(e)) { throw new IllegalArgumentException(tr("invalid_time", e)); } this.end = e; diff --git a/src/main/java/ch/poole/openinghoursparser/Util.java b/src/main/java/ch/poole/openinghoursparser/Util.java index 374142d..da20ddb 100644 --- a/src/main/java/ch/poole/openinghoursparser/Util.java +++ b/src/main/java/ch/poole/openinghoursparser/Util.java @@ -92,20 +92,21 @@ private static String rulesToOpeningHoursString(@NotNull List rules, boole StringBuilder result = new StringBuilder(); boolean first = true; for (Rule r : rules) { - if (!r.isEmpty()) { - if (!first) { - if (r.isAdditive()) { - result.append(", "); - } else if (r.isFallBack()) { - result.append(" || "); - } else { - result.append("; "); - } + if (r.isEmpty()) { + continue; + } + if (!first) { + if (r.isAdditive()) { + result.append(", "); + } else if (r.isFallBack()) { + result.append(" || "); } else { - first = false; + result.append("; "); } - result.append(debug ? r.toDebugString() : r.toString()); + } else { + first = false; } + result.append(debug ? r.toDebugString() : r.toString()); } return result.toString(); } @@ -145,19 +146,19 @@ public static String capitalize(String s) { public static String deWeekDays2En(String s) { switch (s.toLowerCase(Locale.US)) { case "mo": - return "Mo"; + return WeekDay.MO.toString(); case "di": - return "Tu"; + return WeekDay.TU.toString(); case "mi": - return "We"; + return WeekDay.WE.toString(); case "do": - return "Th"; + return WeekDay.TH.toString(); case "fr": - return "Fr"; + return WeekDay.FR.toString(); case "sa": - return "Sa"; + return WeekDay.SA.toString(); case "so": - return "Su"; + return WeekDay.SU.toString(); default: return null; } @@ -173,7 +174,7 @@ public static String deWeekDays2En(String s) { @SuppressWarnings("unchecked") static > List copyList(List l) { if (l == null) { - return null; + return null; // NOSONAR } List r = new ArrayList<>(l.size()); for (T o : l) { @@ -216,8 +217,8 @@ static boolean between(@NotNull String token, int lower, int upper) { } /** - * Returns {@code true} if the arguments are equal to each other - * and {@code false} otherwise. + * Returns {@code true} if the arguments are equal to each other and {@code false} otherwise. + * * @param a the first object * @param b the second object * @return true if the arguments are equal