-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNBuffer.java
More file actions
75 lines (67 loc) · 1.92 KB
/
Copy pathNBuffer.java
File metadata and controls
75 lines (67 loc) · 1.92 KB
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
import java.util.Iterator;
import java.util.LinkedList;
/**
* NBuffer is an auxiliary class I made to store N generic
* objects. It is essentially a FIFO queue, except it only
* has a push function, which will return the oldest object
* in the list if and only if the list is full.
* It also has an iterator function that returns an iterator of
* the list of objects.
* @author gmh73
*
* @param <T>
*/
public class NBuffer<T> {
//The object of the buffer are stored in a LinkedList
private LinkedList<T> buffer;
//The max number of items in the buffer
public final int N;
/**
* Set N and construct the buffer
* @param n The max number of values the buffer holds
*/
public NBuffer(int n){
N = n;
buffer = new LinkedList<T>();
}
/**
* Push an object into the buffer
* This will return and remove the last item in the list
* if there were N items in the buffer before calling push
* @param in The value we are pushing into the buffer
* @return Returns the last element remembered in the buffer if
* it is full (the last element is pushed out), otherwise
* returns null if the buffer was not full.
*/
public T push(T in){
T out = null;
buffer.add(in);
//If this new input makes the list too big
if(buffer.size() == N + 1){
//remove the oldest item and set it to the output
out = buffer.removeFirst();
}
return (T)out;
}
/**
* Fills the buffer with newVal
* @param newVal The value the buffer is filled with
*/
public void reset(T newVal){
for(int i = 0; i < N; i++){
push(newVal);
}
}
/**
* Simply returns the iterator of the buffer.
* This function breaks encapsulation!
* Please use cautiously: it is only intended
* for READ-ONLY operations. There is no way to
* enforce this convention, so please keep it in mind
* when coding.
* @return returns the iterator of the buffer
*/
public Iterator<T> iterator(){
return buffer.iterator();
}
}