keith-turner commented on code in PR #4784: URL: https://github.com/apache/accumulo/pull/4784#discussion_r1702008318
########## core/src/main/java/org/apache/accumulo/core/util/Timer.java: ########## @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.accumulo.core.util; + +import java.time.Duration; + +/** + * This class provides a timer for measuring elapsed time. + */ +public final class Timer { + + private long startNanos; + private long accumulatedNanos; + private boolean isStarted; + + private Timer() { + this.accumulatedNanos = 0; + this.isStarted = false; + } + + /** + * Creates and starts a new Timer instance. + * + * @return a new Timer instance that is already started. + */ + public static Timer startNew() { + Timer timer = new Timer(); + timer.start(); + return timer; + } + + /** + * Starts the timer if it is not already running. + * + * @throws IllegalStateException if the timer is already running. + */ + public void start() throws IllegalStateException { + if (isStarted) { + throw new IllegalStateException("Timer is already running"); + } + startNanos = System.nanoTime(); + isStarted = true; + } + + /** + * Stops the timer if it is running and accumulates the elapsed time. + * + * @throws IllegalStateException if the timer is not running. + */ + public void stop() throws IllegalStateException { Review Comment: Yeah, if its not being used currently and the potential use case you mentioned could be satisfied w/ multiple timers then it seems dropping it would be good for now. Also its internal code, so we can always adapt it easily to future use cases. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
