-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRingBufferOfFloat.java
46 lines (40 loc) · 1.2 KB
/
RingBufferOfFloat.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
package com.newbilius.simplegpsspeedometer.Utilities;
public class RingBufferOfFloat {
private float[] array;
private int maxSize;
private int writePointer = -1;
private int currentSize = 0;
public RingBufferOfFloat(int maxSize) {
array = new float[maxSize];
this.maxSize = maxSize;
}
public void add(float value) {
synchronized (this) {
currentSize++;
if (currentSize > maxSize)
currentSize = maxSize;
writePointer++;
if (writePointer == maxSize)
writePointer = 0;
array[writePointer] = value;
}
}
public float[] getArray() {
synchronized (this) {
if (currentSize == maxSize)
return array.clone();
else {
float[] newArray = new float[currentSize];
System.arraycopy(array, 0, newArray, 0, currentSize);
return newArray;
}
}
}
public void clear() {
synchronized (this) {
writePointer = -1;
currentSize = 0;
array = new float[maxSize];
}
}
}