01: /*
02: * Copyright (c) 2001 by Matt Welsh and The Regents of the University of
03: * California. All rights reserved.
04: *
05: * Permission to use, copy, modify, and distribute this software and its
06: * documentation for any purpose, without fee, and without written agreement is
07: * hereby granted, provided that the above copyright notice and the following
08: * two paragraphs appear in all copies of this software.
09: *
10: * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
11: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
12: * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
13: * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14: *
15: * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
18: * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
19: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
20: *
21: * Author: Matt Welsh <mdw@cs.berkeley.edu>
22: *
23: */
24:
25: package seda.util;
26:
27: import java.io.*;
28: import java.util.*;
29: import java.text.*;
30:
31: /**
32: * This class provides some generic utility functions.
33: *
34: * @author Matt Welsh
35: */
36: public class MDWUtil {
37:
38: static boolean nativeLibraryLoaded = false;
39: static Object nativeLibraryLoadLock = new Object();
40:
41: static void loadNativeLibrary() {
42: synchronized (nativeLibraryLoadLock) {
43: if (!nativeLibraryLoaded) {
44: try {
45: System.loadLibrary("MDWUtil");
46: nativeLibraryLoaded = true;
47: } catch (Exception e) {
48: System.err
49: .println("Cannot load MDWUtil shared library");
50: }
51: }
52: }
53: }
54:
55: private static DecimalFormat df;
56:
57: static {
58: loadNativeLibrary();
59: df = new DecimalFormat();
60: df.applyPattern("#.####");
61: }
62:
63: /**
64: * Returns the current time in microseconds.
65: */
66: public static native long currentTimeUsec();
67:
68: /**
69: * Cause the current thread to sleep for the given number of
70: * microseconds. Returns immediately if the thread is interrupted, but
71: * does not throw an exception.
72: */
73: public static native void usleep(long delay);
74:
75: /**
76: * Format decimals to 4 digits only
77: */
78: public static String format(double val) {
79: return new String(df.format(val));
80: }
81:
82: /**
83: * Cause the current thread to sleep for the given number of
84: * milliseconds. Returns immediately if the thread is interrupted, but
85: * does not throw an exception.
86: */
87: public static void sleep(long delay) {
88: try {
89: Thread.currentThread().sleep(delay);
90: } catch (InterruptedException ie) {
91: // Ignore
92: }
93: }
94:
95: }
|