diff --git a/caffeine/src/jmh/java/com/github/benmanes/caffeine/cache/CacheType.java b/caffeine/src/jmh/java/com/github/benmanes/caffeine/cache/CacheType.java index 89be0d098d..7b9005c7c3 100644 --- a/caffeine/src/jmh/java/com/github/benmanes/caffeine/cache/CacheType.java +++ b/caffeine/src/jmh/java/com/github/benmanes/caffeine/cache/CacheType.java @@ -57,14 +57,14 @@ public enum CacheType { }, ConcurrentHashMap { @Override public BasicCache create(int maximumSize) { - return new ConcurrentMapCache<>(new ConcurrentHashMap(maximumSize)); + return new ConcurrentMapCache<>(new ConcurrentHashMap<>(maximumSize)); } }, NonBlockingHashMap { @Override public BasicCache create(int maximumSize) { // Note that writes that update an entry to the same reference are short circuited // and do not mutate the hash table. This makes those writes equal to a read. - return new ConcurrentMapCache<>(new NonBlockingHashMap(maximumSize)); + return new ConcurrentMapCache<>(new NonBlockingHashMap<>(maximumSize)); } }, @@ -134,7 +134,7 @@ public enum CacheType { }, LinkedHashMap_Lru { @Override public BasicCache create(int maximumSize) { - return new LinkedHashMapCache(true, maximumSize); + return new LinkedHashMapCache<>(true, maximumSize); } }, TCache_Lfu { diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/BoundedLocalCache.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/BoundedLocalCache.java index 3f53835358..d162e76fda 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/BoundedLocalCache.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/BoundedLocalCache.java @@ -2651,7 +2651,7 @@ public BoundedLocalCache cache() { @Override public Policy policy() { return (policy == null) - ? (policy = new BoundedPolicy(cache, Function.identity(), isWeighted)) + ? (policy = new BoundedPolicy<>(cache, Function.identity(), isWeighted)) : policy; } @@ -2935,7 +2935,7 @@ protected Policy policy() { Function, V> transformer = Async::getIfReady; @SuppressWarnings("unchecked") Function castedTransformer = (Function) transformer; - policy = new BoundedPolicy(castedCache, castedTransformer, isWeighted); + policy = new BoundedPolicy<>(castedCache, castedTransformer, isWeighted); } return policy; } diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/LocalAsyncLoadingCache.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/LocalAsyncLoadingCache.java index 69b4134082..df4b832649 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/LocalAsyncLoadingCache.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/LocalAsyncLoadingCache.java @@ -487,7 +487,7 @@ public Policy policy() { @Override public ConcurrentMap asMap() { if (asMapView == null) { - asMapView = new AsMapView(cache); + asMapView = new AsMapView<>(cache); } return asMapView; } diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/UnboundedLocalCache.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/UnboundedLocalCache.java index 8e17689261..7ddf3d685d 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/UnboundedLocalCache.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/UnboundedLocalCache.java @@ -65,7 +65,7 @@ final class UnboundedLocalCache implements LocalCache { StatsCounter statsCounter; UnboundedLocalCache(Caffeine builder, boolean async) { - this.data = new ConcurrentHashMap(builder.getInitialCapacity()); + this.data = new ConcurrentHashMap<>(builder.getInitialCapacity()); this.statsCounter = builder.getStatsCounterSupplier().get(); this.removalListener = builder.getRemovalListener(async); this.isRecordingStats = builder.isRecordingStats(); diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/WriteThroughEntry.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/WriteThroughEntry.java index 3ba8cebb3d..d9fba6dd0a 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/WriteThroughEntry.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/WriteThroughEntry.java @@ -42,6 +42,6 @@ public V setValue(V value) { } Object writeReplace() { - return new SimpleEntry(this); + return new SimpleEntry<>(this); } } diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/stats/GuardedStatsCounter.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/stats/GuardedStatsCounter.java index acf3a51e49..d9e10956db 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/stats/GuardedStatsCounter.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/stats/GuardedStatsCounter.java @@ -90,4 +90,9 @@ public CacheStats snapshot() { return DisabledStatsCounter.EMPTY_STATS; } } + + @Override + public String toString() { + return delegate.toString(); + } } diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/AsyncLoadingCacheTest.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/AsyncLoadingCacheTest.java index ac8d704a57..a1db76eb98 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/AsyncLoadingCacheTest.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/AsyncLoadingCacheTest.java @@ -70,32 +70,6 @@ @Test(dataProviderClass = CacheProvider.class) public final class AsyncLoadingCacheTest { - /* ---------------- CacheLoader -------------- */ - - @Test - public void asyncLoad() throws Exception { - CacheLoader loader = key -> key; - CompletableFuture future = loader.asyncLoad(1, MoreExecutors.directExecutor()); - assertThat(future.get(), is(1)); - } - - @Test(expectedExceptions = UnsupportedOperationException.class) - public void asyncLoadAll() throws Throwable { - CacheLoader loader = key -> key; - try { - loader.asyncLoadAll(Collections.emptyList(), MoreExecutors.directExecutor()).get(); - } catch (ExecutionException e) { - throw e.getCause(); - } - } - - @Test - public void asyncReload() throws Exception { - CacheLoader loader = key -> -key; - CompletableFuture future = loader.asyncReload(1, 2, MoreExecutors.directExecutor()); - assertThat(future.get(), is(-1)); - } - /* ---------------- getIfPresent -------------- */ @CheckNoWriter @@ -715,4 +689,21 @@ public void put_replace(AsyncLoadingCache cache, CacheContext public void serialize(AsyncLoadingCache cache, CacheContext context) { assertThat(cache, is(reserializable())); } + + /* ---------------- AsyncCacheLoader -------------- */ + + @Test(expectedExceptions = UnsupportedOperationException.class) + public void asyncLoadAll() throws Throwable { + AsyncCacheLoader loader = + (key, executor) -> CompletableFuture.completedFuture(-key); + loader.asyncLoadAll(Collections.emptyList(), Runnable::run).get(); + } + + @Test + public void asyncReload() throws Exception { + AsyncCacheLoader loader = + (key, executor) -> CompletableFuture.completedFuture(-key); + CompletableFuture future = loader.asyncReload(1, 2, Runnable::run); + assertThat(future.get(), is(-1)); + } } diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidAsyncCache.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidAsyncCache.java index 6780243826..70f873bf09 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidAsyncCache.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidAsyncCache.java @@ -59,6 +59,6 @@ protected boolean matchesSafely(AsyncLoadingCache cache, Description descr } public static IsValidAsyncCache validAsyncCache() { - return new IsValidAsyncCache(); + return new IsValidAsyncCache<>(); } } diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidBoundedLocalCache.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidBoundedLocalCache.java index 17143689af..87d877e6ec 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidBoundedLocalCache.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidBoundedLocalCache.java @@ -213,6 +213,6 @@ private void checkNode(BoundedLocalCache cache, Node node, Descripti } public static IsValidBoundedLocalCache valid() { - return new IsValidBoundedLocalCache(); + return new IsValidBoundedLocalCache<>(); } } diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidCache.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidCache.java index 0b82b01d68..35a0a2d7c8 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidCache.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidCache.java @@ -80,6 +80,6 @@ protected boolean matchesSafely(Cache cache, Description description) { } public static IsValidCache validCache() { - return new IsValidCache(); + return new IsValidCache<>(); } } diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidLinkedDeque.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidLinkedDeque.java index 807720988f..d7ee74f474 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidLinkedDeque.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidLinkedDeque.java @@ -90,6 +90,6 @@ void checkElement(LinkedDeque deque, E element, DescriptionBuilder desc) { } public static IsValidLinkedDeque validLinkedDeque() { - return new IsValidLinkedDeque(); + return new IsValidLinkedDeque<>(); } } diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidMapView.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidMapView.java index 9341d46180..3eb5151655 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidMapView.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidMapView.java @@ -62,6 +62,6 @@ protected boolean matchesSafely(Map map, Description description) { } public static IsValidMapView validAsMap() { - return new IsValidMapView(); + return new IsValidMapView<>(); } } diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidUnboundedLocalCache.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidUnboundedLocalCache.java index 81e42a5b36..a5e0183c1b 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidUnboundedLocalCache.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/IsValidUnboundedLocalCache.java @@ -70,6 +70,6 @@ private void checkMap(UnboundedLocalCache map, DescriptionBuilder desc) { } public static IsValidUnboundedLocalCache valid() { - return new IsValidUnboundedLocalCache(); + return new IsValidUnboundedLocalCache<>(); } } diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/LoadingCacheTest.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/LoadingCacheTest.java index e75c922085..921f965d14 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/LoadingCacheTest.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/LoadingCacheTest.java @@ -25,11 +25,17 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.Matchers.sameInstance; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.ExecutionException; +import org.junit.Assert; import org.testng.annotations.Listeners; import org.testng.annotations.Test; @@ -339,4 +345,71 @@ public void reload() throws Exception { CacheLoader loader = key -> key; assertThat(loader.reload(1, 1), is(1)); } + + @Test + public void asyncLoad_exception() throws Exception { + Exception e = new Exception(); + CacheLoader loader = key -> { throw e; }; + try { + loader.asyncLoad(1, Runnable::run).join(); + } catch (CompletionException ex) { + assertThat(ex.getCause(), is(sameInstance(e))); + } + } + + @Test + public void asyncLoad() throws Exception { + CacheLoader loader = key -> key; + CompletableFuture future = loader.asyncLoad(1, Runnable::run); + assertThat(future.get(), is(1)); + } + + @Test + public void asyncLoadAll_exception() throws Exception { + Exception e = new Exception(); + CacheLoader loader = new CacheLoader() { + @Override public Integer load(Integer key) throws Exception { + throw new AssertionError(); + } + @Override public Map loadAll( + Iterable keys) throws Exception { + throw e; + } + }; + try { + loader.asyncLoadAll(Arrays.asList(1), Runnable::run).join(); + } catch (CompletionException ex) { + assertThat(ex.getCause(), is(sameInstance(e))); + } + } + + @Test(expectedExceptions = UnsupportedOperationException.class) + public void asyncLoadAll() throws Throwable { + CacheLoader loader = key -> key; + try { + loader.asyncLoadAll(Collections.emptyList(), Runnable::run).get(); + } catch (ExecutionException e) { + throw e.getCause(); + } + } + + @Test + public void asyncReload_exception() throws Exception { + for (Exception e : Arrays.asList(new Exception(), new RuntimeException())) { + CacheLoader loader = key -> { throw e; }; + try { + loader.asyncReload(1, 1, Runnable::run).join(); + Assert.fail(); + } catch (CompletionException ex) { + assertThat(ex.getCause(), is(sameInstance(e))); + } + } + } + + @Test + public void asyncReload() throws Exception { + CacheLoader loader = key -> -key; + CompletableFuture future = loader.asyncReload(1, 2, Runnable::run); + assertThat(future.get(), is(-1)); + } } diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/stats/StatsCounterTest.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/stats/StatsCounterTest.java index f6f1087c56..6b46b8d202 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/stats/StatsCounterTest.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/stats/StatsCounterTest.java @@ -79,6 +79,19 @@ public void concurrent() { @Test public void guarded() { + StatsCounter counter = StatsCounter.guardedStatsCounter(new ConcurrentStatsCounter()); + counter.recordHits(1); + counter.recordMisses(1); + counter.recordEviction(); + counter.recordLoadSuccess(1); + counter.recordLoadFailure(1); + assertThat(counter.snapshot(), is(new CacheStats(1, 1, 1, 1, 2, 1))); + assertThat(counter.toString(), is(new CacheStats(1, 1, 1, 1, 2, 1).toString())); + assertThat(counter.snapshot().toString(), is(new CacheStats(1, 1, 1, 1, 2, 1).toString())); + } + + @Test + public void guarded_exception() { StatsCounter statsCounter = Mockito.mock(StatsCounter.class); when(statsCounter.snapshot()).thenThrow(new NullPointerException()); doThrow(NullPointerException.class).when(statsCounter).recordEviction(); diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/HasRemovalNotifications.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/HasRemovalNotifications.java index f11cddd9e5..19fec0053b 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/HasRemovalNotifications.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/HasRemovalNotifications.java @@ -102,11 +102,11 @@ private void checkNotification(RemovalNotification notification) { public static HasRemovalNotifications hasRemovalNotifications( CacheContext context, long count, RemovalCause cause) { - return new HasRemovalNotifications(context, (int) count, cause); + return new HasRemovalNotifications<>(context, (int) count, cause); } public static HasRemovalNotifications hasRemovalNotifications( CacheContext context, int count, RemovalCause cause) { - return new HasRemovalNotifications(context, count, cause); + return new HasRemovalNotifications<>(context, count, cause); } } diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/RemovalListeners.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/RemovalListeners.java index 3c2a3d70a8..6cf1ad1731 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/RemovalListeners.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/RemovalListeners.java @@ -34,12 +34,12 @@ private RemovalListeners() {} /** A removal listener that stores the notifications for inspection. */ public static RemovalListener consuming() { - return new ConsumingRemovalListener(); + return new ConsumingRemovalListener<>(); } /** A removal listener that throws an exception if a notification arrives. */ public static RemovalListener rejecting() { - return new RejectingRemovalListener(); + return new RejectingRemovalListener<>(); } public static final class RejectingRemovalListener diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/testing/IsEmptyIterable.java b/caffeine/src/test/java/com/github/benmanes/caffeine/testing/IsEmptyIterable.java index fc33d22eb7..ecb57570f4 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/testing/IsEmptyIterable.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/testing/IsEmptyIterable.java @@ -115,6 +115,6 @@ private void checkNoSuchElementException(String label, } public static IsEmptyIterable deeplyEmpty() { - return new IsEmptyIterable(); + return new IsEmptyIterable<>(); } } diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/testing/IsEmptyMap.java b/caffeine/src/test/java/com/github/benmanes/caffeine/testing/IsEmptyMap.java index 9af3b86461..260d023596 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/testing/IsEmptyMap.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/testing/IsEmptyMap.java @@ -56,6 +56,6 @@ protected boolean matchesSafely(Map map, Description d } public static IsEmptyMap emptyMap() { - return new IsEmptyMap(); + return new IsEmptyMap<>(); } } diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/testing/IsFutureValue.java b/caffeine/src/test/java/com/github/benmanes/caffeine/testing/IsFutureValue.java index 42118e977d..0e65d96034 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/testing/IsFutureValue.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/testing/IsFutureValue.java @@ -47,10 +47,10 @@ protected boolean matchesSafely(Future future, Description description) { } public static IsFutureValue future(Matcher matcher) { - return new IsFutureValue(matcher); + return new IsFutureValue<>(matcher); } public static IsFutureValue futureOf(V value) { - return new IsFutureValue(Is.is(value)); + return new IsFutureValue<>(Is.is(value)); } } diff --git a/gradle/dependencies.gradle b/gradle/dependencies.gradle index dfe2c64aa1..62997896e8 100644 --- a/gradle/dependencies.gradle +++ b/gradle/dependencies.gradle @@ -35,9 +35,9 @@ ext { javapoet: '1.5.1', jcache: '1.0.0', jsr305: '3.0.1', - stream: '2.9.0', + stream: '2.9.1', univocity_parsers: '2.0.0', - ycsb: '0.7.0-RC1', + ycsb: '0.7.0-RC2', ] test_versions = [ awaitility: '1.7.0', @@ -46,7 +46,7 @@ ext { jcache_tck: '1.0.1', jctools: '1.1', junit: '4.12', - mockito: '2.0.42-beta', + mockito: '2.0.43-beta', pax_exam: '4.8.0', testng: '6.9.10', truth: '0.24', diff --git a/jcache/src/main/java/com/github/benmanes/caffeine/jcache/CacheProxy.java b/jcache/src/main/java/com/github/benmanes/caffeine/jcache/CacheProxy.java index 9a0d547053..c870c7dccf 100644 --- a/jcache/src/main/java/com/github/benmanes/caffeine/jcache/CacheProxy.java +++ b/jcache/src/main/java/com/github/benmanes/caffeine/jcache/CacheProxy.java @@ -972,19 +972,19 @@ private CacheWriterException writeAllToCacheWriter(Map> entries = map.entrySet().stream() .map(entry -> new EntryProxy<>(entry.getKey(), entry.getValue())) - .collect(Collectors.>toList()); + .collect(Collectors.toList()); try { writer.writeAll(entries); return null; } catch (CacheWriterException e) { - for (Cache.Entry entry : entries) { + entries.forEach(entry -> { map.remove(entry.getKey()); - } + }); throw e; } catch (RuntimeException e) { - for (Cache.Entry entry : entries) { + entries.forEach(entry -> { map.remove(entry.getKey()); - } + }); return new CacheWriterException("Exception in CacheWriter", e); } } diff --git a/jcache/src/main/java/com/github/benmanes/caffeine/jcache/LoadingCacheProxy.java b/jcache/src/main/java/com/github/benmanes/caffeine/jcache/LoadingCacheProxy.java index 332a10d95d..d889d82090 100644 --- a/jcache/src/main/java/com/github/benmanes/caffeine/jcache/LoadingCacheProxy.java +++ b/jcache/src/main/java/com/github/benmanes/caffeine/jcache/LoadingCacheProxy.java @@ -87,7 +87,7 @@ private V getOrLoad(K key) { statistics.recordEvictions(1); return null; } - return expired; + return e; }); expirable = null; } diff --git a/jcache/src/main/java/com/github/benmanes/caffeine/jcache/event/EventDispatcher.java b/jcache/src/main/java/com/github/benmanes/caffeine/jcache/event/EventDispatcher.java index 40f7c5b8ee..34abbc9fde 100644 --- a/jcache/src/main/java/com/github/benmanes/caffeine/jcache/event/EventDispatcher.java +++ b/jcache/src/main/java/com/github/benmanes/caffeine/jcache/event/EventDispatcher.java @@ -76,7 +76,7 @@ public void register(CacheEntryListenerConfiguration configuration) { CacheEntryEventFilter filter = event -> true; if (configuration.getCacheEntryEventFilterFactory() != null) { - filter = new EventTypeFilter(listener, + filter = new EventTypeFilter<>(listener, configuration.getCacheEntryEventFilterFactory().create()); } @@ -211,8 +211,8 @@ public void ignoreSynchronous() { /** Broadcasts the event to all of the interested listener's dispatch queues. */ private void publish(JCacheEntryEvent event, boolean quiet) { for (Registration registration : dispatchQueues.keySet()) { - if (!registration.getCacheEntryFilter().evaluate(event) - || !registration.getCacheEntryListener().isCompatible(event)) { + if (!registration.getCacheEntryListener().isCompatible(event) + || !registration.getCacheEntryFilter().evaluate(event)) { continue; }