]> git.basschouten.com Git - openhab-addons.git/blob
f7ffa0927c58241d63dd56dae41fc4053398ebd1
[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.elerotransmitterstick.internal.stick;
14
15 import java.util.concurrent.Delayed;
16 import java.util.concurrent.TimeUnit;
17
18 /**
19  * @author Volker Bier - Initial contribution
20  */
21 public class DelayedCommand extends Command {
22     private final long origin;
23     private final long delay;
24
25     public DelayedCommand(CommandType cmd, long delayInMillis, int priority, Integer... channels) {
26         super(cmd, priority, channels);
27
28         delay = delayInMillis;
29         origin = System.currentTimeMillis();
30     }
31
32     @Override
33     @SuppressWarnings("PMD.CompareObjectsWithEquals")
34     public int compareTo(Delayed delayed) {
35         if (delayed == this) {
36             return 0;
37         }
38
39         return Long.compare(getDelay(TimeUnit.MILLISECONDS), delayed.getDelay(TimeUnit.MILLISECONDS));
40     }
41
42     @Override
43     public long getDelay(TimeUnit unit) {
44         return unit.convert(delay - (System.currentTimeMillis() - origin), TimeUnit.MILLISECONDS);
45     }
46
47     @Override
48     public String toString() {
49         return super.toString() + " and delay " + getDelay(TimeUnit.MILLISECONDS);
50     }
51 }