From: Roland Häder Date: Sun, 22 Jan 2023 00:34:27 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=c28642062dbeaf12c3a3fcd7b4f614f3d4d0138e;p=jcoreee.git Continued: - rewrote method, now with a local variable initialized with null. When the value from parameter 'day' cannot be solved, throw an IAE --- diff --git a/src/org/mxchange/jcoreee/dates/DayOfTheWeek.java b/src/org/mxchange/jcoreee/dates/DayOfTheWeek.java index 059e387..d546c7b 100644 --- a/src/org/mxchange/jcoreee/dates/DayOfTheWeek.java +++ b/src/org/mxchange/jcoreee/dates/DayOfTheWeek.java @@ -16,6 +16,7 @@ */ package org.mxchange.jcoreee.dates; +import java.text.MessageFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; @@ -94,14 +95,27 @@ public enum DayOfTheWeek { * @return DateOfTheWeek instance */ public static DayOfTheWeek fromCalendarDay (final int day) { + // Init with null instance + DayOfTheWeek dayOfTheWeek = null; + // Walk through all values for (final DayOfTheWeek dayOfWeek : DayOfTheWeek.values()) { + // Has the day being found? if (dayOfWeek.toCalendar() == day) { - return dayOfWeek; + // Found it and break out from lopp + dayOfTheWeek = dayOfWeek; + break; } } - return null; // Consider throwing IllegalArgumentException + // Is it null? + if (null == dayOfTheWeek) { + // This means that the value of parameter 'day' is invalid + throw new IllegalArgumentException(MessageFormat.format("Value for day='{0}' cannot be solved into any day of the week.", day)); + } + + // Return found instance + return dayOfTheWeek; } /**