- * Based on SerialExecutor https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executor.html - * but modified to represent a queue of size one which is replaced by the latest call to {@link #execute(Runnable)}. - */ - private static final class SerialLiveDataExecutor implements Executor { - private final Executor executor; - private Runnable next; - private Runnable active; - - SerialLiveDataExecutor(@NonNull Executor executor) { - this.executor = executor; - } - - public synchronized void execute(@NonNull Runnable command) { - next = () -> { - try { - command.run(); - } finally { - scheduleNext(); - } - }; - - if (active == null) { - scheduleNext(); - } - } - - private synchronized void scheduleNext() { - active = next; - next = null; - if (active != null) { - executor.execute(active); - } - } - } }