|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.commons.lang3.time; |
| 19 | + |
| 20 | +/** |
| 21 | + * <p> |
| 22 | + * {@link SimpleStopWatch} provides a convenient API for timings. |
| 23 | + * </p> |
| 24 | + * <p> |
| 25 | + * {@link SimpleStopWatch} is simple alternative for {@link StopWatch}. It does not have state flow |
| 26 | + * and API is no-brainer. |
| 27 | + * </p> |
| 28 | + * <p> |
| 29 | + * {@link SimpleStopWatch} use {@link System#nanoTime()} for time measure. |
| 30 | + * </p> |
| 31 | +
|
| 32 | + * |
| 33 | + * <p> |
| 34 | + * To start the watch, call {@link SimpleStopWatch#started()}. |
| 35 | + * </p> |
| 36 | + * <p> |
| 37 | + * To get time in millis since start, call {@link #time()}. |
| 38 | + * </p> |
| 39 | + * <p> |
| 40 | + * {@link #lap()} returns time in millis since last {@link #lap()} call or since start |
| 41 | + * when {@link #lap()} not called yet. For example:<br> |
| 42 | + * 1. var stopWatch = SimpleStopWatch.started()<br> |
| 43 | + * 2. sleep (200 ms)<br> |
| 44 | + * 3. stopWatch.getTime() == 200, stopWatch.lap() == 200<br> |
| 45 | + * 4. sleep (100 ms)<br> |
| 46 | + * 5. stopWatch.getTime() == 300, stopWatch.lap() == 100<br> |
| 47 | +
|
| 48 | + * <p>This class is not thread-safe.</p> |
| 49 | + * |
| 50 | + * @since 3.12 |
| 51 | + */ |
| 52 | +public class SimpleStopWatch { |
| 53 | + |
| 54 | + |
| 55 | + private static final long NANO_2_MILLIS = 1000000L; |
| 56 | + |
| 57 | + /** |
| 58 | + * Creates a simple stop watch.<br> |
| 59 | + * To avoid doubts for people used to {@link StopWatch} method name explains |
| 60 | + * that SimpleStopWatch is started when created. |
| 61 | + * |
| 62 | + * @return SimpleStopWatch a stopwatch. |
| 63 | + */ |
| 64 | + public static SimpleStopWatch started() { |
| 65 | + return new SimpleStopWatch(); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + /** |
| 70 | + * The start time in nanoseconds. |
| 71 | + */ |
| 72 | + private long startTimeNanos; |
| 73 | + |
| 74 | + |
| 75 | + /** |
| 76 | + * The lap start time in nanoseconds. |
| 77 | + */ |
| 78 | + private long lapStartTimeNanos; |
| 79 | + |
| 80 | + |
| 81 | + private SimpleStopWatch() { |
| 82 | + this.startTimeNanos = System.nanoTime(); |
| 83 | + this.lapStartTimeNanos = startTimeNanos; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * <p> |
| 88 | + * Gets the time on the stopwatch. |
| 89 | + * </p> |
| 90 | + * |
| 91 | + * @return the time in milliseconds |
| 92 | + */ |
| 93 | + public long time() { |
| 94 | + return (System.nanoTime() - this.startTimeNanos) / NANO_2_MILLIS; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * <p> |
| 99 | + * Gets the time on the stopwatch since last ${lap} call or since start when ${lap} not called yet |
| 100 | + * </p> |
| 101 | + * |
| 102 | + * @return the time in milliseconds |
| 103 | + */ |
| 104 | + public long lap() { |
| 105 | + long lapTime = (System.nanoTime() - lapStartTimeNanos) / NANO_2_MILLIS; |
| 106 | + lapStartTimeNanos = System.nanoTime(); |
| 107 | + return lapTime; |
| 108 | + } |
| 109 | +} |
0 commit comments