01: package kawa.standard;
02:
03: import kawa.lang.*;
04: import gnu.math.*;
05:
06: /** Implements the extended procedure "sleep". */
07:
08: public class sleep {
09: public static void sleep(Quantity q) {
10: Unit u = q.unit();
11: double seconds;
12: // If q is either dimensionless or its unit is a multiple of Unit.second:
13: if (u == Unit.Empty
14: || u.dimensions() == Unit.second.dimensions())
15: seconds = q.doubleValue();
16: else
17: throw new GenericError("bad unit for sleep");
18: long millis = (long) (seconds * 1000.0);
19: int nanos = (int) (seconds * 1e9 - millis * 1e6);
20: try {
21: Thread.sleep(millis, nanos);
22: } catch (InterruptedException ex) {
23: throw new GenericError("sleep was interrupted");
24: }
25: }
26:
27: }
|