From 65e216657e4ac86d4e1a1bee30033d9b27f5f217 Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 19 Aug 2021 20:50:08 +0100 Subject: Abstract tick reporter --- .../spark/common/tick/SimpleTickReporter.java | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 spark-common/src/main/java/me/lucko/spark/common/tick/SimpleTickReporter.java (limited to 'spark-common/src') diff --git a/spark-common/src/main/java/me/lucko/spark/common/tick/SimpleTickReporter.java b/spark-common/src/main/java/me/lucko/spark/common/tick/SimpleTickReporter.java new file mode 100644 index 0000000..9747784 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/tick/SimpleTickReporter.java @@ -0,0 +1,48 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.lucko.spark.common.tick; + +public abstract class SimpleTickReporter extends AbstractTickReporter { + private boolean closed = false; + private long start = 0; + + protected void onStart() { + if (this.closed) { + return; + } + + this.start = System.nanoTime(); + } + + protected void onEnd() { + if (this.closed || this.start == 0) { + return; + } + + double duration = (System.nanoTime() - this.start) / 1000000d; + onTick(duration); + } + + @Override + public void close() { + this.closed = true; + } +} -- cgit