com.github.akurilov.commons.collection.BoundedPriorityBlockngQueue Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-commons Show documentation
Show all versions of java-commons Show documentation
Common functionality Java library
The newest version!
package com.github.akurilov.commons.collection;
import java.util.Collection;
import java.util.Comparator;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.atomic.LongAdder;
public class BoundedPriorityBlockngQueue
extends PriorityBlockingQueue
implements BlockingQueue {
private final int capacity;
private final LongAdder size = new LongAdder();
public BoundedPriorityBlockngQueue(final int capacity, final Comparator comparator) {
super(capacity, comparator);
this.capacity = capacity;
}
@Override
public boolean offer(final E element) {
if(size.sum() < capacity) {
if(super.offer(element)) {
size.increment();
return true;
} else {
return false;
}
} else {
return false;
}
}
@Override
public int drainTo(final Collection super E> c, final int maxElements) {
final var n = super.drainTo(c, maxElements);
size.add(-n);
return n;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy