From 1323728b132f6b87109f89af121c45400e21e849 Mon Sep 17 00:00:00 2001 From: Sebastian Reddig Date: Sun, 15 Sep 2024 13:44:42 +0200 Subject: [PATCH] refactor: Standardize utility classes Standardizing means that the utility class has a private constructor that throws UnsupportedOperationException to make clear that there is never the intension to have an instance of that class. --- src/main/java/core/gui/theme/FontUtil.java | 1 + src/main/java/core/gui/theme/ImageUtilities.java | 4 ++++ src/main/java/core/util/CurrencyUtils.java | 4 +++- src/main/java/core/util/DateTimeUtils.java | 4 +--- src/main/java/core/util/ExceptionUtils.java | 5 +---- src/main/java/core/util/GUIUtils.java | 1 + src/main/java/core/util/Helper.java | 4 ++++ src/main/java/core/util/IOUtils.java | 4 +--- src/main/java/core/util/MathUtils.java | 4 ++++ src/main/java/core/util/OSUtils.java | 5 +++++ src/main/java/core/util/StringUtils.java | 4 +--- src/main/java/core/util/XMLUtils.java | 4 +--- src/main/java/module/ifa/PluginIfaUtils.java | 4 ++++ src/main/java/module/nthrf/NthrfUtil.java | 4 ++++ src/main/java/module/teamAnalyzer/ui/RatingUtil.java | 5 ++--- 15 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/main/java/core/gui/theme/FontUtil.java b/src/main/java/core/gui/theme/FontUtil.java index 17c9c14c7..dd08c9ad3 100644 --- a/src/main/java/core/gui/theme/FontUtil.java +++ b/src/main/java/core/gui/theme/FontUtil.java @@ -6,6 +6,7 @@ public class FontUtil { private FontUtil() { + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); } private static String checkInstalledFont(String targetFont, String sample, Font[] allfonts) { diff --git a/src/main/java/core/gui/theme/ImageUtilities.java b/src/main/java/core/gui/theme/ImageUtilities.java index 587d3e6be..7460abf3c 100644 --- a/src/main/java/core/gui/theme/ImageUtilities.java +++ b/src/main/java/core/gui/theme/ImageUtilities.java @@ -27,6 +27,10 @@ public class ImageUtilities { + private ImageUtilities() { + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); + } + /** Hashtable mit VerÀnderungspfeilgrafiken nach Integer als Key */ private static final Hashtable m_clPfeilCache = new Hashtable<>(); private static final Hashtable m_clPfeilWideCache = new Hashtable<>(); diff --git a/src/main/java/core/util/CurrencyUtils.java b/src/main/java/core/util/CurrencyUtils.java index 27ba7bf47..49d2e7b71 100644 --- a/src/main/java/core/util/CurrencyUtils.java +++ b/src/main/java/core/util/CurrencyUtils.java @@ -11,7 +11,9 @@ public class CurrencyUtils { public static String CURRENCYSYMBOL = ""; - private CurrencyUtils() {} + private CurrencyUtils() { + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); + } /** * convert currency value in swedish krone to local currency diff --git a/src/main/java/core/util/DateTimeUtils.java b/src/main/java/core/util/DateTimeUtils.java index fcd7a5994..a4fca2ae6 100644 --- a/src/main/java/core/util/DateTimeUtils.java +++ b/src/main/java/core/util/DateTimeUtils.java @@ -22,10 +22,8 @@ public class DateTimeUtils { private static Map cl_availableZoneIds; - /** - * Utility class - private constructor enforces non-instantiability. - */ private DateTimeUtils() { + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); } public static Map getAvailableZoneIds() { diff --git a/src/main/java/core/util/ExceptionUtils.java b/src/main/java/core/util/ExceptionUtils.java index 97e7cbedd..c5e293d72 100644 --- a/src/main/java/core/util/ExceptionUtils.java +++ b/src/main/java/core/util/ExceptionUtils.java @@ -10,11 +10,8 @@ */ public class ExceptionUtils { - /** - * Utility class - private constructor enforces noninstantiability. - */ private ExceptionUtils() { - // do nothing + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); } /** diff --git a/src/main/java/core/util/GUIUtils.java b/src/main/java/core/util/GUIUtils.java index 78dbab04c..0fcc2678e 100644 --- a/src/main/java/core/util/GUIUtils.java +++ b/src/main/java/core/util/GUIUtils.java @@ -16,6 +16,7 @@ public class GUIUtils { private GUIUtils() { + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); } /** diff --git a/src/main/java/core/util/Helper.java b/src/main/java/core/util/Helper.java index 49b20af22..9c12c619d 100644 --- a/src/main/java/core/util/Helper.java +++ b/src/main/java/core/util/Helper.java @@ -20,6 +20,10 @@ */ public class Helper { + private Helper() { + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); + } + /** * Form selections */ diff --git a/src/main/java/core/util/IOUtils.java b/src/main/java/core/util/IOUtils.java index add9e8749..92b035746 100644 --- a/src/main/java/core/util/IOUtils.java +++ b/src/main/java/core/util/IOUtils.java @@ -18,10 +18,8 @@ */ public class IOUtils { - /** - * Utility class - private constructor enforces noninstantiability. - */ private IOUtils() { + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); } /** diff --git a/src/main/java/core/util/MathUtils.java b/src/main/java/core/util/MathUtils.java index 7b59f4a98..75c9e2872 100644 --- a/src/main/java/core/util/MathUtils.java +++ b/src/main/java/core/util/MathUtils.java @@ -2,6 +2,10 @@ public class MathUtils { + private MathUtils() { + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); + } + static void checkNonNegative(String role, double x) { if (!(x >= 0)) { throw new IllegalArgumentException(role + " (" + x + ") must be >= 0"); diff --git a/src/main/java/core/util/OSUtils.java b/src/main/java/core/util/OSUtils.java index c8c4b5335..0f8847986 100644 --- a/src/main/java/core/util/OSUtils.java +++ b/src/main/java/core/util/OSUtils.java @@ -4,6 +4,11 @@ * Provides OS-specific utility functions. */ public final class OSUtils { + + private OSUtils() { + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); + } + public static final String OS_NAME = System.getProperty("os.name").toLowerCase(); public enum OS {WINDOWS, LINUX, MAC} private static OS os = determineOS(); diff --git a/src/main/java/core/util/StringUtils.java b/src/main/java/core/util/StringUtils.java index fc877a17f..4a71bdacf 100644 --- a/src/main/java/core/util/StringUtils.java +++ b/src/main/java/core/util/StringUtils.java @@ -8,10 +8,8 @@ */ public class StringUtils { - /** - * Utility class - private constructor enforces noninstantiability. - */ private StringUtils() { + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); } /** diff --git a/src/main/java/core/util/XMLUtils.java b/src/main/java/core/util/XMLUtils.java index 35c27bb10..845a4d8bc 100644 --- a/src/main/java/core/util/XMLUtils.java +++ b/src/main/java/core/util/XMLUtils.java @@ -17,10 +17,8 @@ public class XMLUtils { - /** - * Utility class - private constructor enforces noninstantiability. - */ private XMLUtils() { + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); } /** diff --git a/src/main/java/module/ifa/PluginIfaUtils.java b/src/main/java/module/ifa/PluginIfaUtils.java index a97996f88..49217f12f 100644 --- a/src/main/java/module/ifa/PluginIfaUtils.java +++ b/src/main/java/module/ifa/PluginIfaUtils.java @@ -27,6 +27,10 @@ public class PluginIfaUtils { + private PluginIfaUtils() { + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); + } + private static String getTeamDetails(int teamID) throws Exception { return MyConnector.instance().getTeamDetails(teamID); } diff --git a/src/main/java/module/nthrf/NthrfUtil.java b/src/main/java/module/nthrf/NthrfUtil.java index 09d728649..5e7ebdf7e 100644 --- a/src/main/java/module/nthrf/NthrfUtil.java +++ b/src/main/java/module/nthrf/NthrfUtil.java @@ -17,6 +17,10 @@ public class NthrfUtil { + private NthrfUtil() { + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); + } + /** * TODO * @return success of the operation diff --git a/src/main/java/module/teamAnalyzer/ui/RatingUtil.java b/src/main/java/module/teamAnalyzer/ui/RatingUtil.java index 3918a3ac2..6a10f21f0 100644 --- a/src/main/java/module/teamAnalyzer/ui/RatingUtil.java +++ b/src/main/java/module/teamAnalyzer/ui/RatingUtil.java @@ -14,10 +14,9 @@ * @author Massimiliano Amato */ public final class RatingUtil { - /** - * Private default constructor to prevent class instantiation. - */ + private RatingUtil() { + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); } /**