LRUCache.java

1
package com.renomad.minum.utils;
2
3
import java.io.Serial;
4
import java.util.LinkedHashMap;
5
import java.util.Map;
6
7
/**
8
 * A simple Least-Recently Used Cache
9
 * See <a href="https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU)">LRU</a>
10
 */
11
public final class LRUCache<K,V> extends LinkedHashMap<K, V> {
12
13
    @Serial
14
    private static final long serialVersionUID = -8687744696157499778L;
15
16
    // max number of entries allowed in this cache
17
    private static final int DEFAULT_MAX_ENTRIES = 100;
18
    private final int maxSize;
19
20
    @Override
21
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
22 3 1. removeEldestEntry : negated conditional → KILLED
2. removeEldestEntry : replaced boolean return with true for com/renomad/minum/utils/LRUCache::removeEldestEntry → KILLED
3. removeEldestEntry : changed conditional boundary → KILLED
        return size() > maxSize;
23
    }
24
25
    private LRUCache(int maxSize) {
26
        // uses the same load factor as found in java.util.Hashmap.DEFAULT_LOAD_FACTOR
27 1 1. <init> : Replaced integer addition with subtraction → KILLED
        super(maxSize + 1, 0.75f, true);
28
        this.maxSize = maxSize;
29
    }
30
31
    /**
32
     * Builds a map that functions as a least-recently used cache.
33
     * Sets the max size to DEFAULT_MAX_ENTRIES. If you want to specify the max size,
34
     * use the constructor at {@link #getLruCache(int)}
35
     * <br>
36
     * Make sure, when using this, to assign it to a fully-defined
37
     * type, e.g. {@code Map<String, String> foo = getLruCache()}
38
     * This is necessary since we provide this as a generic method,
39
     * and the assignment is what enables Java to determine
40
     * what types to build.
41
     */
42
    public static <K,V> Map<K, V> getLruCache() {
43 1 1. getLruCache : replaced return value with Collections.emptyMap for com/renomad/minum/utils/LRUCache::getLruCache → KILLED
        return getLruCache(DEFAULT_MAX_ENTRIES);
44
    }
45
46
    /**
47
     * Creates an LRUCache, allowing you to specify the max size.
48
     * Alternately, see {@link #getLruCache()}.
49
     * <br>
50
     * Make sure, when using this, to assign it to a fully-defined
51
     * type, e.g. {@code Map<String, String> foo = getLruCache(2)}
52
     * This is necessary since we provide this as a generic method,
53
     * and the assignment is what enables Java to determine
54
     * what types to build.
55
     */
56
    public static <K,V> Map<K, V> getLruCache(int maxSize) {
57 1 1. getLruCache : replaced return value with Collections.emptyMap for com/renomad/minum/utils/LRUCache::getLruCache → KILLED
        return new LRUCache<>(maxSize);
58
    }
59
}

Mutations

22

1.1
Location : removeEldestEntry
Killed by : com.renomad.minum.sampledomain.LruCacheTests.test_LRUCache_HappyPath(com.renomad.minum.sampledomain.LruCacheTests)
negated conditional → KILLED

2.2
Location : removeEldestEntry
Killed by : com.renomad.minum.sampledomain.LruCacheTests.test_LRUCache_HappyPath(com.renomad.minum.sampledomain.LruCacheTests)
replaced boolean return with true for com/renomad/minum/utils/LRUCache::removeEldestEntry → KILLED

3.3
Location : removeEldestEntry
Killed by : com.renomad.minum.sampledomain.LruCacheTests.test_LRUCache_HappyPath(com.renomad.minum.sampledomain.LruCacheTests)
changed conditional boundary → KILLED

27

1.1
Location : <init>
Killed by : com.renomad.minum.web.FullSystemTests.testFullSystem_EdgeCase_InstantlyClosed(com.renomad.minum.web.FullSystemTests)
Replaced integer addition with subtraction → KILLED

43

1.1
Location : getLruCache
Killed by : com.renomad.minum.FunctionalTests
replaced return value with Collections.emptyMap for com/renomad/minum/utils/LRUCache::getLruCache → KILLED

57

1.1
Location : getLruCache
Killed by : com.renomad.minum.sampledomain.LruCacheTests.test_LRUCache_HappyPath(com.renomad.minum.sampledomain.LruCacheTests)
replaced return value with Collections.emptyMap for com/renomad/minum/utils/LRUCache::getLruCache → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0