File: Timer.java

package info (click to toggle)
libjaba-client-java 0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 368 kB
  • ctags: 551
  • sloc: java: 3,332; makefile: 11
file content (199 lines) | stat: -rw-r--r-- 5,851 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * @(#)Timer.java 1.0 June 2010 Copyright (c) 2010 Peter Troshin JRONN version:
 * 3.1 This library is free software; you can redistribute it and/or modify it
 * under the terms of the Apache License version 2 as published by the Apache
 * Software Foundation This library 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 Apache License
 * for more details. A copy of the license is in apache_license.txt. It is also
 * available here: see: http://www.apache.org/licenses/LICENSE-2.0.txt Any
 * republication or derived work distributed in source code form must include
 * this copyright and license notice.
 */
package compbio.util;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.concurrent.TimeUnit;

/**
 * A simple timer, calculates the time interval between two events. Keeps two
 * counters, one for long time intervals, to measure time between the start and
 * end of the application for instance, and another for short events, to measure
 * how long it took to reach a next block of code. This class is immutable and
 * as such thread safe.
 * 
 * @author Peter Troshin
 * @version 1.0 September 2010
 * 
 */
public class Timer {

	private long checkPoint;
	private final long startTime;
	private TimeUnit reportTimeUnit;

	/*
	 * Place to write the statistics to, handy for the clients
	 */
	private PrintWriter statWriter;

	/**
	 * Constructs the new instance of Timer, initiate default time reporting
	 * unit to seconds.
	 */
	public Timer() {
		startTime = System.nanoTime();
		checkPoint = startTime;
		// set default time unit for reporting
		reportTimeUnit = TimeUnit.SECONDS;
	}

	/**
	 * Coping constructor
	 * 
	 * @param timer
	 */
	public Timer(Timer timer) {
		this();
		this.reportTimeUnit = timer.reportTimeUnit;
		this.statWriter = timer.getStatWriter();
	}

	public Timer(OutputStream statistics) throws IOException {
		this();
		setStatOutput(statistics);
	}

	public synchronized void setStatOutput(OutputStream statistics) {
		if (statistics != null) {
			statWriter = new PrintWriter(new OutputStreamWriter(statistics),
					true);
		} else {
			statWriter = new PrintWriter(new NullOutputStream());
		}
	}

	/**
	 * A plate to write the execution statistics to.
	 * 
	 * @return PrintWriter
	 * @throws IllegalStateException
	 *             if default Timer constructor was used instead of output
	 *             stream or file constructors
	 */
	public PrintWriter getStatWriter() {
		if (statWriter == null) {
			throw new IllegalStateException("Must use file or "
					+ "Output stream constructor is writer is required!");
		}
		return statWriter;
	}

	public void print(String statMessage) {
		getStatWriter().print(statMessage);
	}

	public void println(String statMessage) {
		getStatWriter().println(statMessage);
	}

	/**
	 * Make an instance of Timer which would report time in time unit provided
	 * 
	 * @param reportIn
	 *            - the time unit to report time in e.g.
	 *            {@code TimeUnit.SECONDS}
	 * @see TimeUnit
	 */
	public Timer(final TimeUnit reportIn) {
		this();
		reportTimeUnit = reportIn;
	}

	/**
	 * Convenience factory for micro seconds timer (10-E6 sec) functionally
	 * equivalent to new Timer(TimeUnit.MICROSECONDS).
	 * 
	 * @return Timer instance
	 */
	public static Timer getMicroSecondsTimer() {
		return new Timer(TimeUnit.MICROSECONDS);
	}

	/**
	 * Convenience factory for minutes timer functionally equivalent to new
	 * Timer(TimeUnit.MINUTES).
	 * 
	 * @return Timer instance
	 */
	public static Timer getMinutesTimer() {
		return new Timer(TimeUnit.MINUTES);
	}

	/**
	 * Convenience factory for milliseconds timer (10-E3 sec) functionally
	 * equivalent to new Timer(TimeUnit.MILLISECONDS).
	 * 
	 * @return Timer instance
	 */
	public static Timer getMilliSecondsTimer() {
		return new Timer(TimeUnit.MILLISECONDS);
	}

	private void checkPoint() {
		checkPoint = System.nanoTime();
	}

	/**
	 * Calculates the amount of time passed from previous call of this method in
	 * the time unit supplied as a parameter
	 * 
	 * @param tunit
	 * @return the amount of time passed from the last call of this method
	 */
	public long getStepTime(final TimeUnit tunit) {
		final long duration = tunit.convert(System.nanoTime() - checkPoint,
				TimeUnit.NANOSECONDS);
		checkPoint();
		return duration;
	}

	/**
	 * Calculates the amount of time passed from the last call of this method
	 * The reporting units are the time units provided in the time of the
	 * {@code Timer} instantiation. Default timer instance reports in seconds.
	 * 
	 * {@link Timer#getStepTime()}
	 * 
	 * @return the amount of time passed from previous call of this method
	 */
	public long getStepTime() {
		return getStepTime(reportTimeUnit);
	}

	/**
	 * Calculates the amount of time passed from the constructing of this
	 * instance. Time is reported in the time units provided during
	 * initialization of the Timer instance.
	 * 
	 * @return the amount of time passed from initiation of this instance
	 */
	public long getTotalTime() {
		return getTotalTime(reportTimeUnit);
	}

	/**
	 * Calculates the amount of time passed from the constructing of this
	 * instance convert this amount into the time units requested
	 * 
	 * @return the time passed from initiation of this instance in the time unit
	 *         requested
	 */
	public long getTotalTime(final TimeUnit tunit) {
		return tunit.convert(System.nanoTime() - startTime,
				TimeUnit.NANOSECONDS);
	}
}