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

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

There is a newer version: 5.15.0
Show newest version
package org.mockserver.collections;

import java.util.Collection;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.function.Consumer;

/**
 * @author jamesdbloom
 */
public class CircularConcurrentLinkedDeque extends ConcurrentLinkedDeque {
    private int maxSize;
    private final Consumer onEvictCallback;

    public CircularConcurrentLinkedDeque(int maxSize, Consumer onEvictCallback) {
        this.maxSize = maxSize;
        this.onEvictCallback = onEvictCallback;
    }

    public void setMaxSize(int maxSize) {
        this.maxSize = maxSize;
    }

    @Override
    public boolean add(E element) {
        if (maxSize > 0) {
            evictExcessElements();
            return super.add(element);
        } else {
            return false;
        }
    }

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

    @Override
    public boolean offer(E element) {
        if (maxSize > 0) {
            evictExcessElements();
            return super.offer(element);
        } else {
            return false;
        }
    }

    private void evictExcessElements() {
        if (onEvictCallback == null) {
            while (size() >= maxSize) {
                super.poll();
            }
        } else {
            while (size() >= maxSize) {
                onEvictCallback.accept(super.poll());
            }
        }
    }

    public void clear() {
        if (onEvictCallback == null) {
            super.clear();
        } else {
            while (size() > 0) {
                onEvictCallback.accept(super.poll());
            }
        }
    }

    /**
     * @deprecated use removeItem instead
     */
    @Deprecated
    public boolean remove(Object o) {
        return super.remove(o);
    }

    public boolean removeItem(E e) {
        if (onEvictCallback != null) {
            onEvictCallback.accept(e);
        }
        return super.remove(e);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy