So as it goes in the current scenario, we have a set of APIs as listed below:
Consumer<T> start();Consumer<T> performDailyAggregates();Consumer<T> performLastNDaysAggregates();Consumer<T> repopulateScores();Consumer<T> updateDataStore();
Over these, one of our schedulers performs the tasks e.g.
private void performAllTasks(T data) { start().andThen(performDailyAggregates()) .andThen(performLastNDaysAggregates()) .andThen(repopulateScores()) .andThen(updateDataStore()) .accept(data);}
While reviewing this, I thought of moving to a more flexible implementation 1 of performing tasks which would look like:
// NOOP in the context further stands for 'anything -> {}'private void performAllTasks(Stream<Consumer<T>> consumerList, T data) { consumerList.reduce(NOOP, Consumer::andThen).accept(data);}
The point that strikes my mind now is that the Javadoc clearly states that
accumulator
- an associative, non-interfering, stateless function for combining two values
Next up I was thinking How to ensure order of processing in java8 streams? to be ordered (processing order to be same as encounter order)!
Okay, the stream generated out of a List
would be ordered and unless the stream is made parallel
before reduce
the following implementation shall work. 2
private void performAllTasks(List<Consumer<T>> consumerList, T data) { consumerList.stream().reduce(NOOP, Consumer::andThen).accept(data);}
Q. Does this assumption 2 hold true? Would it be guaranteed to always execute the consumers in the order that the original code had them?
Q. Is there a possibility somehow to expose 1 as well to the callees to perform tasks?