private class LimitedYieldingAccumulator<OutType, T> extends DelegatingYieldingAccumulator<OutType, T> { int count; boolean interruptYield = false; LimitedYieldingAccumulator(YieldingAccumulator<OutType, T> accumulator) { super(accumulator); count = 0; } @Override public OutType accumulate(OutType accumulated, T in) { ++count; if (!withinThreshold()) { // yield to interrupt the sequence interruptYield = true; } // if delegate yields as well we need to distinguish between the two yields final OutType retVal = super.accumulate(accumulated, in); if (yielded() && interruptYield) { interruptYield = false; } if (interruptYield) { yield(); }
@Override public <OutType> Yielder<OutType> toYielder(OutType initValue, YieldingAccumulator<OutType, T> accumulator) { final LimitedYieldingAccumulator<OutType, T> limitedAccumulator = new LimitedYieldingAccumulator<>( accumulator ); final Yielder<OutType> subYielder = baseSequence.toYielder(initValue, limitedAccumulator); return new LimitedYielder<>(subYielder, limitedAccumulator); }
private class LimitedYieldingAccumulator<OutType, T> extends DelegatingYieldingAccumulator<OutType, T> { int count; boolean interruptYield = false; LimitedYieldingAccumulator(YieldingAccumulator<OutType, T> accumulator) { super(accumulator); count = 0; } @Override public OutType accumulate(OutType accumulated, T in) { ++count; if (!withinThreshold()) { // yield to interrupt the sequence interruptYield = true; } // if delegate yields as well we need to distinguish between the two yields final OutType retVal = super.accumulate(accumulated, in); if (yielded() && interruptYield) { interruptYield = false; } if (interruptYield) { yield(); }
@Override public <OutType> Yielder<OutType> toYielder(OutType initValue, YieldingAccumulator<OutType, T> accumulator) { final LimitedYieldingAccumulator<OutType, T> limitedAccumulator = new LimitedYieldingAccumulator<>( accumulator ); final Yielder<OutType> subYielder = baseSequence.toYielder(initValue, limitedAccumulator); return new LimitedYielder<>(subYielder, limitedAccumulator); }