forked from Team254/FRC-2019-Public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircularBufferGeneric.java
53 lines (43 loc) · 1.38 KB
/
CircularBufferGeneric.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
package com.team254.lib.util;
import java.util.LinkedList;
/**
* Implements a simple circular buffer.
* Can be used for any class.
*/
public class CircularBufferGeneric<E> {
final int mWindowSize;
final LinkedList<E> mSamples;
double mSum;
public CircularBufferGeneric(int window_size) {
mWindowSize = window_size;
mSamples = new LinkedList<>();
mSum = 0.0;
}
public void clear() {
mSamples.clear();
mSum = 0.0;
}
public void addValue(E val) {
mSamples.addLast(val);
if (mSamples.size() > mWindowSize) {
mSamples.removeFirst();
}
}
public int getNumValues() {
return mSamples.size();
}
public boolean isFull() {
return mWindowSize == mSamples.size();
}
public LinkedList<E> getLinkedList() {
/*
* NOTE: To get an Array of the specific class type which the instance is using,
* you have to use this specific code:
* specificCircularBufferGeneric.getLinkedList().toArray(new ClassThatIWant[specificCircularBufferGeneric
* .getLinkedList().size()]);
* The reason is that for some reason an array of a generic class(i.e. E[]) cannot be created because
* of some archaic data flow ambiguities
*/
return mSamples;
}
}