Skip to content

Commit

Permalink
chore: use PECS for Lazy factories
Browse files Browse the repository at this point in the history
  • Loading branch information
JarvisCraft committed Aug 15, 2021
1 parent 4b2c548 commit 809c7d6
Showing 1 changed file with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public interface Lazy<T> extends Supplier<T> {
*
* @apiNote might be thread-unsafe
*/
static <T> Lazy<T> create(final @NonNull Supplier<T> valueSupplier) {
static <T> Lazy<T> create(final @NonNull Supplier<? extends T> valueSupplier) {
return new SimpleLazy<>(valueSupplier);
}

Expand All @@ -94,7 +94,7 @@ static <T> Lazy<T> create(final @NonNull Supplier<T> valueSupplier) {
* @param <T> type of value wrapped
* @return created lazy
*/
static <T> Lazy<T> createThreadSafe(final @NonNull Supplier<T> valueSupplier) {
static <T> Lazy<T> createThreadSafe(final @NonNull Supplier<? extends T> valueSupplier) {
//noinspection ZeroLengthArrayAllocation: mutex object
return new DoubleCheckedLazy<>(new Object[0], valueSupplier);
}
Expand All @@ -110,7 +110,7 @@ static <T> Lazy<T> createThreadSafe(final @NonNull Supplier<T> valueSupplier) {
* @apiNote weak lazy stores the value wrapped in weak reference and so it may be GCed
* and so the new one might be recomputed using the value supplier
*/
static <T> Lazy<@NotNull T> createWeak(final @NonNull Supplier<@NotNull T> valueSupplier) {
static <T> Lazy<@NotNull T> createWeak(final @NonNull Supplier<@NotNull ? extends T> valueSupplier) {
return new SimpleWeakLazy<>(valueSupplier, ReferenceUtil.weakReferenceToNull());
}

Expand All @@ -124,7 +124,7 @@ static <T> Lazy<T> createThreadSafe(final @NonNull Supplier<T> valueSupplier) {
* @apiNote weak lazy stores the value wrapped in weak reference and so it may be GCed
* and so the new one might be recomputed using the value supplier
*/
static <T> Lazy<T> createWeakThreadSafe(final @NonNull Supplier<T> valueSupplier) {
static <T> Lazy<T> createWeakThreadSafe(final @NonNull Supplier<? extends T> valueSupplier) {
final ReadWriteLock lock;
return new LockingWeakLazy<>(
(lock = new ReentrantReadWriteLock()).readLock(), lock.writeLock(),
Expand All @@ -139,7 +139,7 @@ static <T> Lazy<T> createWeakThreadSafe(final @NonNull Supplier<T> valueSupplier
* @param <T> type of value wrapped
* @return created lazy
*/
static <T> Lazy<T> createThreadLocal(final @NonNull Supplier<T> valueSupplier) {
static <T> Lazy<T> createThreadLocal(final @NonNull Supplier<? extends T> valueSupplier) {
return new ThreadLocalLazy<>(valueSupplier, ThreadLocal.withInitial(() -> ThreadLocalLazy.UNSET_VALUE));
}

Expand All @@ -155,14 +155,14 @@ final class SimpleLazy<T> implements Lazy<T> {
/**
* Supplier used for creation of the value
*/
@Nullable Supplier<@Nullable T> valueSupplier;
@Nullable Supplier<? extends T> valueSupplier;

/**
* The value stored
*/
T value;

private SimpleLazy(final @NotNull Supplier<T> valueSupplier) {
private SimpleLazy(final @NotNull Supplier<? extends T> valueSupplier) {
this.valueSupplier = valueSupplier;
}

Expand Down Expand Up @@ -209,7 +209,7 @@ final class DoubleCheckedLazy<T> implements Lazy<T> {
/**
* Supplier used for creation of the value
*/
@Nullable volatile Supplier<T> valueSupplier;
@Nullable volatile Supplier<? extends T> valueSupplier;

/**
* The value stored
Expand All @@ -222,15 +222,15 @@ final class DoubleCheckedLazy<T> implements Lazy<T> {
* @param mutex mutex to be used for synchronization
* @param valueSupplier supplier used for creation of the value
*/
private DoubleCheckedLazy(final @NotNull Object mutex, final @NotNull Supplier<T> valueSupplier) {
private DoubleCheckedLazy(final @NotNull Object mutex, final @NotNull Supplier<? extends T> valueSupplier) {
this.mutex = mutex;
this.valueSupplier = valueSupplier;
}

@Override
public T get() {
if (valueSupplier != null) synchronized (mutex) {
final Supplier<T> valueSupplier;
final Supplier<? extends T> valueSupplier;
if ((valueSupplier = this.valueSupplier) != null) {
val value = this.value = valueSupplier.get();
this.valueSupplier = null;
Expand Down Expand Up @@ -283,7 +283,7 @@ final class SimpleWeakLazy<@NotNull T> implements Lazy<T> {
/**
* Supplier used for creation of the value
*/
final @NotNull Supplier<@NotNull T> valueSupplier;
final @NotNull Supplier<@NotNull ? extends T> valueSupplier;

/**
* The value stored wrapped in {@link WeakReference}
Expand Down Expand Up @@ -339,7 +339,7 @@ final class LockingWeakLazy<T> implements Lazy<@NotNull T> {
/**
* Supplier used for creation of the value
*/
@NotNull Supplier<T> valueSupplier;
@NotNull Supplier<? extends T> valueSupplier;

/**
* The value stored wrapped in {@link WeakReference}
Expand Down Expand Up @@ -417,7 +417,7 @@ class ThreadLocalLazy<T> implements Lazy<T> {
/**
* Supplier used for creation of the value
*/
@NotNull Supplier<T> valueSupplier;
@NotNull Supplier<? extends T> valueSupplier;

/**
* The value stored wrapped in {@link ThreadLocal}
Expand Down

0 comments on commit 809c7d6

Please sign in to comment.