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

org.kiwiproject.collect.KiwiEvictingQueues Maven / Gradle / Ivy

Go to download

Kiwi is a utility library. We really like Google's Guava, and also use Apache Commons. But if they don't have something we need, and we think it is useful, this is where we put it.

There is a newer version: 4.5.2
Show newest version
package org.kiwiproject.collect;

import com.google.common.collect.EvictingQueue;
import com.google.common.collect.Queues;
import lombok.experimental.UtilityClass;

import java.util.Queue;

/**
 * Utility methods for working with Guava {@link EvictingQueue} instances.
 */
@SuppressWarnings("UnstableApiUsage")
@UtilityClass
public class KiwiEvictingQueues {

    /**
     * The default maximum number of {@link EvictingQueue} items.
     *
     * @see #synchronizedEvictingQueue()
     */
    public static final int DEFAULT_MAX_RECENT_ITEMS = 100;

    /**
     * Create a new, synchronized {@link EvictingQueue} that can hold up to {@link #DEFAULT_MAX_RECENT_ITEMS} items.
     *
     * @param  the type in the queue
     * @return a synchronized {@link EvictingQueue}
     * @apiNote returns a plain {@link Queue} because Guava's {@link Queues#synchronizedQueue(Queue)} returns a {@link Queue}.
     * Any attempt to cast to an {@link EvictingQueue} will result in a {@link ClassCastException}.
     * @implNote See synchronized notes regarding manual synchronization of the returned queue's {@link java.util.Iterator}
     * in {@link Queues#synchronizedQueue(Queue)}
     * @see Queues#synchronizedQueue(Queue)
     */
    public static  Queue synchronizedEvictingQueue() {
        return synchronizedEvictingQueue(DEFAULT_MAX_RECENT_ITEMS);
    }

    /**
     * Create a new, synchronized {@link EvictingQueue} that can hold up to {@code maxSize} items.
     *
     * @param maxSize maximum size for the queue
     * @param      the type in the queue
     * @return a synchronized {@link EvictingQueue}
     * @apiNote returns a plain {@link Queue} because Guava's {@link Queues#synchronizedQueue(Queue)} returns a {@link Queue}.
     * Any attempt to cast to an {@link EvictingQueue} will result in a {@link ClassCastException}.
     * @implNote See synchronized notes regarding manual synchronization of the returned queue's {@link java.util.Iterator}
     * in {@link Queues#synchronizedQueue(Queue)}
     * @see Queues#synchronizedQueue(Queue)
     */
    public static  Queue synchronizedEvictingQueue(int maxSize) {
        return Queues.synchronizedQueue(EvictingQueue.create(maxSize));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy