Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RingBuffer {
- private int capacity;
- private final BigDecimal[] data;
- private int lastEntryIndex;
- public RingBuffer(int capacity) {
- this.capacity = capacity;
- this.data = new BigDecimal[capacity];
- }
- public void setElement(BigDecimal element) {
- int oldestElementIndex = getOldestElementIndex();
- if (!isFull()) {
- for (int i = 0; i < data.length; i++) {
- if (data[i] == null) {
- data[i] = element;
- lastEntryIndex = i;
- break;
- }
- }
- } else {
- data[oldestElementIndex] = element;
- }
- }
- public int getOldestElementIndex() {
- int oldestEntryIndex;
- if ((lastEntryIndex == data.length - 1) || !isFull()) {
- oldestEntryIndex = 0;
- } else oldestEntryIndex = lastEntryIndex + 1;
- return oldestEntryIndex;
- }
- public boolean isFull() {
- boolean hasNoNulls = false;
- for (int i = 0; i < data.length; i++) {
- hasNoNulls = data[i] != null;
- }
- return hasNoNulls;
- }
- public int getCapacity() {
- return capacity;
- }
- public void setCapacity(int capacity) {
- this.capacity = capacity;
- }
- public BigDecimal[] getData() {
- return data;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement