]> git.basschouten.com Git - openhab-addons.git/blob
cfe19e77241208ff0244fcdeec01ea7540078aa7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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     public int compareTo(Delayed delayed) {
34         if (delayed == this) {
35             return 0;
36         }
37
38         return Long.compare(getDelay(TimeUnit.MILLISECONDS), delayed.getDelay(TimeUnit.MILLISECONDS));
39     }
40
41     @Override
42     public long getDelay(TimeUnit unit) {
43         return unit.convert(delay - (System.currentTimeMillis() - origin), TimeUnit.MILLISECONDS);
44     }
45
46     @Override
47     public String toString() {
48         return super.toString() + " and delay " + getDelay(TimeUnit.MILLISECONDS);
49     }
50 }