Skip to content

Commit

Permalink
misc cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Feb 25, 2016
1 parent 5792e67 commit 56d89da
Show file tree
Hide file tree
Showing 24 changed files with 142 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ public enum CacheType {
},
ConcurrentHashMap {
@Override public <K, V> BasicCache<K, V> create(int maximumSize) {
return new ConcurrentMapCache<>(new ConcurrentHashMap<K, V>(maximumSize));
return new ConcurrentMapCache<>(new ConcurrentHashMap<>(maximumSize));
}
},
NonBlockingHashMap {
@Override public <K, V> BasicCache<K, V> 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<K, V>(maximumSize));
return new ConcurrentMapCache<>(new NonBlockingHashMap<>(maximumSize));
}
},

Expand Down Expand Up @@ -134,7 +134,7 @@ public enum CacheType {
},
LinkedHashMap_Lru {
@Override public <K, V> BasicCache<K, V> create(int maximumSize) {
return new LinkedHashMapCache<K, V>(true, maximumSize);
return new LinkedHashMapCache<>(true, maximumSize);
}
},
TCache_Lfu {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2651,7 +2651,7 @@ public BoundedLocalCache<K, V> cache() {
@Override
public Policy<K, V> policy() {
return (policy == null)
? (policy = new BoundedPolicy<K, V>(cache, Function.identity(), isWeighted))
? (policy = new BoundedPolicy<>(cache, Function.identity(), isWeighted))
: policy;
}

Expand Down Expand Up @@ -2935,7 +2935,7 @@ protected Policy<K, V> policy() {
Function<CompletableFuture<V>, V> transformer = Async::getIfReady;
@SuppressWarnings("unchecked")
Function<V, V> castedTransformer = (Function<V, V>) transformer;
policy = new BoundedPolicy<K, V>(castedCache, castedTransformer, isWeighted);
policy = new BoundedPolicy<>(castedCache, castedTransformer, isWeighted);
}
return policy;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ public Policy<K, V> policy() {
@Override
public ConcurrentMap<K, V> asMap() {
if (asMapView == null) {
asMapView = new AsMapView<K, V>(cache);
asMapView = new AsMapView<>(cache);
}
return asMapView;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ final class UnboundedLocalCache<K, V> implements LocalCache<K, V> {
StatsCounter statsCounter;

UnboundedLocalCache(Caffeine<? super K, ? super V> builder, boolean async) {
this.data = new ConcurrentHashMap<K, V>(builder.getInitialCapacity());
this.data = new ConcurrentHashMap<>(builder.getInitialCapacity());
this.statsCounter = builder.getStatsCounterSupplier().get();
this.removalListener = builder.getRemovalListener(async);
this.isRecordingStats = builder.isRecordingStats();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ public V setValue(V value) {
}

Object writeReplace() {
return new SimpleEntry<K, V>(this);
return new SimpleEntry<>(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,9 @@ public CacheStats snapshot() {
return DisabledStatsCounter.EMPTY_STATS;
}
}

@Override
public String toString() {
return delegate.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,6 @@
@Test(dataProviderClass = CacheProvider.class)
public final class AsyncLoadingCacheTest {

/* ---------------- CacheLoader -------------- */

@Test
public void asyncLoad() throws Exception {
CacheLoader<Integer, ?> 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<Object, ?> loader = key -> key;
try {
loader.asyncLoadAll(Collections.<Object>emptyList(), MoreExecutors.directExecutor()).get();
} catch (ExecutionException e) {
throw e.getCause();
}
}

@Test
public void asyncReload() throws Exception {
CacheLoader<Integer, Integer> loader = key -> -key;
CompletableFuture<?> future = loader.asyncReload(1, 2, MoreExecutors.directExecutor());
assertThat(future.get(), is(-1));
}

/* ---------------- getIfPresent -------------- */

@CheckNoWriter
Expand Down Expand Up @@ -715,4 +689,21 @@ public void put_replace(AsyncLoadingCache<Integer, Integer> cache, CacheContext
public void serialize(AsyncLoadingCache<Integer, Integer> cache, CacheContext context) {
assertThat(cache, is(reserializable()));
}

/* ---------------- AsyncCacheLoader -------------- */

@Test(expectedExceptions = UnsupportedOperationException.class)
public void asyncLoadAll() throws Throwable {
AsyncCacheLoader<Integer, Integer> loader =
(key, executor) -> CompletableFuture.completedFuture(-key);
loader.asyncLoadAll(Collections.emptyList(), Runnable::run).get();
}

@Test
public void asyncReload() throws Exception {
AsyncCacheLoader<Integer, Integer> loader =
(key, executor) -> CompletableFuture.completedFuture(-key);
CompletableFuture<?> future = loader.asyncReload(1, 2, Runnable::run);
assertThat(future.get(), is(-1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ protected boolean matchesSafely(AsyncLoadingCache<K, V> cache, Description descr
}

public static <K, V> IsValidAsyncCache<K, V> validAsyncCache() {
return new IsValidAsyncCache<K, V>();
return new IsValidAsyncCache<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,6 @@ private void checkNode(BoundedLocalCache<K, V> cache, Node<K, V> node, Descripti
}

public static <K, V> IsValidBoundedLocalCache<K, V> valid() {
return new IsValidBoundedLocalCache<K, V>();
return new IsValidBoundedLocalCache<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ protected boolean matchesSafely(Cache<K, V> cache, Description description) {
}

public static <K, V> IsValidCache<K, V> validCache() {
return new IsValidCache<K, V>();
return new IsValidCache<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ void checkElement(LinkedDeque<E> deque, E element, DescriptionBuilder desc) {
}

public static <E> IsValidLinkedDeque<E> validLinkedDeque() {
return new IsValidLinkedDeque<E>();
return new IsValidLinkedDeque<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ protected boolean matchesSafely(Map<K, V> map, Description description) {
}

public static <K, V> IsValidMapView<K, V> validAsMap() {
return new IsValidMapView<K, V>();
return new IsValidMapView<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ private void checkMap(UnboundedLocalCache<K, V> map, DescriptionBuilder desc) {
}

public static <K, V> IsValidUnboundedLocalCache<K, V> valid() {
return new IsValidUnboundedLocalCache<K, V>();
return new IsValidUnboundedLocalCache<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -339,4 +345,71 @@ public void reload() throws Exception {
CacheLoader<Integer, Integer> loader = key -> key;
assertThat(loader.reload(1, 1), is(1));
}

@Test
public void asyncLoad_exception() throws Exception {
Exception e = new Exception();
CacheLoader<Integer, Integer> 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<Integer, ?> 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<Integer, Integer> loader = new CacheLoader<Integer, Integer>() {
@Override public Integer load(Integer key) throws Exception {
throw new AssertionError();
}
@Override public Map<Integer, Integer> loadAll(
Iterable<? extends Integer> 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<Object, ?> 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<Integer, Integer> 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<Integer, Integer> loader = key -> -key;
CompletableFuture<?> future = loader.asyncReload(1, 2, Runnable::run);
assertThat(future.get(), is(-1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ private void checkNotification(RemovalNotification<?, ?> notification) {

public static <K, V> HasRemovalNotifications<K, V> hasRemovalNotifications(
CacheContext context, long count, RemovalCause cause) {
return new HasRemovalNotifications<K, V>(context, (int) count, cause);
return new HasRemovalNotifications<>(context, (int) count, cause);
}

public static <K, V> HasRemovalNotifications<K, V> hasRemovalNotifications(
CacheContext context, int count, RemovalCause cause) {
return new HasRemovalNotifications<K, V>(context, count, cause);
return new HasRemovalNotifications<>(context, count, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ private RemovalListeners() {}

/** A removal listener that stores the notifications for inspection. */
public static <K, V> RemovalListener<K, V> consuming() {
return new ConsumingRemovalListener<K, V>();
return new ConsumingRemovalListener<>();
}

/** A removal listener that throws an exception if a notification arrives. */
public static <K, V> RemovalListener<K, V> rejecting() {
return new RejectingRemovalListener<K, V>();
return new RejectingRemovalListener<>();
}

public static final class RejectingRemovalListener<K, V>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,6 @@ private void checkNoSuchElementException(String label,
}

public static <E> IsEmptyIterable<E> deeplyEmpty() {
return new IsEmptyIterable<E>();
return new IsEmptyIterable<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ protected boolean matchesSafely(Map<? extends K, ? extends V> map, Description d
}

public static <K, V> IsEmptyMap<K, V> emptyMap() {
return new IsEmptyMap<K, V>();
return new IsEmptyMap<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ protected boolean matchesSafely(Future<V> future, Description description) {
}

public static <V> IsFutureValue<V> future(Matcher<V> matcher) {
return new IsFutureValue<V>(matcher);
return new IsFutureValue<>(matcher);
}

public static <V> IsFutureValue<V> futureOf(V value) {
return new IsFutureValue<V>(Is.is(value));
return new IsFutureValue<>(Is.is(value));
}
}
6 changes: 3 additions & 3 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -972,19 +972,19 @@ private <T> CacheWriterException writeAllToCacheWriter(Map<? extends K, ? extend
}
List<Cache.Entry<? extends K, ? extends V>> entries = map.entrySet().stream()
.map(entry -> new EntryProxy<>(entry.getKey(), entry.getValue()))
.collect(Collectors.<Cache.Entry<? extends K, ? extends V>>toList());
.collect(Collectors.toList());
try {
writer.writeAll(entries);
return null;
} catch (CacheWriterException e) {
for (Cache.Entry<? extends K, ? extends V> entry : entries) {
entries.forEach(entry -> {
map.remove(entry.getKey());
}
});
throw e;
} catch (RuntimeException e) {
for (Cache.Entry<? extends K, ? extends V> entry : entries) {
entries.forEach(entry -> {
map.remove(entry.getKey());
}
});
return new CacheWriterException("Exception in CacheWriter", e);
}
}
Expand Down
Loading

0 comments on commit 56d89da

Please sign in to comment.