Skip to content

Commit 2605ade

Browse files
committed
Improved the generics
1 parent 9ed9c64 commit 2605ade

File tree

5 files changed

+48
-39
lines changed

5 files changed

+48
-39
lines changed

src/main/java/io/engagingspaces/vertx/dataloader/CacheMap.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.engagingspaces.vertx.dataloader;
1818

1919
import io.engagingspaces.vertx.dataloader.impl.DefaultCacheMap;
20+
import io.vertx.core.Future;
2021

2122
/**
2223
* Cache map interface for data loaders that use caching.
@@ -41,7 +42,7 @@ public interface CacheMap<U, V> {
4142
* @param <V> type parameter indicating the type of the data that is cached
4243
* @return the cache map
4344
*/
44-
static <U, V> CacheMap<U, V> simpleMap() {
45+
static <U, V> CacheMap<U, Future<V>> simpleMap() {
4546
return new DefaultCacheMap<>();
4647
}
4748

@@ -71,20 +72,20 @@ static <U, V> CacheMap<U, V> simpleMap() {
7172
* @param value the value to cache
7273
* @return the cache map for fluent coding
7374
*/
74-
CacheMap set(U key, V value);
75+
CacheMap<U, V> set(U key, V value);
7576

7677
/**
7778
* Deletes the entry with the specified key from the cache map, if it exists.
7879
*
7980
* @param key the key to delete
8081
* @return the cache map for fluent coding
8182
*/
82-
CacheMap delete(U key);
83+
CacheMap<U, V> delete(U key);
8384

8485
/**
8586
* Clears all entries of the cache map
8687
*
8788
* @return the cache map for fluent coding
8889
*/
89-
CacheMap clear();
90+
CacheMap<U, V> clear();
9091
}

src/main/java/io/engagingspaces/vertx/dataloader/DataLoader.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
public class DataLoader<K, V> {
5050

5151
private final BatchLoader<K> batchLoadFunction;
52-
private final DataLoaderOptions loaderOptions;
52+
private final DataLoaderOptions<K, V> loaderOptions;
5353
private final CacheMap<Object, Future<V>> futureCache;
5454
private final LinkedHashMap<K, Future<V>> loaderQueue;
5555

@@ -68,11 +68,12 @@ public DataLoader(BatchLoader<K> batchLoadFunction) {
6868
* @param batchLoadFunction the batch load function to use
6969
* @param options the batch load options
7070
*/
71-
public DataLoader(BatchLoader<K> batchLoadFunction, DataLoaderOptions options) {
71+
@SuppressWarnings("unchecked")
72+
public DataLoader(BatchLoader<K> batchLoadFunction, DataLoaderOptions<K, V> options) {
7273
Objects.requireNonNull(batchLoadFunction, "Batch load function cannot be null");
7374
this.batchLoadFunction = batchLoadFunction;
74-
this.loaderOptions = options == null ? new DataLoaderOptions() : options;
75-
this.futureCache = loaderOptions.cacheMap().isPresent() ? loaderOptions.cacheMap().get() : CacheMap.simpleMap();
75+
this.loaderOptions = options == null ? new DataLoaderOptions<>() : options;
76+
this.futureCache = loaderOptions.cacheMap().isPresent() ? (CacheMap<Object, Future<V>>) loaderOptions.cacheMap().get() : CacheMap.simpleMap();
7677
this.loaderQueue = new LinkedHashMap<>();
7778
}
7879

src/main/java/io/engagingspaces/vertx/dataloader/DataLoaderOptions.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.engagingspaces.vertx.dataloader;
1818

19+
import io.vertx.core.Future;
1920
import io.vertx.core.json.JsonObject;
2021

2122
import java.util.Objects;
@@ -26,12 +27,12 @@
2627
*
2728
* @author <a href="https://github.com/aschrijver/">Arnold Schrijver</a>
2829
*/
29-
public class DataLoaderOptions {
30+
public class DataLoaderOptions<K, V> {
3031

3132
private boolean batchingEnabled;
3233
private boolean cachingEnabled;
3334
private CacheKey cacheKeyFunction;
34-
private CacheMap cacheMap;
35+
private CacheMap<K, Future<V>> cacheMap;
3536

3637
/**
3738
* Creates a new data loader options with default settings.
@@ -41,12 +42,16 @@ public DataLoaderOptions() {
4142
cachingEnabled = true;
4243
}
4344

45+
public static <K, V> DataLoaderOptions<K, V> create() {
46+
return new DataLoaderOptions<>();
47+
}
48+
4449
/**
4550
* Clones the provided data loader options.
4651
*
4752
* @param other the other options instance
4853
*/
49-
public DataLoaderOptions(DataLoaderOptions other) {
54+
public DataLoaderOptions(DataLoaderOptions<K, V> other) {
5055
Objects.requireNonNull(other, "Other data loader options cannot be null");
5156
this.batchingEnabled = other.batchingEnabled;
5257
this.cachingEnabled = other.cachingEnabled;
@@ -139,7 +144,7 @@ public DataLoaderOptions setCacheKeyFunction(CacheKey cacheKeyFunction) {
139144
*
140145
* @return an optional with the cache map instance, or empty
141146
*/
142-
public Optional<CacheMap> cacheMap() {
147+
public Optional<CacheMap<K, Future<V>>> cacheMap() {
143148
return Optional.ofNullable(cacheMap);
144149
}
145150

@@ -149,7 +154,7 @@ public Optional<CacheMap> cacheMap() {
149154
* @param cacheMap the cache map instance
150155
* @return the data loader options for fluent coding
151156
*/
152-
public DataLoaderOptions setCacheMap(CacheMap cacheMap) {
157+
public DataLoaderOptions setCacheMap(CacheMap<K, Future<V>> cacheMap) {
153158
this.cacheMap = cacheMap;
154159
return this;
155160
}

src/main/java/io/engagingspaces/vertx/dataloader/impl/DefaultCacheMap.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@
2222
import java.util.Map;
2323

2424
/**
25+
* Default implementation of {@link CacheMap} that is based on a regular {@link java.util.LinkedHashMap}.
2526
*
26-
* @param <K>
27-
* @param <V>
27+
* @param <U> type parameter indicating the type of the cache keys
28+
* @param <V> type parameter indicating the type of the data that is cached
2829
*
2930
* @author <a href="https://github.com/aschrijver/">Arnold Schrijver</a>
3031
*/
31-
public class DefaultCacheMap<K, V> implements CacheMap<K, V> {
32+
public class DefaultCacheMap<U, V> implements CacheMap<U, V> {
3233

33-
private Map<K, V> cache;
34+
private Map<U, V> cache;
3435

3536
/**
3637
* Default constructor
@@ -43,23 +44,23 @@ public DefaultCacheMap() {
4344
* {@inheritDoc}
4445
*/
4546
@Override
46-
public boolean containsKey(K key) {
47+
public boolean containsKey(U key) {
4748
return cache.containsKey(key);
4849
}
4950

5051
/**
5152
* {@inheritDoc}
5253
*/
5354
@Override
54-
public V get(K key) {
55+
public V get(U key) {
5556
return cache.get(key);
5657
}
5758

5859
/**
5960
* {@inheritDoc}
6061
*/
6162
@Override
62-
public CacheMap set(K key, V value) {
63+
public CacheMap<U, V> set(U key, V value) {
6364
cache.put(key, value);
6465
return this;
6566
}
@@ -68,7 +69,7 @@ public CacheMap set(K key, V value) {
6869
* {@inheritDoc}
6970
*/
7071
@Override
71-
public CacheMap delete(K key) {
72+
public CacheMap<U, V> delete(U key) {
7273
cache.remove(key);
7374
return this;
7475
}
@@ -77,7 +78,7 @@ public CacheMap delete(K key) {
7778
* {@inheritDoc}
7879
*/
7980
@Override
80-
public CacheMap clear() {
81+
public CacheMap<U, V> clear() {
8182
cache.clear();
8283
return this;
8384
}

src/test/java/io/engagingspaces/vertx/dataloader/DataLoaderTest.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class DataLoaderTest {
5454

5555
@Before
5656
public void setUp() {
57-
identityLoader = idLoader(new DataLoaderOptions(), new ArrayList<>());
57+
identityLoader = idLoader(new DataLoaderOptions<>(), new ArrayList<>());
5858
}
5959

6060
@Test
@@ -108,7 +108,7 @@ public void should_Resolve_to_empty_list_when_no_keys_supplied() {
108108
@Test
109109
public void should_Batch_multiple_requests() {
110110
ArrayList<Collection> loadCalls = new ArrayList<>();
111-
DataLoader<Integer, Integer> identityLoader = idLoader(new DataLoaderOptions(), loadCalls);
111+
DataLoader<Integer, Integer> identityLoader = idLoader(new DataLoaderOptions<>(), loadCalls);
112112

113113
Future<Integer> future1 = identityLoader.load(1);
114114
Future<Integer> future2 = identityLoader.load(2);
@@ -123,7 +123,7 @@ public void should_Batch_multiple_requests() {
123123
@Test
124124
public void should_Coalesce_identical_requests() {
125125
ArrayList<Collection> loadCalls = new ArrayList<>();
126-
DataLoader<Integer, Integer> identityLoader = idLoader(new DataLoaderOptions(), loadCalls);
126+
DataLoader<Integer, Integer> identityLoader = idLoader(new DataLoaderOptions<>(), loadCalls);
127127

128128
Future<Integer> future1a = identityLoader.load(1);
129129
Future<Integer> future1b = identityLoader.load(1);
@@ -139,7 +139,7 @@ public void should_Coalesce_identical_requests() {
139139
@Test
140140
public void should_Cache_repeated_requests() {
141141
ArrayList<Collection> loadCalls = new ArrayList<>();
142-
DataLoader<String, String> identityLoader = idLoader(new DataLoaderOptions(), loadCalls);
142+
DataLoader<String, String> identityLoader = idLoader(new DataLoaderOptions<>(), loadCalls);
143143

144144
Future<String> future1 = identityLoader.load("A");
145145
Future<String> future2 = identityLoader.load("B");
@@ -174,7 +174,7 @@ public void should_Cache_repeated_requests() {
174174
@Test
175175
public void should_Clear_single_value_in_loader() {
176176
ArrayList<Collection> loadCalls = new ArrayList<>();
177-
DataLoader<String, String> identityLoader = idLoader(new DataLoaderOptions(), loadCalls);
177+
DataLoader<String, String> identityLoader = idLoader(new DataLoaderOptions<>(), loadCalls);
178178

179179
Future<String> future1 = identityLoader.load("A");
180180
Future<String> future2 = identityLoader.load("B");
@@ -200,7 +200,7 @@ public void should_Clear_single_value_in_loader() {
200200
@Test
201201
public void should_Clear_all_values_in_loader() {
202202
ArrayList<Collection> loadCalls = new ArrayList<>();
203-
DataLoader<String, String> identityLoader = idLoader(new DataLoaderOptions(), loadCalls);
203+
DataLoader<String, String> identityLoader = idLoader(new DataLoaderOptions<>(), loadCalls);
204204

205205
Future<String> future1 = identityLoader.load("A");
206206
Future<String> future2 = identityLoader.load("B");
@@ -226,7 +226,7 @@ public void should_Clear_all_values_in_loader() {
226226
@Test
227227
public void should_Allow_priming_the_cache() {
228228
ArrayList<Collection> loadCalls = new ArrayList<>();
229-
DataLoader<String, String> identityLoader = idLoader(new DataLoaderOptions(), loadCalls);
229+
DataLoader<String, String> identityLoader = idLoader(new DataLoaderOptions<>(), loadCalls);
230230

231231
identityLoader.prime("A", "A");
232232

@@ -243,7 +243,7 @@ public void should_Allow_priming_the_cache() {
243243
@Test
244244
public void should_Not_prime_keys_that_already_exist() {
245245
ArrayList<Collection> loadCalls = new ArrayList<>();
246-
DataLoader<String, String> identityLoader = idLoader(new DataLoaderOptions(), loadCalls);
246+
DataLoader<String, String> identityLoader = idLoader(new DataLoaderOptions<>(), loadCalls);
247247

248248
identityLoader.prime("A", "X");
249249

@@ -271,7 +271,7 @@ public void should_Not_prime_keys_that_already_exist() {
271271
@Test
272272
public void should_Allow_to_forcefully_prime_the_cache() {
273273
ArrayList<Collection> loadCalls = new ArrayList<>();
274-
DataLoader<String, String> identityLoader = idLoader(new DataLoaderOptions(), loadCalls);
274+
DataLoader<String, String> identityLoader = idLoader(new DataLoaderOptions<>(), loadCalls);
275275

276276
identityLoader.prime("A", "X");
277277

@@ -299,7 +299,7 @@ public void should_Allow_to_forcefully_prime_the_cache() {
299299
@Test
300300
public void should_Resolve_to_error_to_indicate_failure() {
301301
ArrayList<Collection> loadCalls = new ArrayList<>();
302-
DataLoader<Integer, Integer> evenLoader = idLoaderWithErrors(new DataLoaderOptions(), loadCalls);
302+
DataLoader<Integer, Integer> evenLoader = idLoaderWithErrors(new DataLoaderOptions<>(), loadCalls);
303303

304304
Future<Integer> future1 = evenLoader.load(1);
305305
evenLoader.dispatch();
@@ -319,7 +319,7 @@ public void should_Resolve_to_error_to_indicate_failure() {
319319
@Test
320320
public void should_Represent_failures_and_successes_simultaneously() {
321321
ArrayList<Collection> loadCalls = new ArrayList<>();
322-
DataLoader<Integer, Integer> evenLoader = idLoaderWithErrors(new DataLoaderOptions(), loadCalls);
322+
DataLoader<Integer, Integer> evenLoader = idLoaderWithErrors(new DataLoaderOptions<>(), loadCalls);
323323

324324
Future<Integer> future1 = evenLoader.load(1);
325325
Future<Integer> future2 = evenLoader.load(2);
@@ -337,7 +337,7 @@ public void should_Represent_failures_and_successes_simultaneously() {
337337
@Test
338338
public void should_Cache_failed_fetches() {
339339
ArrayList<Collection> loadCalls = new ArrayList<>();
340-
DataLoader<Integer, Integer> errorLoader = idLoaderAllErrors(new DataLoaderOptions(), loadCalls);
340+
DataLoader<Integer, Integer> errorLoader = idLoaderAllErrors(new DataLoaderOptions<>(), loadCalls);
341341

342342
Future<Integer> future1 = errorLoader.load(1);
343343
errorLoader.dispatch();
@@ -358,7 +358,7 @@ public void should_Cache_failed_fetches() {
358358
@Test
359359
public void should_Handle_priming_the_cache_with_an_error() {
360360
ArrayList<Collection> loadCalls = new ArrayList<>();
361-
DataLoader<Integer, Integer> identityLoader = idLoader(new DataLoaderOptions(), loadCalls);
361+
DataLoader<Integer, Integer> identityLoader = idLoader(new DataLoaderOptions<>(), loadCalls);
362362

363363
identityLoader.prime(1, new IllegalStateException("Error"));
364364

@@ -374,7 +374,7 @@ public void should_Handle_priming_the_cache_with_an_error() {
374374
@Test
375375
public void should_Clear_values_from_cache_after_errors() {
376376
ArrayList<Collection> loadCalls = new ArrayList<>();
377-
DataLoader<Integer, Integer> errorLoader = idLoaderAllErrors(new DataLoaderOptions(), loadCalls);
377+
DataLoader<Integer, Integer> errorLoader = idLoaderAllErrors(new DataLoaderOptions<>(), loadCalls);
378378

379379
Future<Integer> future1 = errorLoader.load(1);
380380
future1.setHandler(rh -> {
@@ -405,7 +405,7 @@ public void should_Clear_values_from_cache_after_errors() {
405405
}
406406

407407
@SuppressWarnings("unchecked")
408-
private static <K, V> DataLoader<K, V> idLoader(DataLoaderOptions options, List<Collection> loadCalls) {
408+
private static <K, V> DataLoader<K, V> idLoader(DataLoaderOptions<K, V> options, List<Collection> loadCalls) {
409409
return new DataLoader<>(keys -> {
410410
loadCalls.add(new ArrayList(keys));
411411
List<Future> futures = keys.stream().map(Future::succeededFuture).collect(Collectors.toList());
@@ -414,7 +414,8 @@ private static <K, V> DataLoader<K, V> idLoader(DataLoaderOptions options, List<
414414
}
415415

416416
@SuppressWarnings("unchecked")
417-
private static <K, V> DataLoader<K, V> idLoaderAllErrors(DataLoaderOptions options, List<Collection> loadCalls) {
417+
private static <K, V> DataLoader<K, V> idLoaderAllErrors(
418+
DataLoaderOptions<K, V> options, List<Collection> loadCalls) {
418419
return new DataLoader<>(keys -> {
419420
loadCalls.add(new ArrayList(keys));
420421
List<Future> futures = keys.stream()
@@ -426,7 +427,7 @@ private static <K, V> DataLoader<K, V> idLoaderAllErrors(DataLoaderOptions optio
426427

427428
@SuppressWarnings("unchecked")
428429
private static DataLoader<Integer, Integer> idLoaderWithErrors(
429-
DataLoaderOptions options, List<Collection> loadCalls) {
430+
DataLoaderOptions<Integer, Integer> options, List<Collection> loadCalls) {
430431
return new DataLoader<>(keys -> {
431432
loadCalls.add(new ArrayList(keys));
432433
List<Future> futures = keys.stream()

0 commit comments

Comments
 (0)