Skip
Skip

Reputation: 6531

Difference between BoundedFifoBuffer and CircularFifoBuffer?

In apache common collections what is the difference between:

  1. CircularFifoBuffer
  2. BoundedFifoBuffer

Ok, the first deletes oldest Entries when full, the other deletes entries in the same order, as the came in. But isn't it the same?

Upvotes: 3

Views: 1047

Answers (3)

Yash Singla
Yash Singla

Reputation: 144

say you put n items in a buffer with index 1,2,3--n

Now in both CircularFifoBuffer & BoundedFifoBuffer the space is full.

In bounded buffer since the nth element is full, It will say the whole buffer is full.

In CircularFifoBuffer, If the buffer is full, the least recently added element is discarded so that a new element can be inserted. So if nth element is full it would put the next element in the 1st index.

Upvotes: 3

arvin_codeHunk
arvin_codeHunk

Reputation: 2390

When BoundedFifoBuffer s full it prevent insertion of another element. But in CircularFifoBuffer ,it removes eldest one ,once it full.

Upvotes: 2

Mikita Belahlazau
Mikita Belahlazau

Reputation: 15434

CircularFifoBuffer extends BoundedFifoBuffer. It only overrides single method - add:

public boolean add(Object element) {
    if (isFull()) {
        remove();
    }
    return super.add(element);
}

So the only difference is that BoundedFifoBuffer throws exception when it's full and you try to add new element while CircularFifoBuffer removes oldest element.

Upvotes: 5

Related Questions