1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.bam; |
18 |
|
|
19 |
|
import java.util.concurrent.TimeUnit; |
20 |
|
import java.util.Date; |
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
public class TimeBuilder { |
28 |
0 |
private long number; |
29 |
0 |
private TimeUnit timeUnit = TimeUnit.MILLISECONDS; |
30 |
|
private boolean configuredTime; |
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
0 |
public static TimeBuilder time(long number) { |
37 |
0 |
return new TimeBuilder(number); |
38 |
|
} |
39 |
0 |
|
40 |
0 |
public TimeBuilder(long number) { |
41 |
0 |
this.number = number; |
42 |
0 |
} |
43 |
|
|
44 |
0 |
public long toMillis() { |
45 |
0 |
return timeUnit.toMillis(number); |
46 |
|
} |
47 |
|
|
48 |
0 |
public Date toDate() { |
49 |
0 |
return new Date(toMillis()); |
50 |
|
} |
51 |
|
|
52 |
|
public TimeBuilder millis() { |
53 |
0 |
setTimeUnit(TimeUnit.MILLISECONDS); |
54 |
0 |
return this; |
55 |
|
} |
56 |
|
|
57 |
|
public TimeBuilder nanos() { |
58 |
0 |
setTimeUnit(TimeUnit.NANOSECONDS); |
59 |
0 |
return this; |
60 |
|
} |
61 |
|
|
62 |
|
public TimeBuilder micros() { |
63 |
0 |
setTimeUnit(TimeUnit.MICROSECONDS); |
64 |
0 |
return this; |
65 |
|
} |
66 |
|
|
67 |
|
public TimeBuilder seconds() { |
68 |
0 |
setTimeUnit(TimeUnit.SECONDS); |
69 |
0 |
return this; |
70 |
0 |
} |
71 |
|
|
72 |
|
public TimeBuilder minutes() { |
73 |
0 |
setTimeUnit(TimeUnit.SECONDS); |
74 |
0 |
number = minutesAsSeconds(number); |
75 |
0 |
return this; |
76 |
0 |
} |
77 |
|
|
78 |
|
public TimeBuilder hours() { |
79 |
0 |
setTimeUnit(TimeUnit.SECONDS); |
80 |
0 |
number = hoursAsSeconds(number); |
81 |
0 |
return this; |
82 |
0 |
} |
83 |
|
|
84 |
|
public TimeBuilder days() { |
85 |
0 |
setTimeUnit(TimeUnit.SECONDS); |
86 |
0 |
number = daysAsSeconds(number); |
87 |
0 |
return this; |
88 |
|
} |
89 |
|
|
90 |
0 |
public long getNumber() { |
91 |
0 |
return number; |
92 |
|
} |
93 |
|
|
94 |
0 |
public TimeUnit getTimeUnit() { |
95 |
0 |
return timeUnit; |
96 |
|
} |
97 |
|
|
98 |
0 |
public void setTimeUnit(TimeUnit timeUnit) { |
99 |
0 |
if (configuredTime) { |
100 |
0 |
throw new IllegalArgumentException("Cannot configure the time unit twice!"); |
101 |
0 |
} |
102 |
|
else { |
103 |
0 |
configuredTime = true; |
104 |
0 |
} |
105 |
0 |
this.timeUnit = timeUnit; |
106 |
0 |
} |
107 |
|
|
108 |
0 |
protected long minutesAsSeconds(long value) { |
109 |
0 |
return value * 60; |
110 |
|
} |
111 |
|
|
112 |
0 |
protected long hoursAsSeconds(long value) { |
113 |
0 |
return minutesAsSeconds(value) * 60; |
114 |
|
} |
115 |
|
|
116 |
|
protected long daysAsSeconds(long value) { |
117 |
0 |
return hoursAsSeconds(value) * 24; |
118 |
|
} |
119 |
|
|
120 |
|
} |