comparison src/org/dancres/blitz/util/Time.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 package org.dancres.blitz.util;
2
3 import net.jini.core.lease.Lease;
4 import net.jini.core.lease.LeaseDeniedException;
5
6 /**
7 Various methods/classes must cope with the JINI time model. This class
8 provides useful methods for dealing with that model.
9 */
10 public class Time {
11 /**
12 Computes an expiry time based on the passed in wait time which might
13 be a suitable value or it might be Lease.FOREVER
14 */
15 public static long getAbsoluteTime(long aWaitTime) {
16 if (aWaitTime == Lease.FOREVER)
17 return Long.MAX_VALUE;
18
19 long myTime = System.currentTimeMillis() + aWaitTime;
20
21 if (myTime < 0)
22 return Long.MAX_VALUE;
23 else
24 return myTime;
25 }
26
27 /**
28 Compute the actual time to relative wait time based on the passed
29 argument. This will typically used by read/take variants.
30 */
31 public static long getWaitTime(long aWaitTime) {
32 if (aWaitTime == Lease.FOREVER)
33 return Long.MAX_VALUE;
34 else
35 return aWaitTime;
36 }
37
38 /**
39 Computes a lease duration given a lease duration which
40 could be a legitimate duration, Lease.FOREVER or Lease.ANY
41
42 @param aTime the desired lease length
43 @param aBound the maximum lease length allowed. If aBound is zero,
44 then the max time is assumed to be Lease.FOREVER
45 */
46 public static long getLeaseDuration(long aTime, long aBound) {
47
48 long myBound;
49 long myDuration = aTime;
50
51 // Convert the bound to something sane
52 if (aBound == 0)
53 myBound = Lease.FOREVER;
54 else
55 myBound = aBound;
56
57 // Allow FOREVER or not, based on bound
58 if (aTime == Lease.FOREVER) {
59 if (aBound == 0)
60 return Long.MAX_VALUE;
61 else
62 return myBound;
63 }
64
65 // If ANY, use the sanitized bound
66 if (aTime == Lease.ANY) {
67 myDuration = myBound;
68 }
69
70 if (myDuration > myBound)
71 return myBound;
72 else
73 return myDuration;
74 }
75 }