blob: 13596b151610a909b02787c85f75401bd9b23a21 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
package cgeo.geocaching.utils;
import java.util.ArrayList;
import java.util.Collection;
/**
* Base class for a limited list.
*
* @author blafoo
*/
public class LRUList<T> extends ArrayList<T> {
private static final long serialVersionUID = -5077882607489806620L;
private final int maxEntries;
public LRUList(int maxEntries) {
this.maxEntries = maxEntries;
}
private void removeElements(int count) {
for (int i = 0; i < count; i++) {
this.remove(0);
}
}
@Override
public boolean add(T item) {
removeElements(this.size() + 1 - maxEntries);
return super.add(item);
}
@Override
public boolean addAll(Collection<? extends T> collection) {
if (collection.size() > this.size()) {
this.clear();
for (T item : collection) {
this.add(item);
}
return false;
} else {
removeElements(this.size() + collection.size() - maxEntries);
return super.addAll(collection);
}
}
}
|