From c28642062dbeaf12c3a3fcd7b4f614f3d4d0138e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 22 Jan 2023 01:34:27 +0100 Subject: [PATCH] Continued: - rewrote method, now with a local variable initialized with null. When the value from parameter 'day' cannot be solved, throw an IAE --- .../mxchange/jcoreee/dates/DayOfTheWeek.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) 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; } /** -- 2.39.5