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.2.1.71 tracks/java/exercises/circular-buffer/.meta/src/reference/java/CircularBuffer.java
trackler-2.2.1.70 tracks/java/exercises/circular-buffer/.meta/src/reference/java/CircularBuffer.java
trackler-2.2.1.69 tracks/java/exercises/circular-buffer/.meta/src/reference/java/CircularBuffer.java
trackler-2.2.1.68 tracks/java/exercises/circular-buffer/.meta/src/reference/java/CircularBuffer.java
trackler-2.2.1.67 tracks/java/exercises/circular-buffer/.meta/src/reference/java/CircularBuffer.java
trackler-2.2.1.66 tracks/java/exercises/circular-buffer/.meta/src/reference/java/CircularBuffer.java
trackler-2.2.1.65 tracks/java/exercises/circular-buffer/.meta/src/reference/java/CircularBuffer.java
trackler-2.2.1.64 tracks/java/exercises/circular-buffer/.meta/src/reference/java/CircularBuffer.java
trackler-2.2.1.63 tracks/java/exercises/circular-buffer/.meta/src/reference/java/CircularBuffer.java
trackler-2.2.1.62 tracks/java/exercises/circular-buffer/.meta/src/reference/java/CircularBuffer.java
trackler-2.2.1.61 tracks/java/exercises/circular-buffer/.meta/src/reference/java/CircularBuffer.java
trackler-2.2.1.60 tracks/java/exercises/circular-buffer/.meta/src/reference/java/CircularBuffer.java
trackler-2.2.1.59 tracks/java/exercises/circular-buffer/.meta/src/reference/java/CircularBuffer.java
trackler-2.2.1.58 tracks/java/exercises/circular-buffer/.meta/src/reference/java/CircularBuffer.java
trackler-2.2.1.57 tracks/java/exercises/circular-buffer/.meta/src/reference/java/CircularBuffer.java
trackler-2.2.1.56 tracks/java/exercises/circular-buffer/.meta/src/reference/java/CircularBuffer.java
trackler-2.2.1.55 tracks/java/exercises/circular-buffer/.meta/src/reference/java/CircularBuffer.java
trackler-2.2.1.54 tracks/java/exercises/circular-buffer/.meta/src/reference/java/CircularBuffer.java
trackler-2.2.1.53 tracks/java/exercises/circular-buffer/.meta/src/reference/java/CircularBuffer.java
trackler-2.2.1.52 tracks/java/exercises/circular-buffer/.meta/src/reference/java/CircularBuffer.java