summaryrefslogtreecommitdiffstats
path: root/timer/com/vladium/utils/timing/TimerFactory.java
blob: ae127c50a51ae9e6546850978e23083e1849ce4c (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

package com.vladium.utils.timing;

// ----------------------------------------------------------------------------
/**
 * This non-instantiable non-extendible class acts as a Factory for {@link ITimer}
 * implementations.
 * 
 * @author (C) <a href="mailto:vroubtsov@illinoisalumni.org">Vlad Roubtsov</a>, 2002
 */
public abstract class TimerFactory
{
    // public: ................................................................

	private static final String HRTIMER_LIB = "hrtlib";

    /**
     * Creates a new instance of {@link ITimer} which is returned in 'ready'
     * state. If the JNI-based/high-resolution implementation is not available
     * this will return an instance of <code>JavaSystemTimer</code>, so this
     * method is guaranteed not to fail.
     * 
     * @return ITimer a new timer instance in 'ready' state [never null] 
     */
	
	public static void initialize(String path) {

		UnsatisfiedLinkError exception = null;
		
		try {
			System.loadLibrary (HRTIMER_LIB);
		} catch (UnsatisfiedLinkError e) {
			if(path != null) {
				try {
					System.load(path);
				} catch (UnsatisfiedLinkError ex) {
					exception = ex;
				}
			} else {
				exception = e;
			}
		}

		if(exception != null) {
			System.out.println ("native lib '" + HRTIMER_LIB
					+ "' not found in 'java.library.path': "
					+ System.getProperty ("java.library.path")
					+path==null?"":(" or in "+path));

			throw exception; // re-throw
		}
	}
	
    public static ITimer newTimer ()
    {
//        try
//        {
            return new HRTimer ();
//        }
//        catch (Throwable t)
//        {
//            return new JavaSystemTimer ();
//        }
    }
    
    // protected: .............................................................

    // package: ...............................................................
    
    // private: ...............................................................
    
    private TimerFactory () {} // prevent subclassing
    
} // end of class
// ----------------------------------------------------------------------------