-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheFIFO.java
45 lines (38 loc) · 1.05 KB
/
CacheFIFO.java
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
import java.util.LinkedList;
public class CacheFIFO {
private final int maxSize;
private LinkedList<NoAVL> cache;
public CacheFIFO() {
this.maxSize = 21;
this.cache = new LinkedList<>();
}
public void adicionar(NoAVL os) {
if (cache.size() >= maxSize) {
cache.removeFirst();
}
cache.addLast(os);
}
public NoAVL buscar(int codigo) {
for (NoAVL no : cache) {
if (no.os.getCodigo() == codigo) {
return no;
}
}
return null;
}
public void listarCache() {
for (NoAVL no : cache) {
System.out.println(no.os);
}
}
public void remover(int codigo) {
cache.removeIf(no -> no.os.getCodigo() == codigo);
}
public String gerarStringCache() {
StringBuilder sb = new StringBuilder();
for (NoAVL no : cache) {
sb.append(no.os.imprimir()).append(", ");
}
return sb.length() > 0 ? sb.substring(0, sb.length() - 2) : "Cache vazia";
}
}