[New] Added FixedSizeQueue

This commit is contained in:
Robert von Burg 2017-06-15 13:33:24 +02:00
parent e459b561b8
commit 030ff9f6b3
1 changed files with 33 additions and 0 deletions

View File

@ -0,0 +1,33 @@
package li.strolch.utils.collections;
import java.util.Collection;
import java.util.LinkedList;
public class FixedSizeQueue<E> extends LinkedList<E> {
private static final long serialVersionUID = 1L;
private int maxSize;
public FixedSizeQueue(int size) {
this.maxSize = size;
}
@Override
public boolean add(E e) {
boolean b = super.add(e);
if (size() >= this.maxSize) {
removeRange(0, size() - this.maxSize);
}
return b;
}
@Override
public boolean addAll(int index, Collection<? extends E> c) {
boolean b = super.addAll(index, c);
if (size() >= this.maxSize) {
removeRange(0, size() - this.maxSize);
}
return b;
}
}