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

pers.clare.polarbearcache.event.EventSenderQueue Maven / Gradle / Ivy

There is a newer version: 1.2.3-RELEASE
Show newest version
package pers.clare.polarbearcache.event;

import pers.clare.polarbearcache.support.VolatileSenderQueue;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;

public class EventSenderQueue {

    private final VolatileSenderQueue senderQueue = new VolatileSenderQueue<>(5000);

    private final ConcurrentMap> clearSenderQueue = new ConcurrentHashMap<>(16);

    private final ConcurrentMap>> evictBlocks = new ConcurrentHashMap<>(16);

    public T poll() {
        return senderQueue.poll();
    }

    public T poll(String name) {
        return getClearBlock(name).poll();
    }

    public T poll(String name, String key) {
        return getEvictBlock(name, key).poll();
    }

    public void add(T t) {
        senderQueue.add(t);
    }

    public void add(T t, String name) {
        getClearBlock(name).add(t);
    }

    public void add(T t, String name, String key) {
        getEvictBlock(name, key).add(t);
    }

    private final Function>> createConcurrentHashMap = (k) -> new ConcurrentHashMap<>();

    private final Function> createVolatileBlock = (k) -> new VolatileSenderQueue<>(5000);

    private VolatileSenderQueue getClearBlock(String name) {
        return clearSenderQueue.computeIfAbsent(name, createVolatileBlock);
    }

    private VolatileSenderQueue getEvictBlock(String name, String key) {
        return evictBlocks
                .computeIfAbsent(name, createConcurrentHashMap)
                .computeIfAbsent(key, createVolatileBlock);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy