public TransferFiber( final FibersExecutor executor, final Input<T> input, final Output<T> output, final int capacity ) { this(executor, new CircularArrayBuffer<>(capacity), input, output); }
@Override public final CircularArrayBuffer<E> removeFirst(final int n) { if(size() < n) { throw new IndexOutOfBoundsException(); } else if(size() == n) { clear(); } else { offset = increaseIndex(offset, n); } return this; }
@Override public final boolean add(final E e) { if(isEmpty()) { array[offset] = e; end = increaseIndex(offset, 1); return true; } else if(size() < capacity) { array[end] = e; end = increaseIndex(end, 1); return true; } else { return false; } }
@Override public E get(final int i) { if(isEmpty()) { throw new IndexOutOfBoundsException(); } return array[translateIndex(i)]; }
@Override public final E remove(final int i) { if(isEmpty()) { throw new IndexOutOfBoundsException(Integer.toString(i)); } final E e; if(size() == 1) { e = array[offset]; clear(); } else { final int j = translateIndex(i); e = array[j]; if(j == offset) { offset = increaseIndex(offset, 1); } else if(end > 0 && j == end - 1) { end --; } else if(end == 0 && j == capacity - 1) { end = capacity - 1; } else { throw new UnsupportedOperationException("Able to remove only from the beginning either end"); } } return e; }
@Override public final int size() { if(isEmpty()) { return 0; } else if(offset < end) { return end - offset; } else { return capacity + end - offset; } }
private int translateIndex(final int i) throws IndexOutOfBoundsException { if(i < size()) { return (offset + i) % capacity; } else { throw new IndexOutOfBoundsException(Integer.toString(i)); } }
@Override public E set(final int i, final E e) { if(isEmpty()) { throw new IndexOutOfBoundsException(Integer.toString(i)); } final int j = translateIndex(i); final E prev = array[j]; array[j] = e; return prev; }
@Override public final boolean addAll(final Collection<? extends E> elements) { final int elementsCount = elements.size(); if(capacity - size() < elementsCount) { return false; } else { for(final E e: elements) { if(isEmpty()) { array[offset] = e; end = increaseIndex(offset, 1); } else if(size() < capacity) { array[end] = e; end = increaseIndex(end, 1); } } return true; } }
public RoundRobinOutputFiber(final FibersExecutor executor, final List<O> outputs, final int buffCapacity) { super(executor); this.outputs = outputs; this.outputsCount = outputs.size(); this.buffCapacity = buffCapacity; this.buffs = new HashMap<>(this.outputsCount); this.buffLocks = new HashMap<>(this.outputsCount); for(int i = 0; i < this.outputsCount; i ++) { final CircularBuffer<T> buff = new CircularArrayBuffer<>(buffCapacity); this.buffs.put(outputs.get(i), buff); this.buffLocks.put(outputs.get(i), new ReentrantLock()); } }
+ (countLimit > 0 && countLimit < Long.MAX_VALUE ? Long.toString(countLimit) : "") + itemInput.toString(); threadLocalOpBuff = ThreadLocal.withInitial(() -> new CircularArrayBuffer<>(batchSize));