Skip to content

Commit

Permalink
Add Timer.isCancelled() method
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Tanagra <[email protected]>
  • Loading branch information
Jimmy Tanagra committed Nov 18, 2021
1 parent f9d201c commit d4cb926
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void testTimerIsActiveAndCancel() {
subject.cancel();
assertThat(subject.isActive(), is(false));
assertThat(subject.hasTerminated(), is(true));
assertThat(subject.isCancelled(), is(true));

subject.reschedule(ZonedDateTime.now().plusSeconds(DEFAULT_TIMEOUT_SECONDS));
assertThat(subject.isActive(), is(true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public interface Timer {
*/
public boolean isActive();

/**
* Determines whether the timer has been cancelled
*
* @return true, if the timer has been cancelled, false otherwise
*/
public boolean isCancelled();

/**
* Determines whether the scheduled code is currently executed.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ public class TimerImpl implements Timer {
private final SchedulerRunnable runnable;
private ScheduledCompletableFuture<Object> future;

private boolean cancelled;

public TimerImpl(Scheduler scheduler, ZonedDateTime startTime, SchedulerRunnable runnable) {
this.scheduler = scheduler;
this.startTime = startTime;
Expand All @@ -48,26 +46,29 @@ public TimerImpl(Scheduler scheduler, ZonedDateTime startTime, SchedulerRunnable

@Override
public boolean cancel() {
cancelled = true;
return future.cancel(true);
}

@Override
public boolean reschedule(ZonedDateTime newTime) {
future.cancel(false);
cancelled = false;
future = scheduler.schedule(runnable, newTime.toInstant());
return true;
}

@Override
public ZonedDateTime getExecutionTime() {
return cancelled ? null : ZonedDateTime.now().plusNanos(future.getDelay(TimeUnit.NANOSECONDS));
return future.isCancelled() ? null : ZonedDateTime.now().plusNanos(future.getDelay(TimeUnit.NANOSECONDS));
}

@Override
public boolean isActive() {
return !future.isDone() && !cancelled;
return !future.isDone();
}

@Override
public boolean isCancelled() {
return future.isCancelled();
}

@Override
Expand Down

0 comments on commit d4cb926

Please sign in to comment.