-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCache.java
89 lines (78 loc) · 1.78 KB
/
Cache.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.Arrays;
import java.util.LinkedList;
/**
* Cache is a "Cache" implementation using linked list data structure.
*
* @author Mario Torres
*
* @param <T>
*/
public class Cache<T> {
private LinkedList<T> cacheLinkedList;
private int size;
/**
* Constructor: creates a Cache
*/
public Cache(int size) {
this.cacheLinkedList = new LinkedList<T>();
this.size = size;
}
/**
* Returns Cache Linked List
*
* @return Cache Linked List
*/
public LinkedList<T> getCacheLinkedList() {
return cacheLinkedList;
}
/**
* Adds the element at the top of the Cache
*
* @param element which is added to the top of the Cache
*/
public void addToCache(T element) {
cacheLinkedList.addFirst(element);
}
/**
* Removes the last element in the Cache
*/
public void removeLastCache() {
cacheLinkedList.removeLast();
}
/**
* Removes the element in the index of the Cache
*
* @param index is the index of the element to be removed
*/
public void removeFromCache(int index) {
cacheLinkedList.remove(index);
}
/**
* Empties the Cache
*/
public void clearCache() {
cacheLinkedList.clear();
}
/**
* Returns true if the Cache is full
*
* @return true if Cache is full
*/
public boolean cacheFull() {
return (cacheLinkedList.size() == size);
}
/**
* Returns the Cache size
*
* @return the Cache size
*/
public int cacheSize() {
return cacheLinkedList.size();
}
/**
* String output of the Cache Linked List
*/
public String toString() {
return Arrays.deepToString(cacheLinkedList.toArray());
}
}