1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
package net.sourceforge.subsonic.androidapp.util; |
20 |
|
|
21 |
|
import java.lang.ref.SoftReference; |
22 |
|
import java.util.HashMap; |
23 |
|
import java.util.Map; |
24 |
|
|
25 |
|
|
26 |
|
@author |
27 |
|
|
|
|
| 51,4% |
Uncovered Elements: 17 (35) |
Complexity: 9 |
Complexity Density: 0,41 |
|
28 |
|
public class LRUCache<K,V>{ |
29 |
|
|
30 |
|
private final int capacity; |
31 |
|
private final Map<K, TimestampedValue> map; |
32 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
33 |
3
|
public LRUCache(int capacity) {... |
34 |
3
|
map = new HashMap<K, TimestampedValue>(capacity); |
35 |
3
|
this.capacity = capacity; |
36 |
|
} |
37 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0,33 |
|
38 |
502
|
public synchronized V get(K key) {... |
39 |
502
|
TimestampedValue value = map.get(key); |
40 |
|
|
41 |
502
|
V result = null; |
42 |
502
|
if (value != null) { |
43 |
430
|
value.updateTimestamp(); |
44 |
430
|
result = value.getValue(); |
45 |
|
} |
46 |
|
|
47 |
502
|
return result; |
48 |
|
} |
49 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
50 |
90
|
public synchronized void put(K key, V value) {... |
51 |
90
|
if (map.size() >= capacity) { |
52 |
0
|
removeOldest(); |
53 |
|
} |
54 |
90
|
map.put(key, new TimestampedValue(value)); |
55 |
|
} |
56 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
57 |
2
|
public void clear() {... |
58 |
2
|
map.clear(); |
59 |
|
} |
60 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 3 |
Complexity Density: 0,3 |
|
61 |
0
|
private void removeOldest() {... |
62 |
0
|
K oldestKey = null; |
63 |
0
|
long oldestTimestamp = Long.MAX_VALUE; |
64 |
|
|
65 |
0
|
for (Map.Entry<K, TimestampedValue> entry : map.entrySet()) { |
66 |
0
|
K key = entry.getKey(); |
67 |
0
|
TimestampedValue value = entry.getValue(); |
68 |
0
|
if (value.getTimestamp() < oldestTimestamp) { |
69 |
0
|
oldestTimestamp = value.getTimestamp(); |
70 |
0
|
oldestKey = key; |
71 |
|
} |
72 |
|
} |
73 |
|
|
74 |
0
|
if (oldestKey != null) { |
75 |
0
|
map.remove(oldestKey); |
76 |
|
} |
77 |
|
} |
78 |
|
|
|
|
| 77,8% |
Uncovered Elements: 2 (9) |
Complexity: 4 |
Complexity Density: 0,8 |
|
79 |
|
private final class TimestampedValue { |
80 |
|
|
81 |
|
private final SoftReference<V> value; |
82 |
|
private long timestamp; |
83 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
84 |
90
|
public TimestampedValue(V value) {... |
85 |
90
|
this.value = new SoftReference<V>(value); |
86 |
90
|
updateTimestamp(); |
87 |
|
} |
88 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
89 |
430
|
public V getValue() {... |
90 |
430
|
return value.get(); |
91 |
|
} |
92 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
93 |
0
|
public long getTimestamp() {... |
94 |
0
|
return timestamp; |
95 |
|
} |
96 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
97 |
520
|
public void updateTimestamp() {... |
98 |
520
|
timestamp = System.currentTimeMillis(); |
99 |
|
} |
100 |
|
} |
101 |
|
|
102 |
|
} |