2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.elerotransmitterstick.internal.stick;
15 import java.util.concurrent.Delayed;
16 import java.util.concurrent.TimeUnit;
19 * @author Volker Bier - Initial contribution
21 public class DelayedCommand extends Command {
22 private final long origin;
23 private final long delay;
25 public DelayedCommand(CommandType cmd, long delayInMillis, int priority, Integer... channels) {
26 super(cmd, priority, channels);
28 delay = delayInMillis;
29 origin = System.currentTimeMillis();
33 @SuppressWarnings("PMD.CompareObjectsWithEquals")
34 public int compareTo(Delayed delayed) {
35 if (delayed == this) {
39 return Long.compare(getDelay(TimeUnit.MILLISECONDS), delayed.getDelay(TimeUnit.MILLISECONDS));
43 public long getDelay(TimeUnit unit) {
44 return unit.convert(delay - (System.currentTimeMillis() - origin), TimeUnit.MILLISECONDS);
48 public String toString() {
49 return super.toString() + " and delay " + getDelay(TimeUnit.MILLISECONDS);