From a4b737c40162a01bd8c5b35a5d15b2f83680079c Mon Sep 17 00:00:00 2001 From: jimtng <2554958+jimtng@users.noreply.github.com> Date: Wed, 24 Nov 2021 06:51:51 +1000 Subject: [PATCH] Add Timer.isCancelled() method (#2570) Signed-off-by: Jimmy Tanagra --- .../script/internal/actions/TimerImplTest.java | 11 +++++++++++ .../openhab/core/model/script/actions/Timer.java | 7 +++++++ .../model/script/internal/actions/TimerImpl.java | 13 +++++++------ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.core.model.script/src.moved/test/java/org/openhab/core/model/script/internal/actions/TimerImplTest.java b/bundles/org.openhab.core.model.script/src.moved/test/java/org/openhab/core/model/script/internal/actions/TimerImplTest.java index d0ad6f07fd1..5e925845b23 100644 --- a/bundles/org.openhab.core.model.script/src.moved/test/java/org/openhab/core/model/script/internal/actions/TimerImplTest.java +++ b/bundles/org.openhab.core.model.script/src.moved/test/java/org/openhab/core/model/script/internal/actions/TimerImplTest.java @@ -52,24 +52,29 @@ public void setUp() { public void testTimerIsActiveAndCancel() { assertThat(subject.isActive(), is(true)); assertThat(subject.hasTerminated(), is(false)); + assertThat(subject.isCancelled(), is(false)); 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)); assertThat(subject.hasTerminated(), is(false)); + assertThat(subject.isCancelled(), is(false)); } @Test public void testTimerIsActiveAndTerminate() throws InterruptedException { assertThat(subject.isActive(), is(true)); assertThat(subject.hasTerminated(), is(false)); + assertThat(subject.isCancelled(), is(false)); Thread.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_TIMEOUT_SECONDS + DEFAULT_RUNTIME_SECONDS + 1)); assertThat(subject.isActive(), is(false)); assertThat(subject.hasTerminated(), is(true)); + assertThat(subject.isCancelled(), is(false)); } @Test @@ -77,28 +82,34 @@ public void testTimerHasTerminatedAndReschedule() throws InterruptedException { Thread.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_TIMEOUT_SECONDS + DEFAULT_RUNTIME_SECONDS + 1)); assertThat(subject.isActive(), is(false)); assertThat(subject.hasTerminated(), is(true)); + assertThat(subject.isCancelled(), is(false)); subject.reschedule(ZonedDateTime.now().plusSeconds(DEFAULT_TIMEOUT_SECONDS)); assertThat(subject.isActive(), is(true)); assertThat(subject.hasTerminated(), is(false)); + assertThat(subject.isCancelled(), is(false)); Thread.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_TIMEOUT_SECONDS + DEFAULT_RUNTIME_SECONDS + 1)); assertThat(subject.isActive(), is(false)); assertThat(subject.hasTerminated(), is(true)); + assertThat(subject.isCancelled(), is(false)); } @Test public void testTimerIsRunning() throws InterruptedException { assertThat(subject.isRunning(), is(false)); assertThat(subject.hasTerminated(), is(false)); + assertThat(subject.isCancelled(), is(false)); Thread.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_TIMEOUT_SECONDS) + 500); assertThat(subject.isRunning(), is(true)); assertThat(subject.hasTerminated(), is(false)); + assertThat(subject.isCancelled(), is(false)); Thread.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_RUNTIME_SECONDS + 1)); assertThat(subject.isRunning(), is(false)); assertThat(subject.hasTerminated(), is(true)); + assertThat(subject.isCancelled(), is(false)); } private Timer createTimer(ZonedDateTime instant, SchedulerRunnable runnable) { diff --git a/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/actions/Timer.java b/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/actions/Timer.java index 56d28015505..8bd94382266 100644 --- a/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/actions/Timer.java +++ b/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/actions/Timer.java @@ -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. * diff --git a/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/internal/actions/TimerImpl.java b/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/internal/actions/TimerImpl.java index 89e83900ae5..1d7e1201475 100644 --- a/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/internal/actions/TimerImpl.java +++ b/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/internal/actions/TimerImpl.java @@ -36,8 +36,6 @@ public class TimerImpl implements Timer { private final SchedulerRunnable runnable; private ScheduledCompletableFuture future; - private boolean cancelled; - public TimerImpl(Scheduler scheduler, ZonedDateTime startTime, SchedulerRunnable runnable) { this.scheduler = scheduler; this.startTime = startTime; @@ -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