diff options
Diffstat (limited to 'src/main/java/org/gnunet/util/AbsoluteTime.java')
-rw-r--r-- | src/main/java/org/gnunet/util/AbsoluteTime.java | 269 |
1 files changed, 269 insertions, 0 deletions
diff --git a/src/main/java/org/gnunet/util/AbsoluteTime.java b/src/main/java/org/gnunet/util/AbsoluteTime.java new file mode 100644 index 0000000..9d22d36 --- /dev/null +++ b/src/main/java/org/gnunet/util/AbsoluteTime.java | |||
@@ -0,0 +1,269 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.util; | ||
22 | |||
23 | import org.slf4j.Logger; | ||
24 | import org.slf4j.LoggerFactory; | ||
25 | |||
26 | import java.util.Date; | ||
27 | |||
28 | /** | ||
29 | * A specific point in time. | ||
30 | * | ||
31 | * @author Florian Dold | ||
32 | */ | ||
33 | public class AbsoluteTime implements Comparable<AbsoluteTime> { | ||
34 | private static final Logger logger = LoggerFactory | ||
35 | .getLogger(AbsoluteTime.class); | ||
36 | |||
37 | /** | ||
38 | * Constant for 'the beginning of time' in our frame. | ||
39 | */ | ||
40 | public final static AbsoluteTime ZERO = new AbsoluteTime(0); | ||
41 | public final static AbsoluteTime FOREVER = new AbsoluteTime(Long.MAX_VALUE); | ||
42 | |||
43 | /** | ||
44 | * Absolute time value in microseconds. | ||
45 | */ | ||
46 | private final long abs_value_us; | ||
47 | |||
48 | /** | ||
49 | * Gets the current time. | ||
50 | * | ||
51 | * @return the current time | ||
52 | */ | ||
53 | public static AbsoluteTime now() { | ||
54 | return new AbsoluteTime(System.currentTimeMillis() * 1000); | ||
55 | } | ||
56 | |||
57 | public AbsoluteTime(final long abs_value_us) { | ||
58 | this.abs_value_us = abs_value_us; | ||
59 | } | ||
60 | |||
61 | /** | ||
62 | * Adds a relative time value to an absolute time. | ||
63 | * | ||
64 | * @param duration duration to add to {@literal this} | ||
65 | * @return {@literal this + duration} | ||
66 | */ | ||
67 | public AbsoluteTime add(RelativeTime duration) { | ||
68 | if (abs_value_us == Long.MAX_VALUE | ||
69 | || duration.isForever()) { | ||
70 | return AbsoluteTime.FOREVER; | ||
71 | } | ||
72 | if (abs_value_us + duration.getMicroseconds() < abs_value_us) { | ||
73 | return AbsoluteTime.FOREVER; | ||
74 | } | ||
75 | return new AbsoluteTime(abs_value_us + duration.getMicroseconds()); | ||
76 | } | ||
77 | |||
78 | /** | ||
79 | * Calculates the estimate time of arrival/completion for an operation. | ||
80 | * | ||
81 | * @param start | ||
82 | * when did the operation start? | ||
83 | * @param finished | ||
84 | * how much has been done? | ||
85 | * @param total | ||
86 | * how much must be done overall (same unit as for "finished") | ||
87 | * @return remaining duration for the operation, assuming it continues at | ||
88 | * the same speed | ||
89 | */ | ||
90 | public static RelativeTime calculateETA(final AbsoluteTime start, | ||
91 | final long finished, final long total) { | ||
92 | if (finished >= total) { | ||
93 | return RelativeTime.ZERO; | ||
94 | } | ||
95 | if (finished == 0) { | ||
96 | return RelativeTime.FOREVER; | ||
97 | } | ||
98 | final RelativeTime dur = start.getDuration(); | ||
99 | final double exp = dur.getMicroseconds() * total | ||
100 | / (double) finished; | ||
101 | return new RelativeTime((long) exp); | ||
102 | } | ||
103 | |||
104 | |||
105 | /** | ||
106 | * {@inheritDoc} | ||
107 | */ | ||
108 | @Override | ||
109 | public boolean equals(Object other) { | ||
110 | return other instanceof AbsoluteTime && compareTo((AbsoluteTime) other) == 0; | ||
111 | } | ||
112 | |||
113 | /** | ||
114 | * {@inheritDoc} | ||
115 | */ | ||
116 | @Override | ||
117 | public int hashCode() { | ||
118 | return (int) this.abs_value_us; | ||
119 | } | ||
120 | |||
121 | /** | ||
122 | * {@inheritDoc} | ||
123 | */ | ||
124 | @Override | ||
125 | public int compareTo(AbsoluteTime other) { | ||
126 | if (this.abs_value_us < other.abs_value_us) { | ||
127 | return -1; | ||
128 | } | ||
129 | if (this.abs_value_us > other.abs_value_us) { | ||
130 | return 1; | ||
131 | } | ||
132 | return 0; | ||
133 | } | ||
134 | |||
135 | /** | ||
136 | * {@inheritDoc} | ||
137 | */ | ||
138 | @Override | ||
139 | public String toString() { | ||
140 | if (this.isForever()) { | ||
141 | return "AbsoluteTime(FOREVER)"; | ||
142 | } | ||
143 | return "AbsoluteTime("+this.abs_value_us +")"; | ||
144 | } | ||
145 | |||
146 | |||
147 | /** | ||
148 | * Check if a deadline is due. | ||
149 | * @return true if NOW is greater than the given time, false otherwise | ||
150 | */ | ||
151 | public boolean isDue() { | ||
152 | return this.abs_value_us < now().abs_value_us; | ||
153 | } | ||
154 | |||
155 | /** | ||
156 | * Does this AbsoluteTime value represent forever? | ||
157 | * | ||
158 | * @return this==FOREVER | ||
159 | */ | ||
160 | public boolean isForever() { | ||
161 | return this.abs_value_us == Long.MAX_VALUE; | ||
162 | } | ||
163 | |||
164 | /** | ||
165 | * Calculates the difference between two absolute times. | ||
166 | * | ||
167 | * @param other ... | ||
168 | * @return this - other | ||
169 | */ | ||
170 | public RelativeTime getDifference(final AbsoluteTime other) { | ||
171 | if (other.abs_value_us == Long.MAX_VALUE) { | ||
172 | return RelativeTime.FOREVER; | ||
173 | } | ||
174 | return new RelativeTime(abs_value_us - other.abs_value_us); | ||
175 | } | ||
176 | |||
177 | /** | ||
178 | * Gets the duration of an operation as the difference of the current time | ||
179 | * and {@literal this}. | ||
180 | * | ||
181 | * @return this - now | ||
182 | */ | ||
183 | public RelativeTime getDuration() { | ||
184 | assert abs_value_us != Long.MAX_VALUE; | ||
185 | return getDifference(AbsoluteTime.now()); | ||
186 | } | ||
187 | |||
188 | /** | ||
189 | * Returns the milliseconds since some fixed point of reference. | ||
190 | * | ||
191 | * @return the absolute time in milliseconds | ||
192 | */ | ||
193 | public long getMicroseconds() { | ||
194 | return abs_value_us; | ||
195 | } | ||
196 | |||
197 | /** | ||
198 | * Calculates the remaining time relative to now. | ||
199 | * | ||
200 | * @return this - now | ||
201 | */ | ||
202 | public RelativeTime getRemaining() { | ||
203 | if (abs_value_us == Long.MAX_VALUE) { | ||
204 | return RelativeTime.FOREVER; | ||
205 | } | ||
206 | return getDifference(AbsoluteTime.now()); | ||
207 | } | ||
208 | |||
209 | /** | ||
210 | * Returns the maximum of two time values. | ||
211 | * | ||
212 | * @param other ... | ||
213 | * @return max(this,other) | ||
214 | */ | ||
215 | public AbsoluteTime max(final AbsoluteTime other) { | ||
216 | return abs_value_us >= other.abs_value_us ? this : other; | ||
217 | |||
218 | } | ||
219 | |||
220 | /** | ||
221 | * Returns the minimum of two time values. | ||
222 | * | ||
223 | * @param other ... | ||
224 | * @return min(this,other) | ||
225 | */ | ||
226 | public AbsoluteTime min(final AbsoluteTime other) { | ||
227 | return abs_value_us <= other.abs_value_us ? this : other; | ||
228 | } | ||
229 | |||
230 | /** | ||
231 | * Subtracts a relative time value to an absolute time | ||
232 | * | ||
233 | * @param duration ... | ||
234 | * @return this - duration | ||
235 | */ | ||
236 | public AbsoluteTime subtract(final RelativeTime duration) { | ||
237 | if (abs_value_us <= duration.getMicroseconds()) { | ||
238 | return AbsoluteTime.ZERO; | ||
239 | } | ||
240 | if (abs_value_us == Long.MAX_VALUE) { | ||
241 | return this; | ||
242 | } | ||
243 | return new AbsoluteTime(abs_value_us - duration.getMicroseconds()); | ||
244 | } | ||
245 | |||
246 | /** | ||
247 | * Get a serializable message corresponding to this AbsoluteTime. | ||
248 | * | ||
249 | * @return a serializable message corresponding to this AbsoluteTime | ||
250 | */ | ||
251 | public AbsoluteTimeMessage asMessage() { | ||
252 | return new AbsoluteTimeMessage(this); | ||
253 | } | ||
254 | |||
255 | /** | ||
256 | * Get the AbsoluteTime from a AbsoluteTimeMessage. | ||
257 | * | ||
258 | * @param m serializable representation of an AbsoluteTime | ||
259 | * | ||
260 | * @return the real AbsoluteTime associated with m | ||
261 | */ | ||
262 | public static AbsoluteTime fromNetwork(AbsoluteTimeMessage m) { | ||
263 | return m.value__ < 0 ? AbsoluteTime.FOREVER : new AbsoluteTime(m.value__); | ||
264 | } | ||
265 | |||
266 | public Date toDate() { | ||
267 | return new Date(abs_value_us / 1000); | ||
268 | } | ||
269 | } | ||