Sha256: 798cd73ce422c58f9e00e84c3780265eeb2e2559bfd550673cfd95336a522c74

Contents?: true

Size: 942 Bytes

Versions: 81

Compression:

Stored size: 942 Bytes

Contents

import java.util.LinkedList;
import java.util.Queue;

public class CircularBuffer<T> {

    private Queue<T> buffer;
    private int capacity;

    public CircularBuffer(int size) {
        this.buffer = new LinkedList<>();
        this.capacity = size;
    }

    public T read() throws BufferIOException {
        if (this.buffer.size() == 0) {
            throw new BufferIOException("Tried to read from empty buffer");
        }

        return this.buffer.remove();
    }

    public void write(T data) throws BufferIOException {
        if (this.buffer.size() == this.capacity) {
            throw new BufferIOException("Tried to write to full buffer");
        }

        this.buffer.add(data);
    }
    public void overwrite(T data) {
        if (this.buffer.size() == this.capacity) {
            this.buffer.remove();
        }

        this.buffer.add(data);
    }

    public void clear() {
        this.buffer.clear();
    }
}

Version data entries

81 entries across 81 versions & 1 rubygems

Version Path
trackler-2.1.0.54 tracks/java/exercises/circular-buffer/src/example/java/CircularBuffer.java