diff --git a/src/main/java/org/apache/commons/lang3/concurrent/AtomicInitializer.java b/src/main/java/org/apache/commons/lang3/concurrent/AtomicInitializer.java index 7d72ad5e2..42706f79c 100644 --- a/src/main/java/org/apache/commons/lang3/concurrent/AtomicInitializer.java +++ b/src/main/java/org/apache/commons/lang3/concurrent/AtomicInitializer.java @@ -67,7 +67,7 @@ public abstract class AtomicInitializer extends AbstractConcurrentInitializer private static final Object NO_INIT = new Object(); /** Holds the reference to the managed object. */ - private final AtomicReference reference = new AtomicReference<>((T) NO_INIT); + private final AtomicReference reference = new AtomicReference<>(getNoInit()); /** * Returns the object managed by this initializer. The object is created if @@ -82,9 +82,9 @@ public abstract class AtomicInitializer extends AbstractConcurrentInitializer public T get() throws ConcurrentException { T result = reference.get(); - if (result == (T) NO_INIT) { + if (result == getNoInit()) { result = initialize(); - if (!reference.compareAndSet((T) NO_INIT, result)) { + if (!reference.compareAndSet(getNoInit(), result)) { // another thread has initialized the reference result = reference.get(); } @@ -93,6 +93,12 @@ public abstract class AtomicInitializer extends AbstractConcurrentInitializer return result; } + /** Gets the internal no-init object cast for this instance. */ + @SuppressWarnings("unchecked") + private T getNoInit() { + return (T) NO_INIT; + } + /** * Tests whether this instance is initialized. Once initialized, always returns true. * diff --git a/src/main/java/org/apache/commons/lang3/concurrent/AtomicSafeInitializer.java b/src/main/java/org/apache/commons/lang3/concurrent/AtomicSafeInitializer.java index 29195797c..5e2911b50 100644 --- a/src/main/java/org/apache/commons/lang3/concurrent/AtomicSafeInitializer.java +++ b/src/main/java/org/apache/commons/lang3/concurrent/AtomicSafeInitializer.java @@ -59,7 +59,7 @@ public abstract class AtomicSafeInitializer extends AbstractConcurrentInitial private final AtomicReference> factory = new AtomicReference<>(); /** Holds the reference to the managed object. */ - private final AtomicReference reference = new AtomicReference<>((T) NO_INIT); + private final AtomicReference reference = new AtomicReference<>(getNoInit()); /** * Gets (and initialize, if not initialized yet) the required object @@ -72,7 +72,7 @@ public abstract class AtomicSafeInitializer extends AbstractConcurrentInitial public final T get() throws ConcurrentException { T result; - while ((result = reference.get()) == (T) NO_INIT) { + while ((result = reference.get()) == getNoInit()) { if (factory.compareAndSet(null, this)) { reference.set(initialize()); } @@ -81,6 +81,12 @@ public abstract class AtomicSafeInitializer extends AbstractConcurrentInitial return result; } + /** Gets the internal no-init object cast for this instance. */ + @SuppressWarnings("unchecked") + private T getNoInit() { + return (T) NO_INIT; + } + /** * Tests whether this instance is initialized. Once initialized, always returns true. *