Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions core/src/main/java/net/staticstudios/data/CachedValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ default CachedValue<T> withFallback(T fallback) {

CachedValue<T> supplyFallback(Supplier<T> fallback);

CachedValue<T> refresh(Supplier<T> refresh);

class ProxyCachedValue<T> implements CachedValue<T> {
protected final UniqueData holder;
protected final Class<T> dataType;
private final List<ValueUpdateHandlerWrapper<?, T>> updateHandlers = new ArrayList<>();
private @Nullable CachedValue<T> delegate;
private Supplier<T> fallback = () -> null;
private @Nullable Supplier<T> refresh = null;

public ProxyCachedValue(UniqueData holder, Class<T> dataType) {
this.holder = holder;
Expand All @@ -48,6 +51,7 @@ public void setDelegate(CachedValueMetadata metadata, AbstractCachedValue<T> del
Preconditions.checkNotNull(delegate, "Delegate cannot be null");
Preconditions.checkState(this.delegate == null, "Delegate is already set");
delegate.setFallback(this.fallback);
delegate.setRefresh(this.refresh);
this.delegate = delegate;

//since an update handler can be registered before the fallback is set, we need to convert them here
Expand Down Expand Up @@ -88,6 +92,16 @@ public CachedValue<T> supplyFallback(Supplier<T> fallback) {
return this;
}

@Override
public CachedValue<T> refresh(Supplier<T> refresh) {
if (delegate != null) {
throw new UnsupportedOperationException("Cannot set fallback after initialization");
}
Preconditions.checkNotNull(refresh, "Refresh supplier cannot be null");
LambdaUtils.assertLambdaDoesntCapture(refresh, List.of(UniqueData.class), null);
return delegate.refresh(refresh);
}

@Override
public T get() {
if (delegate != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package net.staticstudios.data.impl.data;

import com.google.common.base.Preconditions;
import net.staticstudios.data.CachedValue;

import java.util.function.Supplier;

public abstract class AbstractCachedValue<T> implements CachedValue<T> {
private Supplier<T> fallbackSupplier;
private Supplier<T> refreshSupplier;

public void setFallback(Supplier<T> fallbackSupplier) {
this.fallbackSupplier = fallbackSupplier;
Expand All @@ -17,4 +19,17 @@ protected T getFallback() {
}
return null;
}

public void setRefresh(Supplier<T> refreshSupplier) {
this.refreshSupplier = refreshSupplier;
}

protected T refresh() {
if (refreshSupplier == null) {
return null;
}
T value = refreshSupplier.get();
set(value);
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,18 @@ public CachedValue<T> supplyFallback(Supplier<T> fallback) {
throw new UnsupportedOperationException("Cannot set fallback after initialization");
}

@Override
public CachedValue<T> refresh(Supplier<T> refresh) {
throw new UnsupportedOperationException("Cannot set refresh after initialization");
}

@Override
public T get() {
Preconditions.checkArgument(!holder.isDeleted(), "Cannot get value from a deleted UniqueData instance");
T value = holder.getDataManager().getRedis(metadata.holderSchema(), metadata.holderTable(), metadata.identifier(), holder.getIdColumns(), dataType);
if (value == null) {
return getFallback();
value = refresh();
value = value == null ? getFallback() : value;
}
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public CachedValue<T> supplyFallback(Supplier<T> fallback) {
throw new UnsupportedOperationException("Cannot set fallback on a read-only CachedValue");
}

@Override
public CachedValue<T> refresh(Supplier<T> refresh) {
throw new UnsupportedOperationException("Cannot refresh a read-only CachedValue");
}

@Override
public T get() {
return value;
Expand Down
Loading