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

org.rx.bean.CircularBlockingQueue Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package org.rx.bean;

import lombok.Getter;
import org.rx.core.*;
import org.rx.util.function.TripleFunc;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.locks.ReentrantLock;

import static org.rx.core.Extends.ifNull;

public class CircularBlockingQueue extends LinkedBlockingQueue implements EventPublisher> {
    private static final long serialVersionUID = 4685018531330571106L;
    public final Delegate, NEventArgs> onConsume = Delegate.create();
    public TripleFunc, T, Boolean> onFull;
    final ReentrantLock pLock = Reflects.readField(this, "putLock");
    TimeoutFuture consumeTimer;
    @Getter
    long consumePeriod;

    public void setCapacity(int capacity) {
        if (capacity <= 0) throw new IllegalArgumentException();
        Reflects.writeField(this, "capacity", capacity);
    }

    public int getCapacity() {
        return Reflects.readField(this, "capacity");
    }

    public synchronized void setConsumePeriod(long consumePeriod) {
        if ((this.consumePeriod = consumePeriod) > 0) {
            if (consumeTimer != null) {
                consumeTimer.cancel();
            }
            consumeTimer = Tasks.timer().setTimeout(() -> {
                T t;
                NEventArgs e = new NEventArgs<>();
                while ((t = poll()) != null) {
                    e.setValue(t);
                    raiseEvent(onConsume, e);
                }
            }, d -> consumePeriod, null, Constants.TIMER_PERIOD_FLAG);
        } else {
            if (consumeTimer != null) {
                consumeTimer.cancel();
            }
        }
    }

    public CircularBlockingQueue(int capacity) {
        this(capacity, null);
        onFull = (q, t) -> {
            pLock.lock();
            try {
                boolean ok;
                do {
                    q.poll();
                    ok = q.innerOffer(t);
                }
                while (!ok);
                return true;
            } finally {
                pLock.unlock();
            }
        };
    }

    public CircularBlockingQueue(int capacity, TripleFunc, T, Boolean> onFull) {
        super(capacity);
        this.onFull = onFull;
    }

    //Full会抛异常
//        @Override
//        public boolean add(T t) {
//            return offer(t);
//        }

    @Override
    public boolean offer(T t) {
        boolean r = super.offer(t);
        if (!r && onFull != null) {
            return ifNull(onFull.apply(this, t), false);
        }
        return r;
    }

    protected boolean innerOffer(T t) {
        return super.offer(t);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy