Skip to content

Commit 56766bc

Browse files
committed
Rename CachedSupplier to ThreadSafeCachingSupplier and refactor
1 parent a912062 commit 56766bc

File tree

6 files changed

+23
-22
lines changed

6 files changed

+23
-22
lines changed

src/main/java/com/networknt/schema/keyword/DynamicRefValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import com.networknt.schema.Schema;
2525
import com.networknt.schema.SchemaException;
2626
import com.networknt.schema.SchemaRef;
27-
import com.networknt.schema.utils.CachedSupplier;
27+
import com.networknt.schema.utils.ThreadSafeCachingSupplier;
2828
import com.networknt.schema.SchemaLocation;
2929
import com.networknt.schema.SchemaContext;
3030

@@ -83,7 +83,7 @@ static SchemaRef getRefSchema(Schema parentSchema, SchemaContext schemaContext,
8383
}
8484

8585
static <T> Supplier<T> getSupplier(Supplier<T> supplier, boolean cache) {
86-
return cache ? new CachedSupplier<>(supplier) : supplier;
86+
return cache ? new ThreadSafeCachingSupplier<>(supplier) : supplier;
8787
}
8888

8989
private static String resolve(Schema parentSchema, String refValue) {

src/main/java/com/networknt/schema/keyword/RecursiveRefValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import com.networknt.schema.Schema;
2525
import com.networknt.schema.SchemaException;
2626
import com.networknt.schema.SchemaRef;
27-
import com.networknt.schema.utils.CachedSupplier;
27+
import com.networknt.schema.utils.ThreadSafeCachingSupplier;
2828
import com.networknt.schema.SchemaLocation;
2929
import com.networknt.schema.SchemaContext;
3030

@@ -57,7 +57,7 @@ static SchemaRef getRefSchema(Schema parentSchema, SchemaContext schemaContext,
5757
}
5858

5959
static <T> Supplier<T> getSupplier(Supplier<T> supplier, boolean cache) {
60-
return cache ? new CachedSupplier<>(supplier) : supplier;
60+
return cache ? new ThreadSafeCachingSupplier<>(supplier) : supplier;
6161
}
6262

6363
static Schema getSchema(Schema parentSchema, SchemaContext schemaContext, String refValue,

src/main/java/com/networknt/schema/keyword/RefValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import com.networknt.schema.Schema;
2525
import com.networknt.schema.SchemaException;
2626
import com.networknt.schema.SchemaRef;
27-
import com.networknt.schema.utils.CachedSupplier;
27+
import com.networknt.schema.utils.ThreadSafeCachingSupplier;
2828
import com.networknt.schema.SchemaLocation;
2929
import com.networknt.schema.SchemaContext;
3030

@@ -126,7 +126,7 @@ static SchemaRef getRefSchema(Schema parentSchema, SchemaContext schemaContext,
126126
}
127127

128128
static <T> Supplier<T> getSupplier(Supplier<T> supplier, boolean cache) {
129-
return cache ? new CachedSupplier<>(supplier) : supplier;
129+
return cache ? new ThreadSafeCachingSupplier<>(supplier) : supplier;
130130
}
131131

132132
private static void copySchemaResources(SchemaContext schemaContext, Schema schemaResource) {

src/main/java/com/networknt/schema/utils/CachingSupplier.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,23 @@
2222
* {@link Supplier} that caches the value.
2323
* <p>
2424
* This is not threadsafe.
25-
*
26-
* @param <T> the type of results supplied by this supplier
25+
*
26+
* @param <T> the type of results cached by this supplier
2727
*/
2828
public class CachingSupplier<T> implements Supplier<T> {
29-
private final Supplier<T> delegate;
30-
private T cached = null;
29+
private Supplier<T> delegate;
30+
private T cache = null;
3131

32-
public CachingSupplier(Supplier<T> supplier) {
33-
this.delegate = supplier;
32+
public CachingSupplier(Supplier<T> delegate) {
33+
this.delegate = delegate;
3434
}
3535

3636
@Override
3737
public T get() {
38-
if (this.cached == null) {
39-
this.cached = this.delegate.get();
38+
if (this.cache == null && this.delegate != null) {
39+
this.cache = this.delegate.get();
40+
this.delegate = null;
4041
}
41-
return this.cached;
42+
return this.cache;
4243
}
4344
}

src/main/java/com/networknt/schema/utils/CachedSupplier.java renamed to src/main/java/com/networknt/schema/utils/ThreadSafeCachingSupplier.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
import java.util.function.Supplier;
1919

2020
/**
21-
* Supplier that caches the output.
21+
* {@link Supplier} that caches the value.
2222
*
23-
* @param <T> the type cached
23+
* @param <T> the type of results cached by this supplier
2424
*/
25-
public class CachedSupplier<T> implements Supplier<T> {
25+
public class ThreadSafeCachingSupplier<T> implements Supplier<T> {
2626
private volatile Supplier<T> delegate;
2727
private volatile T cache = null;
2828

29-
public CachedSupplier(Supplier<T> delegate) {
29+
public ThreadSafeCachingSupplier(Supplier<T> delegate) {
3030
this.delegate = delegate;
3131
}
3232

src/test/java/com/networknt/schema/utils/CachedSupplierTest.java renamed to src/test/java/com/networknt/schema/utils/ThreadSafeCachingSupplierTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@
2828
/**
2929
* Test for CachedSupplier.
3030
*/
31-
class CachedSupplierTest {
31+
class ThreadSafeCachingSupplierTest {
3232
@Test
3333
void nullValue() {
34-
CachedSupplier<String> supplier = new CachedSupplier<>(null);
34+
ThreadSafeCachingSupplier<String> supplier = new ThreadSafeCachingSupplier<>(null);
3535
assertNull(supplier.get());
3636
}
3737

3838
@Test
3939
void concurrency() throws Exception {
4040
AtomicInteger value = new AtomicInteger(0);
4141

42-
CachedSupplier<Integer> supplier = new CachedSupplier<>(() -> {
42+
ThreadSafeCachingSupplier<Integer> supplier = new ThreadSafeCachingSupplier<>(() -> {
4343
return value.addAndGet(1);
4444
});
4545
Exception[] instance = new Exception[1];

0 commit comments

Comments
 (0)