}
}
@Override
public Event next() {
Event nextEvent = mIter.next();
try {
switch (InMemStreamSymbol.this.mLatencyPolicy) {
case None:
break; // No delay required.
case Timestamp:
// Sleep based on the timestamps of the nextEvent and the prev event.
Thread.sleep(getNapTime(nextEvent));
break;
case MildLag:
// Add up to 40 ms of lag.
Thread.sleep(getNapTime(nextEvent) + mRandom.nextInt(40));
break;
case HeavyLag:
// Add up to 500 ms of lag.
Thread.sleep(getNapTime(nextEvent) + mRandom.nextInt(500));
break;
case Avalanche:
if (mAvalanched) {
// Avalanche already occurred; dump data as fast as possible.
break;
} else if (causeAvalanche()) {
// We just triggered an avalanche. Sleep two full seconds.
Thread.sleep(2000);
} else {
// Avalanche has not yet happened. Return things in 'real time.'
Thread.sleep(getNapTime(nextEvent));
}
break;
default:
throw new RuntimeException("Unexpected latency policy: " + mLatencyPolicy);
}
} catch (InterruptedException ie) {
// ignore any interrupt in here; just return the next event
// quickly to the upstream caller.
}
mPrevTimestamp = nextEvent.getTimestamp();
mPrevReturnTime = System.currentTimeMillis();
return nextEvent;
}