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

org.mockserver.collections.BoundedConcurrentLinkedQueue Maven / Gradle / Ivy

package org.mockserver.collections;

import java.util.Collection;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @author jamesdbloom
 */
public class BoundedConcurrentLinkedQueue extends ConcurrentLinkedQueue {
    static final long serialVersionUID = -8190199206751953870L;
    private final int maxSize;

    public BoundedConcurrentLinkedQueue(int maxSize) {
        this.maxSize = maxSize;
    }

    @Override
    public boolean add(E element) {
        if (size() >= maxSize) {
            super.poll();
        }
        return super.add(element);
    }

    @Override
    public boolean addAll(Collection collection) {
        boolean result = false;
        for (E element : collection) {
            if (add(element)) {
                result = true;
            }
        }
        return result;
    }

    @Override
    public boolean offer(E element) {
        if (size() >= maxSize) {
            super.poll();
        }
        return super.offer(element);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy