From 4fcc82bcb1480ba813068333a64563bb7c65a45b Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Thu, 21 Mar 2024 09:21:52 -0400 Subject: [PATCH] Use ternary expression - Javadoc - Use fluent Objects.requireNonNull() - Remove some whitespace --- .../lang3/concurrent/BasicThreadFactory.java | 52 ++++++------------- 1 file changed, 17 insertions(+), 35 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/concurrent/BasicThreadFactory.java b/src/main/java/org/apache/commons/lang3/concurrent/BasicThreadFactory.java index dcf220108..8ea22f01b 100644 --- a/src/main/java/org/apache/commons/lang3/concurrent/BasicThreadFactory.java +++ b/src/main/java/org/apache/commons/lang3/concurrent/BasicThreadFactory.java @@ -26,7 +26,6 @@ import java.util.concurrent.atomic.AtomicLong; /** * An implementation of the {@link ThreadFactory} interface that provides some * configuration options for the threads it creates. - * *

* A {@link ThreadFactory} is used for instance by an {@link ExecutorService} to * create the threads it uses for executing tasks. In many cases users do not @@ -89,10 +88,10 @@ import java.util.concurrent.atomic.AtomicLong; * @since 3.0 */ public class BasicThreadFactory implements ThreadFactory { + /** * A builder class for creating instances of {@code * BasicThreadFactory}. - * *

* Using this builder class instances of {@link BasicThreadFactory} can be * created and initialized. The class provides methods that correspond to @@ -105,7 +104,7 @@ public class BasicThreadFactory implements ThreadFactory { implements org.apache.commons.lang3.builder.Builder { /** The wrapped factory. */ - private ThreadFactory wrappedFactory; + private ThreadFactory factory; /** The uncaught exception handler. */ private Thread.UncaughtExceptionHandler exceptionHandler; @@ -150,14 +149,12 @@ public class BasicThreadFactory implements ThreadFactory { * Sets the naming pattern to be used by the new {@code * BasicThreadFactory}. * - * @param pattern the naming pattern (must not be null) + * @param namingPattern the naming pattern (must not be null) * @return a reference to this {@link Builder} * @throws NullPointerException if the naming pattern is null */ - public Builder namingPattern(final String pattern) { - Objects.requireNonNull(pattern, "pattern"); - - namingPattern = pattern; + public Builder namingPattern(final String namingPattern) { + this.namingPattern = Objects.requireNonNull(namingPattern, "pattern"); return this; } @@ -180,7 +177,7 @@ public class BasicThreadFactory implements ThreadFactory { * automatically. */ public void reset() { - wrappedFactory = null; + factory = null; exceptionHandler = null; namingPattern = null; priority = null; @@ -191,16 +188,14 @@ public class BasicThreadFactory implements ThreadFactory { * Sets the uncaught exception handler for the threads created by the * new {@link BasicThreadFactory}. * - * @param handler the {@link UncaughtExceptionHandler} (must not be + * @param exceptionHandler the {@link UncaughtExceptionHandler} (must not be * null) * @return a reference to this {@link Builder} * @throws NullPointerException if the exception handler is null */ public Builder uncaughtExceptionHandler( - final Thread.UncaughtExceptionHandler handler) { - Objects.requireNonNull(handler, "handler"); - - exceptionHandler = handler; + final Thread.UncaughtExceptionHandler exceptionHandler) { + this.exceptionHandler = Objects.requireNonNull(exceptionHandler, "handler"); return this; } @@ -215,9 +210,7 @@ public class BasicThreadFactory implements ThreadFactory { * is null */ public Builder wrappedFactory(final ThreadFactory factory) { - Objects.requireNonNull(factory, "factory"); - - wrappedFactory = factory; + this.factory = Objects.requireNonNull(factory, "factory"); return this; } } @@ -247,22 +240,16 @@ public class BasicThreadFactory implements ThreadFactory { * @param builder the {@link Builder} object */ private BasicThreadFactory(final Builder builder) { - if (builder.wrappedFactory == null) { - wrappedFactory = Executors.defaultThreadFactory(); - } else { - wrappedFactory = builder.wrappedFactory; - } - + wrappedFactory = builder.factory != null ? builder.factory : Executors.defaultThreadFactory(); namingPattern = builder.namingPattern; priority = builder.priority; daemon = builder.daemon; uncaughtExceptionHandler = builder.exceptionHandler; - threadCounter = new AtomicLong(); } /** - * Returns the daemon flag. This flag determines whether newly created + * Gets the daemon flag. This flag determines whether newly created * threads should be daemon threads. If true, this factory object * calls {@code setDaemon(true)} on the newly created threads. Result can be * null if no daemon flag was provided at creation time. @@ -274,7 +261,7 @@ public class BasicThreadFactory implements ThreadFactory { } /** - * Returns the naming pattern for naming newly created threads. Result can + * Gets the naming pattern for naming newly created threads. Result can * be null if no naming pattern was provided. * * @return the naming pattern @@ -284,7 +271,7 @@ public class BasicThreadFactory implements ThreadFactory { } /** - * Returns the priority of the threads created by this factory. Result can + * Gets the priority of the threads created by this factory. Result can * be null if no priority was specified. * * @return the priority for newly created threads @@ -294,7 +281,7 @@ public class BasicThreadFactory implements ThreadFactory { } /** - * Returns the number of threads this factory has already created. This + * Gets the number of threads this factory has already created. This * class maintains an internal counter that is incremented each time the * {@link #newThread(Runnable)} method is invoked. * @@ -305,7 +292,7 @@ public class BasicThreadFactory implements ThreadFactory { } /** - * Returns the {@link UncaughtExceptionHandler} for the threads created by + * Gets the {@link UncaughtExceptionHandler} for the threads created by * this factory. Result can be null if no handler was provided. * * @return the {@link UncaughtExceptionHandler} @@ -315,7 +302,7 @@ public class BasicThreadFactory implements ThreadFactory { } /** - * Returns the wrapped {@link ThreadFactory}. This factory is used for + * Gets the wrapped {@link ThreadFactory}. This factory is used for * actually creating threads. This method never returns null. If no * {@link ThreadFactory} was passed when this object was created, a default * thread factory is returned. @@ -335,20 +322,16 @@ public class BasicThreadFactory implements ThreadFactory { * @param thread the thread to be initialized */ private void initializeThread(final Thread thread) { - if (getNamingPattern() != null) { final Long count = Long.valueOf(threadCounter.incrementAndGet()); thread.setName(String.format(getNamingPattern(), count)); } - if (getUncaughtExceptionHandler() != null) { thread.setUncaughtExceptionHandler(getUncaughtExceptionHandler()); } - if (getPriority() != null) { thread.setPriority(getPriority().intValue()); } - if (getDaemonFlag() != null) { thread.setDaemon(getDaemonFlag().booleanValue()); } @@ -366,7 +349,6 @@ public class BasicThreadFactory implements ThreadFactory { public Thread newThread(final Runnable runnable) { final Thread thread = getWrappedFactory().newThread(runnable); initializeThread(thread); - return thread; } }