Use ternary expression

- Javadoc
- Use fluent Objects.requireNonNull()
- Remove some whitespace
This commit is contained in:
Gary Gregory 2024-03-21 09:21:52 -04:00
parent 155abe805b
commit 4fcc82bcb1
1 changed files with 17 additions and 35 deletions

View File

@ -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.
*
* <p>
* 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 <em>builder</em> class for creating instances of {@code
* BasicThreadFactory}.
*
* <p>
* 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<BasicThreadFactory> {
/** 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 <b>null</b>)
* @param namingPattern the naming pattern (must not be <b>null</b>)
* @return a reference to this {@link Builder}
* @throws NullPointerException if the naming pattern is <b>null</b>
*/
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
* <b>null</b>)
* @return a reference to this {@link Builder}
* @throws NullPointerException if the exception handler is <b>null</b>
*/
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 <b>null</b>
*/
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 <b>true</b>, this factory object
* calls {@code setDaemon(true)} on the newly created threads. Result can be
* <b>null</b> 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 <b>null</b> 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 <b>null</b> 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 <b>null</b> 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 <b>null</b>. 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;
}
}