Use ternary expression
- Javadoc - Use fluent Objects.requireNonNull() - Remove some whitespace
This commit is contained in:
parent
155abe805b
commit
4fcc82bcb1
|
@ -26,7 +26,6 @@ import java.util.concurrent.atomic.AtomicLong;
|
||||||
/**
|
/**
|
||||||
* An implementation of the {@link ThreadFactory} interface that provides some
|
* An implementation of the {@link ThreadFactory} interface that provides some
|
||||||
* configuration options for the threads it creates.
|
* configuration options for the threads it creates.
|
||||||
*
|
|
||||||
* <p>
|
* <p>
|
||||||
* A {@link ThreadFactory} is used for instance by an {@link ExecutorService} to
|
* 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
|
* 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
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public class BasicThreadFactory implements ThreadFactory {
|
public class BasicThreadFactory implements ThreadFactory {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A <em>builder</em> class for creating instances of {@code
|
* A <em>builder</em> class for creating instances of {@code
|
||||||
* BasicThreadFactory}.
|
* BasicThreadFactory}.
|
||||||
*
|
|
||||||
* <p>
|
* <p>
|
||||||
* Using this builder class instances of {@link BasicThreadFactory} can be
|
* Using this builder class instances of {@link BasicThreadFactory} can be
|
||||||
* created and initialized. The class provides methods that correspond to
|
* 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> {
|
implements org.apache.commons.lang3.builder.Builder<BasicThreadFactory> {
|
||||||
|
|
||||||
/** The wrapped factory. */
|
/** The wrapped factory. */
|
||||||
private ThreadFactory wrappedFactory;
|
private ThreadFactory factory;
|
||||||
|
|
||||||
/** The uncaught exception handler. */
|
/** The uncaught exception handler. */
|
||||||
private Thread.UncaughtExceptionHandler exceptionHandler;
|
private Thread.UncaughtExceptionHandler exceptionHandler;
|
||||||
|
@ -150,14 +149,12 @@ public class BasicThreadFactory implements ThreadFactory {
|
||||||
* Sets the naming pattern to be used by the new {@code
|
* Sets the naming pattern to be used by the new {@code
|
||||||
* BasicThreadFactory}.
|
* 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}
|
* @return a reference to this {@link Builder}
|
||||||
* @throws NullPointerException if the naming pattern is <b>null</b>
|
* @throws NullPointerException if the naming pattern is <b>null</b>
|
||||||
*/
|
*/
|
||||||
public Builder namingPattern(final String pattern) {
|
public Builder namingPattern(final String namingPattern) {
|
||||||
Objects.requireNonNull(pattern, "pattern");
|
this.namingPattern = Objects.requireNonNull(namingPattern, "pattern");
|
||||||
|
|
||||||
namingPattern = pattern;
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,7 +177,7 @@ public class BasicThreadFactory implements ThreadFactory {
|
||||||
* automatically.
|
* automatically.
|
||||||
*/
|
*/
|
||||||
public void reset() {
|
public void reset() {
|
||||||
wrappedFactory = null;
|
factory = null;
|
||||||
exceptionHandler = null;
|
exceptionHandler = null;
|
||||||
namingPattern = null;
|
namingPattern = null;
|
||||||
priority = null;
|
priority = null;
|
||||||
|
@ -191,16 +188,14 @@ public class BasicThreadFactory implements ThreadFactory {
|
||||||
* Sets the uncaught exception handler for the threads created by the
|
* Sets the uncaught exception handler for the threads created by the
|
||||||
* new {@link BasicThreadFactory}.
|
* new {@link BasicThreadFactory}.
|
||||||
*
|
*
|
||||||
* @param handler the {@link UncaughtExceptionHandler} (must not be
|
* @param exceptionHandler the {@link UncaughtExceptionHandler} (must not be
|
||||||
* <b>null</b>)
|
* <b>null</b>)
|
||||||
* @return a reference to this {@link Builder}
|
* @return a reference to this {@link Builder}
|
||||||
* @throws NullPointerException if the exception handler is <b>null</b>
|
* @throws NullPointerException if the exception handler is <b>null</b>
|
||||||
*/
|
*/
|
||||||
public Builder uncaughtExceptionHandler(
|
public Builder uncaughtExceptionHandler(
|
||||||
final Thread.UncaughtExceptionHandler handler) {
|
final Thread.UncaughtExceptionHandler exceptionHandler) {
|
||||||
Objects.requireNonNull(handler, "handler");
|
this.exceptionHandler = Objects.requireNonNull(exceptionHandler, "handler");
|
||||||
|
|
||||||
exceptionHandler = handler;
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -215,9 +210,7 @@ public class BasicThreadFactory implements ThreadFactory {
|
||||||
* is <b>null</b>
|
* is <b>null</b>
|
||||||
*/
|
*/
|
||||||
public Builder wrappedFactory(final ThreadFactory factory) {
|
public Builder wrappedFactory(final ThreadFactory factory) {
|
||||||
Objects.requireNonNull(factory, "factory");
|
this.factory = Objects.requireNonNull(factory, "factory");
|
||||||
|
|
||||||
wrappedFactory = factory;
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -247,22 +240,16 @@ public class BasicThreadFactory implements ThreadFactory {
|
||||||
* @param builder the {@link Builder} object
|
* @param builder the {@link Builder} object
|
||||||
*/
|
*/
|
||||||
private BasicThreadFactory(final Builder builder) {
|
private BasicThreadFactory(final Builder builder) {
|
||||||
if (builder.wrappedFactory == null) {
|
wrappedFactory = builder.factory != null ? builder.factory : Executors.defaultThreadFactory();
|
||||||
wrappedFactory = Executors.defaultThreadFactory();
|
|
||||||
} else {
|
|
||||||
wrappedFactory = builder.wrappedFactory;
|
|
||||||
}
|
|
||||||
|
|
||||||
namingPattern = builder.namingPattern;
|
namingPattern = builder.namingPattern;
|
||||||
priority = builder.priority;
|
priority = builder.priority;
|
||||||
daemon = builder.daemon;
|
daemon = builder.daemon;
|
||||||
uncaughtExceptionHandler = builder.exceptionHandler;
|
uncaughtExceptionHandler = builder.exceptionHandler;
|
||||||
|
|
||||||
threadCounter = new AtomicLong();
|
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
|
* threads should be daemon threads. If <b>true</b>, this factory object
|
||||||
* calls {@code setDaemon(true)} on the newly created threads. Result can be
|
* calls {@code setDaemon(true)} on the newly created threads. Result can be
|
||||||
* <b>null</b> if no daemon flag was provided at creation time.
|
* <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.
|
* be <b>null</b> if no naming pattern was provided.
|
||||||
*
|
*
|
||||||
* @return the naming pattern
|
* @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.
|
* be <b>null</b> if no priority was specified.
|
||||||
*
|
*
|
||||||
* @return the priority for newly created threads
|
* @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
|
* class maintains an internal counter that is incremented each time the
|
||||||
* {@link #newThread(Runnable)} method is invoked.
|
* {@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.
|
* this factory. Result can be <b>null</b> if no handler was provided.
|
||||||
*
|
*
|
||||||
* @return the {@link UncaughtExceptionHandler}
|
* @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
|
* actually creating threads. This method never returns <b>null</b>. If no
|
||||||
* {@link ThreadFactory} was passed when this object was created, a default
|
* {@link ThreadFactory} was passed when this object was created, a default
|
||||||
* thread factory is returned.
|
* thread factory is returned.
|
||||||
|
@ -335,20 +322,16 @@ public class BasicThreadFactory implements ThreadFactory {
|
||||||
* @param thread the thread to be initialized
|
* @param thread the thread to be initialized
|
||||||
*/
|
*/
|
||||||
private void initializeThread(final Thread thread) {
|
private void initializeThread(final Thread thread) {
|
||||||
|
|
||||||
if (getNamingPattern() != null) {
|
if (getNamingPattern() != null) {
|
||||||
final Long count = Long.valueOf(threadCounter.incrementAndGet());
|
final Long count = Long.valueOf(threadCounter.incrementAndGet());
|
||||||
thread.setName(String.format(getNamingPattern(), count));
|
thread.setName(String.format(getNamingPattern(), count));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getUncaughtExceptionHandler() != null) {
|
if (getUncaughtExceptionHandler() != null) {
|
||||||
thread.setUncaughtExceptionHandler(getUncaughtExceptionHandler());
|
thread.setUncaughtExceptionHandler(getUncaughtExceptionHandler());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getPriority() != null) {
|
if (getPriority() != null) {
|
||||||
thread.setPriority(getPriority().intValue());
|
thread.setPriority(getPriority().intValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getDaemonFlag() != null) {
|
if (getDaemonFlag() != null) {
|
||||||
thread.setDaemon(getDaemonFlag().booleanValue());
|
thread.setDaemon(getDaemonFlag().booleanValue());
|
||||||
}
|
}
|
||||||
|
@ -366,7 +349,6 @@ public class BasicThreadFactory implements ThreadFactory {
|
||||||
public Thread newThread(final Runnable runnable) {
|
public Thread newThread(final Runnable runnable) {
|
||||||
final Thread thread = getWrappedFactory().newThread(runnable);
|
final Thread thread = getWrappedFactory().newThread(runnable);
|
||||||
initializeThread(thread);
|
initializeThread(thread);
|
||||||
|
|
||||||
return thread;
|
return thread;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue