01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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:
19: package org.apache.jorphan.timer;
20:
21: /**
22: * A package-private implementation of {@link ITimer} based around native
23: * <code>getTime</code> method. It will work on any platform for which a JNI
24: * implementation of "hrtlib" library is available.
25: * <P>
26: *
27: * {@link TimerFactory} acts as the Factory for this class.
28: * <P>
29: *
30: * MT-safety: an instance of this class is safe to be used within the same
31: * thread only.
32: *
33: * @author <a href="mailto:vroubtsov@illinoisalumni.org">Vlad Roubtsov</a>
34: * @author Originally published in <a
35: * href="http://www.javaworld.com/javaworld/javaqa/2003-01/01-qa-0110-timing.html">JavaWorld</a>
36: * @version $Revision: 493784 $
37: */
38: final class HRTimer extends AbstractTimer {
39: private static final String HRTIMER_LIB = "hrtlib";
40:
41: static {
42: try {
43: System.loadLibrary(HRTIMER_LIB);
44: } catch (UnsatisfiedLinkError e) {
45: System.out.println("native lib '" + HRTIMER_LIB
46: + "' not found in 'java.library.path': "
47: + System.getProperty("java.library.path"));
48:
49: throw e; // re-throw
50: }
51: }
52:
53: /*
54: * This is supposed to return a fractional count of milliseconds elapsed
55: * since some indeterminate moment in the past. The exact starting point is
56: * not relevant because this timer class reports time differences only.
57: *
58: * JNI code in HRTIMER_LIB library is supposed to implement this.
59: */
60: private static native double getTime();
61:
62: protected double getCurrentTime() {
63: return getTime();
64: }
65: }
|