package reactor.util.concurrent;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import reactor.util.annotation.Nullable;
public final class Queues {
public static final int CAPACITY_UNSURE = Integer.MIN_VALUE;
public static final int capacity(Queue q) {
if (q instanceof ZeroQueue) {
return 0;
}
if (q instanceof OneQueue) {
return 1;
}
if (q instanceof SpscLinkedArrayQueue) {
return Integer.MAX_VALUE;
}
else if (q instanceof SpscArrayQueue) {
return ((SpscArrayQueue) q).length();
}
else if(q instanceof MpscLinkedQueue) {
return Integer.MAX_VALUE;
}
else if (q instanceof BlockingQueue) {
return ((BlockingQueue) q).remainingCapacity();
}
else if (q instanceof ConcurrentLinkedQueue) {
return Integer.MAX_VALUE;
}
else {
return CAPACITY_UNSURE;
}
}
public static final int XS_BUFFER_SIZE = Math.max(8,
Integer.parseInt(System.getProperty("reactor.bufferSize.x", "32")));
public static final int SMALL_BUFFER_SIZE = Math.max(16,
Integer.parseInt(System.getProperty("reactor.bufferSize.small", "256")));
public static int ceilingNextPowerOfTwo(final int x) {
return 1 << (32 - Integer.numberOfLeadingZeros(x - 1));
}
@SuppressWarnings("unchecked")
public static <T> Supplier<Queue<T>> get(int batchSize) {
if (batchSize == Integer.MAX_VALUE) {
return SMALL_UNBOUNDED;
}
if (batchSize == XS_BUFFER_SIZE) {
return XS_SUPPLIER;
}
if (batchSize == SMALL_BUFFER_SIZE) {
return SMALL_SUPPLIER;
}
if (batchSize == 1) {
return ONE_SUPPLIER;
}
if (batchSize == 0) {
return ZERO_SUPPLIER;
}
final int adjustedBatchSize = Math.max(8, batchSize);
if (adjustedBatchSize > 10_000_000) {
return SMALL_UNBOUNDED;
}
else{
return () -> new SpscArrayQueue<>(adjustedBatchSize);
}
}
public static boolean isPowerOfTwo(final int x) {
return Integer.bitCount(x) == 1;
}
@SuppressWarnings("unchecked")
public static <T> Supplier<Queue<T>> empty() {
return ZERO_SUPPLIER;
}
@SuppressWarnings("unchecked")
public static <T> Supplier<Queue<T>> one() {
return ONE_SUPPLIER;
}
@SuppressWarnings("unchecked")
public static <T> Supplier<Queue<T>> small() {
return SMALL_SUPPLIER;
}
@SuppressWarnings("unchecked")
public static <T> Supplier<Queue<T>> unbounded() {
return SMALL_UNBOUNDED;
}
@SuppressWarnings("unchecked")
public static <T> Supplier<Queue<T>> unbounded(int linkSize) {
if (linkSize == XS_BUFFER_SIZE) {
return XS_UNBOUNDED;
}
else if (linkSize == Integer.MAX_VALUE || linkSize == SMALL_BUFFER_SIZE) {
return unbounded();
}
return () -> new SpscLinkedArrayQueue<>(linkSize);
}
@SuppressWarnings("unchecked")
public static <T> Supplier<Queue<T>> xs() {
return XS_SUPPLIER;
}
public static <T> Supplier<Queue<T>> unboundedMultiproducer() {
return MpscLinkedQueue::new;
}
private Queues() {
}
static final class OneQueue<T> extends AtomicReference<T> implements Queue<T> {
@Override
public boolean add(T t) {
while (!offer(t));
return true;
}
@Override
public boolean addAll(Collection<? extends T> c) {
return false;
}
@Override
public void clear() {
set(null);
}
@Override
public boolean contains(Object o) {
return Objects.equals(get(), o);
}
@Override
public boolean containsAll(Collection<?> c) {
return false;
}
@Override
public T element() {
return get();
}
@Override
public boolean isEmpty() {
return get() == null;
}
@Override
public Iterator<T> iterator() {
return new QueueIterator<>(this);
}
@Override
public boolean offer(T t) {
if (get() != null) {
return false;
}
lazySet(t);
return true;
}
@Override
@Nullable
public T peek() {
return get();
}
@Override
@Nullable
public T poll() {
T v = get();
if (v != null) {
lazySet(null);
}
return v;
}
@Override
public T remove() {
return getAndSet(null);
}
@Override
public boolean remove(Object o) {
return false;
}
@Override
public boolean removeAll(Collection<?> c) {
return false;
}
@Override
public boolean retainAll(Collection<?> c) {
return false;
}
@Override
public int size() {
return get() == null ? 0 : 1;
}
@Override
public Object[] toArray() {
T t = get();
if (t == null) {
return new Object[0];
}
return new Object[]{t};
}
@Override
@SuppressWarnings("unchecked")
public <T1> T1[] toArray(T1[] a) {
int size = size();
if (a.length < size) {
a = (T1[]) java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), size);
}
if (size == 1) {
a[0] = (T1) get();
}
if (a.length > size) {
a[size] = null;
}
return a;
}
private static final long serialVersionUID = -6079491923525372331L;
}
static final class ZeroQueue<T> implements Queue<T>, Serializable {
@Override
public boolean add(T t) {
return false;
}
@Override
public boolean addAll(Collection<? extends T> c) {
return false;
}
@Override
public void clear() {
}
@Override
public boolean contains(Object o) {
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
return false;
}
@Override
public T element() {
throw new NoSuchElementException("immutable empty queue");
}
@Override
public boolean isEmpty() {
return true;
}
@Override
public Iterator<T> iterator() {
return Collections.emptyIterator();
}
@Override
public boolean offer(T t) {
return false;
}
@Override
@Nullable
public T peek() {
return null;
}
@Override
@Nullable
public T poll() {
return null;
}
@Override
public T remove() {
throw new NoSuchElementException("immutable empty queue");
}
@Override
public boolean remove(Object o) {
return false;
}
@Override
public boolean removeAll(Collection<?> c) {
return false;
}
@Override
public boolean retainAll(Collection<?> c) {
return false;
}
@Override
public int size() {
return 0;
}
@Override
public Object[] toArray() {
return new Object[0];
}
@Override
@SuppressWarnings("unchecked")
public <T1> T1[] toArray(T1[] a) {
if (a.length > 0) {
a[0] = null;
}
return a;
}
private static final long serialVersionUID = -8876883675795156827L;
}
static final class QueueIterator<T> implements Iterator<T> {
final Queue<T> queue;
public QueueIterator(Queue<T> queue) {
this.queue = queue;
}
@Override
public boolean hasNext() {
return !queue.isEmpty();
}
@Override
public T next() {
return queue.poll();
}
@Override
public void remove() {
queue.remove();
}
}
@SuppressWarnings("rawtypes")
static final Supplier ZERO_SUPPLIER = ZeroQueue::new;
@SuppressWarnings("rawtypes")
static final Supplier ONE_SUPPLIER = OneQueue::new;
@SuppressWarnings("rawtypes")
static final Supplier XS_SUPPLIER = () -> new SpscArrayQueue<>(XS_BUFFER_SIZE);
@SuppressWarnings("rawtypes")
static final Supplier SMALL_SUPPLIER = () -> new SpscArrayQueue<>(SMALL_BUFFER_SIZE);
@SuppressWarnings("rawtypes")
static final Supplier SMALL_UNBOUNDED =
() -> new SpscLinkedArrayQueue<>(SMALL_BUFFER_SIZE);
@SuppressWarnings("rawtypes")
static final Supplier XS_UNBOUNDED = () -> new SpscLinkedArrayQueue<>(XS_BUFFER_SIZE);
}