blob: 261ad86ef353250cc53d3f61ee9afda5c6b5d920 (
plain)
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
|
package Tools;
/*
* Copyright 2009 Volker Oth
*
* Licensed 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
*
* http://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.
*/
/**
* Wrapper class for direct timer access. Implements a microsecond timer.
*
* @author Volker Oth
*/
public class MicrosecondTimer {
private long timeBase;
/**
* Constructor.
*/
public MicrosecondTimer() {
timeBase = System.nanoTime()/1000;
}
/**
* Return delta time since last update.
* @return delta time since last update.
*/
public long delta() {
long t = System.nanoTime()/1000;
long delta = t - timeBase;
// check for inconsistency: external clock setting (e.g. to internet server)
// might cause t to jump "back" in time
if (delta < 0) {
System.out.println("MicrosecondTimer inconsistency detected: "+(-delta)+"us");
timeBase = t;
delta = 0;
}
return delta;
}
/**
* Return delta time since last update and perform an update.
* @return delta time since last update.
*/
public long deltaUpdate() {
long t = System.nanoTime()/1000;
long delta = t - timeBase;
// check for inconsistency: external clock setting (e.g. to internet server)
// might cause t to jump "back" in time
if (delta < 0) {
System.out.println("MicrosecondTimer inconsistency detected: "+(-delta)+"us");
delta = 0;
}
timeBase = t;
return delta;
}
/**
* Returns true if the given time has passed since the last update.
* @param dt time delta in microseconds
* @return true if the given time has passed since the last update.
*/
public boolean timePassed(final long dt) {
long delta = delta();
if (delta>=dt)
return true;
return false;
}
/**
* Returns true if the given time has passed since the last update and performs an update.
* @param dt time delta in microseconds
* @return true if the given time has passed since the last update.
*/
public boolean timePassedUpdate(final long dt) {
long t = System.nanoTime()/1000;
long delta = delta();
if (delta>=dt) {
timeBase = t;
return true;
}
return false;
}
/**
* Returns true if the given time has passed since the last update and adds the time delta.
* @param dt time delta
* @return true if the given time has passed since the last update.
*/
public boolean timePassedAdd(final long dt) {
long delta = delta();
if (delta>=dt) {
timeBase+= dt;
return true;
}
return false;
}
/**
* Updates the internal time base to the current timer value.
*/
public void update() {
long t = System.nanoTime()/1000;
timeBase = t;
}
/**
* Adds a time delta to the internal time base.
* @param delta time delta in microseconds
*/
public void update(long delta) {
timeBase += delta;
}
}
|