Reduce compiler warnings

This commit is contained in:
Gary Gregory 2023-10-15 10:04:34 -04:00
parent 3ed3ec85a3
commit 4d63091bdc
2 changed files with 17 additions and 5 deletions

View File

@ -67,7 +67,7 @@ public abstract class AtomicInitializer<T> extends AbstractConcurrentInitializer
private static final Object NO_INIT = new Object();
/** Holds the reference to the managed object. */
private final AtomicReference<T> reference = new AtomicReference<>((T) NO_INIT);
private final AtomicReference<T> reference = new AtomicReference<>(getNoInit());
/**
* Returns the object managed by this initializer. The object is created if
@ -82,9 +82,9 @@ public abstract class AtomicInitializer<T> 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<T> 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.
*

View File

@ -59,7 +59,7 @@ public abstract class AtomicSafeInitializer<T> extends AbstractConcurrentInitial
private final AtomicReference<AtomicSafeInitializer<T>> factory = new AtomicReference<>();
/** Holds the reference to the managed object. */
private final AtomicReference<T> reference = new AtomicReference<>((T) NO_INIT);
private final AtomicReference<T> reference = new AtomicReference<>(getNoInit());
/**
* Gets (and initialize, if not initialized yet) the required object
@ -72,7 +72,7 @@ public abstract class AtomicSafeInitializer<T> 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<T> 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.
*