All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.akurilov.commons.collection.BoundedPriorityBlockngQueue Maven / Gradle / Ivy

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 c, final int maxElements) {
		final var n = super.drainTo(c, maxElements);
		size.add(-n);
		return n;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy