From b31758e884a74deae9ea6c115723a3092220cfd4 Mon Sep 17 00:00:00 2001 From: XMRZombie Date: Sun, 10 Nov 2024 13:39:12 +0000 Subject: [PATCH] improve robustness and clarity of SingleThreadExecutorUtils.java (#1378) --- .../util/SingleThreadExecutorUtils.java | 45 +++++++++++++++---- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/common/src/main/java/haveno/common/util/SingleThreadExecutorUtils.java b/common/src/main/java/haveno/common/util/SingleThreadExecutorUtils.java index 6e336c00..d9af624c 100644 --- a/common/src/main/java/haveno/common/util/SingleThreadExecutorUtils.java +++ b/common/src/main/java/haveno/common/util/SingleThreadExecutorUtils.java @@ -11,8 +11,8 @@ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public * License for more details. * - * You should have received a copy of the GNU Affero General Public License - * along with Bisq. If not, see . + * You should have received a copy of the GNU Affero General Public + * License along with Bisq. If not, see . */ package haveno.common.util; @@ -25,38 +25,67 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; +/** + * Utility class for creating single-threaded executors. + */ public class SingleThreadExecutorUtils { + + private SingleThreadExecutorUtils() { + // Prevent instantiation + } + public static ExecutorService getSingleThreadExecutor(Class aClass) { - String name = aClass.getSimpleName(); - return getSingleThreadExecutor(name); + validateClass(aClass); + return getSingleThreadExecutor(aClass.getSimpleName()); } public static ExecutorService getNonDaemonSingleThreadExecutor(Class aClass) { - String name = aClass.getSimpleName(); - return getSingleThreadExecutor(name, false); + validateClass(aClass); + return getSingleThreadExecutor(aClass.getSimpleName(), false); } public static ExecutorService getSingleThreadExecutor(String name) { + validateName(name); return getSingleThreadExecutor(name, true); } public static ListeningExecutorService getSingleThreadListeningExecutor(String name) { + validateName(name); return MoreExecutors.listeningDecorator(getSingleThreadExecutor(name)); } public static ExecutorService getSingleThreadExecutor(ThreadFactory threadFactory) { + validateThreadFactory(threadFactory); return Executors.newSingleThreadExecutor(threadFactory); } private static ExecutorService getSingleThreadExecutor(String name, boolean isDaemonThread) { - final ThreadFactory threadFactory = getThreadFactory(name, isDaemonThread); + ThreadFactory threadFactory = getThreadFactory(name, isDaemonThread); return Executors.newSingleThreadExecutor(threadFactory); } private static ThreadFactory getThreadFactory(String name, boolean isDaemonThread) { return new ThreadFactoryBuilder() - .setNameFormat(name) + .setNameFormat(name + "-%d") .setDaemon(isDaemonThread) .build(); } + + private static void validateClass(Class aClass) { + if (aClass == null) { + throw new IllegalArgumentException("Class must not be null."); + } + } + + private static void validateName(String name) { + if (name == null || name.isEmpty()) { + throw new IllegalArgumentException("Name must not be null or empty."); + } + } + + private static void validateThreadFactory(ThreadFactory threadFactory) { + if (threadFactory == null) { + throw new IllegalArgumentException("ThreadFactory must not be null."); + } + } }