Skip to content

Commit

Permalink
Minor code simplifications and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpoole committed Jan 9, 2024
1 parent ab32afd commit 42d1821
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 32 deletions.
39 changes: 28 additions & 11 deletions src/main/java/ch/poole/openinghoursparser/TimeSpan.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("-");
Expand All @@ -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) {
Expand Down Expand Up @@ -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
*/
Expand All @@ -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;
Expand Down
43 changes: 22 additions & 21 deletions src/main/java/ch/poole/openinghoursparser/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,21 @@ private static String rulesToOpeningHoursString(@NotNull List<Rule> 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();
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -173,7 +174,7 @@ public static String deWeekDays2En(String s) {
@SuppressWarnings("unchecked")
static <T extends Copy<?>> List<T> copyList(List<T> l) {
if (l == null) {
return null;
return null; // NOSONAR
}
List<T> r = new ArrayList<>(l.size());
for (T o : l) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 42d1821

Please sign in to comment.