File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private void evictExpired() {
201201
while (it.hasNext()) {
202202
K k = it.next();
203203
CacheEntry<V> entry = cache.get(k);
204-
if (entry.isExpired()) {
204+
if (entry != null && entry.isExpired()) {
205205
it.remove();
206206
cache.remove(k);
207207
notifyEviction(k, entry.value);
@@ -248,7 +248,11 @@ private void notifyEviction(K key, V value) {
248248
*/
249249
public long getHits() {
250250
lock.lock();
251-
try { return hits; } finally { lock.unlock(); }
251+
try {
252+
return hits;
253+
} finally {
254+
lock.unlock();
255+
}
252256
}
253257

254258
/**
@@ -258,7 +262,11 @@ public long getHits() {
258262
*/
259263
public long getMisses() {
260264
lock.lock();
261-
try { return misses; } finally { lock.unlock(); }
265+
try {
266+
return misses;
267+
} finally {
268+
lock.unlock();
269+
}
262270
}
263271

264272
/**
@@ -300,8 +308,7 @@ public String toString() {
300308
visible.put(entry.getKey(), entry.getValue().value);
301309
}
302310
}
303-
return String.format("Cache(capacity=%d, size=%d, hits=%d, misses=%d, entries=%s)",
304-
capacity, visible.size(), hits, misses, visible);
311+
return String.format("Cache(capacity=%d, size=%d, hits=%d, misses=%d, entries=%s)", capacity, visible.size(), hits, misses, visible);
305312
} finally {
306313
lock.unlock();
307314
}
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
11
package com.thealgorithms.datastructures.caches;
22

3-
import org.junit.jupiter.api.BeforeEach;
4-
import org.junit.jupiter.api.Test;
5-
import org.junit.jupiter.api.function.Executable;
6-
7-
import java.util.ArrayList;
8-
import java.util.List;
9-
import java.util.Random;
10-
113
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
124
import static org.junit.jupiter.api.Assertions.assertEquals;
135
import static org.junit.jupiter.api.Assertions.assertFalse;
146
import static org.junit.jupiter.api.Assertions.assertNull;
157
import static org.junit.jupiter.api.Assertions.assertThrows;
168
import static org.junit.jupiter.api.Assertions.assertTrue;
179

10+
import java.util.ArrayList;
11+
import java.util.HashSet;
12+
import java.util.List;
13+
import java.util.Random;
14+
import java.util.Set;
15+
import org.junit.jupiter.api.BeforeEach;
16+
import org.junit.jupiter.api.Test;
17+
import org.junit.jupiter.api.function.Executable;
18+
1819
class RRCacheTest {
1920

2021
private RRCache<String, String> cache;
21-
private List<String> evictedKeys;
22+
private Set<String> evictedKeys;
2223
private List<String> evictedValues;
2324

2425
@BeforeEach
2526
void setUp() {
26-
evictedKeys = new ArrayList<>();
27+
evictedKeys = new HashSet<>();
2728
evictedValues = new ArrayList<>();
2829

2930
cache = new RRCache.Builder<String, String>(3)
30-
.defaultTTL(1000)
31-
.random(new Random(0))
32-
.evictionListener((k, v) -> {
33-
evictedKeys.add(k);
34-
evictedValues.add(v);
35-
})
36-
.build();
31+
.defaultTTL(1000)
32+
.random(new Random(0))
33+
.evictionListener((k, v) -> {
34+
evictedKeys.add(k);
35+
evictedValues.add(v);
36+
})
37+
.build();
3738
}
3839

3940
@Test
@@ -148,11 +149,7 @@ void testBuilderNullEvictionListenerThrows() {
148149

149150
@Test
150151
void testEvictionListenerExceptionDoesNotCrash() {
151-
RRCache<String, String> listenerCache = new RRCache.Builder<String, String>(1)
152-
.evictionListener((k, v) -> {
153-
throw new RuntimeException("Exception");
154-
})
155-
.build();
152+
RRCache<String, String> listenerCache = new RRCache.Builder<String, String>(1).evictionListener((k, v) -> { throw new RuntimeException("Exception"); }).build();
156153

157154
listenerCache.put("a", "a");
158155
listenerCache.put("b", "b"); // causes eviction but should not crash
@@ -161,9 +158,7 @@ void testEvictionListenerExceptionDoesNotCrash() {
161158

162159
@Test
163160
void testTtlZeroThrowsIllegalArgumentException() {
164-
Executable exec = () -> new RRCache.Builder<String, String>(3)
165-
.defaultTTL(-1)
166-
.build();
161+
Executable exec = () -> new RRCache.Builder<String, String>(3).defaultTTL(-1).build();
167162
assertThrows(IllegalArgumentException.class, exec);
168163
}
169164
}

0 commit comments

Comments
 (0)