]> git.basschouten.com Git - openhab-addons.git/blob
c0e8cd18d96decf6a93b05228d30f6fb09fa3e12
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.ecovacs.internal.api.util;
14
15 import java.util.concurrent.Future;
16 import java.util.concurrent.ScheduledExecutorService;
17 import java.util.concurrent.TimeUnit;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.slf4j.Logger;
22
23 /**
24  * @author Danny Baumann - Initial contribution
25  */
26 @NonNullByDefault
27 public class SchedulerTask implements Runnable {
28     private final Logger logger;
29     private final String name;
30     private String prefixedName;
31     private final Runnable runnable;
32     private final ScheduledExecutorService scheduler;
33     private @Nullable Future<?> future;
34
35     public SchedulerTask(ScheduledExecutorService scheduler, Logger logger, String name, Runnable runnable) {
36         this.logger = logger;
37         this.name = name;
38         this.prefixedName = name;
39         this.runnable = runnable;
40         this.scheduler = scheduler;
41     }
42
43     public void setNamePrefix(String prefix) {
44         if (future != null) {
45             throw new IllegalStateException("Must not set prefix while scheduled");
46         }
47         if (prefix.isEmpty()) {
48             prefixedName = name;
49         } else {
50             prefixedName = prefix + ": " + name;
51         }
52     }
53
54     public void submit() {
55         schedule(0);
56     }
57
58     public synchronized void schedule(long delaySeconds) {
59         if (future != null) {
60             logger.trace("{}: Already scheduled to run", prefixedName);
61             return;
62         }
63         logger.trace("{}: Scheduling to run in {} seconds", prefixedName, delaySeconds);
64         if (delaySeconds == 0) {
65             future = scheduler.submit(this);
66         } else {
67             future = scheduler.schedule(this, delaySeconds, TimeUnit.SECONDS);
68         }
69     }
70
71     public synchronized void scheduleRecurring(long intervalSeconds) {
72         if (future != null) {
73             logger.trace("{}: Already scheduled to run", prefixedName);
74             return;
75         }
76         logger.trace("{}: Scheduling to run in {} second intervals", prefixedName, intervalSeconds);
77         future = scheduler.scheduleWithFixedDelay(runnable, 0, intervalSeconds, TimeUnit.SECONDS);
78     }
79
80     public synchronized void cancel() {
81         Future<?> future = this.future;
82         this.future = null;
83         if (future != null) {
84             future.cancel(true);
85             logger.trace("{}: Cancelled", prefixedName);
86         }
87     }
88
89     @Override
90     public void run() {
91         synchronized (this) {
92             future = null;
93         }
94         logger.trace("{}: Running one-shot", prefixedName);
95         runnable.run();
96     }
97 }