Skip to content

Instantly share code, notes, and snippets.

@SergejIsbrecht
Created January 25, 2021 23:14
Show Gist options
  • Save SergejIsbrecht/2a9d71e781c6f35d7b74e9ae2bc0c6ef to your computer and use it in GitHub Desktop.
Save SergejIsbrecht/2a9d71e781c6f35d7b74e9ae2bc0c6ef to your computer and use it in GitHub Desktop.
Wrap SingleScheduler
final class SingleSchedulerWrapper extends Scheduler {
private final Scheduler delegate;
SingleSchedulerWrapper(Scheduler delegate) {
this.delegate = Objects.requireNonNull(delegate, "delegate");
}
@Override
public long now(@NonNull TimeUnit unit) {
return System.nanoTime();
}
@Override
public @NonNull Worker createWorker() {
return new SingleWorkerWrapper(delegate.createWorker());
}
@Override
public void start() {
delegate.start();
}
@Override
public void shutdown() {
delegate.shutdown();
}
@Override
public @NonNull Disposable schedulePeriodicallyDirect(
@NonNull Runnable run, long initialDelay, long period, @NonNull TimeUnit unit) {
return delegate.schedulePeriodicallyDirect(run, initialDelay, period, unit);
}
@Override
public @NonNull Disposable scheduleDirect(@NonNull Runnable run) {
return delegate.scheduleDirect(run);
}
@Override
public @NonNull Disposable scheduleDirect(
@NonNull Runnable run, long delay, @NonNull TimeUnit unit) {
return delegate.scheduleDirect(run, delay, unit);
}
}
final class SingleWorkerWrapper extends Scheduler.Worker {
private final Scheduler.Worker delegate;
SingleWorkerWrapper(Scheduler.Worker delegate) {
this.delegate = Objects.requireNonNull(delegate, "delegate");
}
@Override
public long now(@NonNull TimeUnit unit) {
return System.nanoTime();
}
@Override
public @NonNull Disposable schedule(@NonNull Runnable run, long delay, @NonNull TimeUnit unit) {
return delegate.schedule(run, delay, unit);
}
@Override
public @NonNull Disposable schedule(@NonNull Runnable run) {
return delegate.schedule(run);
}
// do NOT delegate `schedulePeriodically`, because it calls it's `now` not the overloaded `now`
// from this worker impl.
// public Disposable schedulePeriodically(@NonNull Runnable run, final long initialDelay, final
// long period, @NonNull final TimeUnit unit)
@Override
public void dispose() {
delegate.dispose();
}
@Override
public boolean isDisposed() {
return delegate.isDisposed();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment