2 * Copyright (c) 2010-2020 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.io.transport.modbus.internal;
15 import java.util.function.Consumer;
16 import java.util.function.Supplier;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.io.transport.modbus.internal.ModbusManagerImpl.PollTaskUnregistered;
21 import net.wimpi.modbus.ModbusException;
24 * Implementation of simple stop watch.
26 * @author Sami Salonen - initial contribution
30 public class SimpleStopWatch {
32 private volatile long totalMillis;
33 private volatile long resumed;
36 public abstract interface SupplierWithPollTaskUnregisteredException<T> {
37 public abstract T get() throws ModbusManagerImpl.PollTaskUnregistered;
41 public abstract interface RunnableWithModbusException {
42 public abstract void run() throws ModbusException;
46 * Resume or start the stop watch
48 * @throws IllegalStateException if stop watch is running already
50 public synchronized void resume() {
52 throw new IllegalStateException("Cannot suspend a running StopWatch");
54 resumed = System.currentTimeMillis();
58 * Suspend the stop watch
60 * @throws IllegalStateException if stop watch has not been resumed
62 public synchronized void suspend() {
64 throw new IllegalStateException("Cannot suspend non-running StopWatch");
66 totalMillis += System.currentTimeMillis() - resumed;
71 * Get total running time of this StopWatch in milliseconds
73 * @return total running time in milliseconds
75 public synchronized long getTotalTimeMillis() {
80 * Tells whether this StopWatch is now running
82 * @return boolean telling whether this StopWatch is running
84 public synchronized boolean isRunning() {
89 * Time single action using this StopWatch
91 * First StopWatch is resumed, then action is applied. Finally the StopWatch is suspended.
93 * @param supplier action to time
94 * @return return value from supplier
95 * @throws PollTaskUnregistered when original supplier throws the exception
97 public <R> R timeSupplierWithPollTaskUnregisteredException(SupplierWithPollTaskUnregisteredException<R> supplier)
98 throws PollTaskUnregistered {
101 return supplier.get();
108 * Time single action using this StopWatch
110 * First StopWatch is resumed, then action is applied. Finally the StopWatch is suspended.
112 * @param supplier action to time
113 * @return return value from supplier
115 public <R> R timeSupplier(Supplier<R> supplier) {
118 return supplier.get();
125 * Time single action using this StopWatch
127 * First StopWatch is resumed, then action is applied. Finally the StopWatch is suspended.
129 * @param action action to time
130 * @throws ModbusException when original action throws the exception
132 public void timeRunnableWithModbusException(RunnableWithModbusException action) throws ModbusException {
142 * Time single action using this StopWatch
144 * First StopWatch is resumed, then action is applied. Finally the StopWatch is suspended.
146 * @param supplier action to time
147 * @return return value from supplier
149 public void timeRunnable(Runnable runnable) {
159 * Time single action using this StopWatch
161 * First StopWatch is resumed, then action is applied. Finally the StopWatch is suspended.
163 * @param consumer action to time
164 * @return return value from supplier
166 public <T> void timeConsumer(Consumer<T> consumer, T parameter) {
169 consumer.accept(parameter);