aboutsummaryrefslogtreecommitdiff
path: root/src/org/gnunet/util/RelativeTime.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/gnunet/util/RelativeTime.java')
-rw-r--r--src/org/gnunet/util/RelativeTime.java56
1 files changed, 30 insertions, 26 deletions
diff --git a/src/org/gnunet/util/RelativeTime.java b/src/org/gnunet/util/RelativeTime.java
index 7b45bb0..0faedfc 100644
--- a/src/org/gnunet/util/RelativeTime.java
+++ b/src/org/gnunet/util/RelativeTime.java
@@ -32,7 +32,8 @@ public final class RelativeTime implements Comparable<RelativeTime> {
32 private static final Logger logger = LoggerFactory 32 private static final Logger logger = LoggerFactory
33 .getLogger(RelativeTime.class); 33 .getLogger(RelativeTime.class);
34 34
35 public static final RelativeTime MILLISECOND = new RelativeTime(1); 35 public static final RelativeTime MICROSECOND = new RelativeTime(1);
36 public static final RelativeTime MILLISECOND = MICROSECOND.multiply(1000);
36 public static final RelativeTime SECOND = MILLISECOND.multiply(1000); 37 public static final RelativeTime SECOND = MILLISECOND.multiply(1000);
37 public static final RelativeTime MINUTE = SECOND.multiply(60); 38 public static final RelativeTime MINUTE = SECOND.multiply(60);
38 public static final RelativeTime HOUR = MINUTE.multiply(60); 39 public static final RelativeTime HOUR = MINUTE.multiply(60);
@@ -44,7 +45,10 @@ public final class RelativeTime implements Comparable<RelativeTime> {
44 public static final RelativeTime ZERO = new RelativeTime(0); 45 public static final RelativeTime ZERO = new RelativeTime(0);
45 public static final RelativeTime FOREVER = new RelativeTime(Long.MAX_VALUE); 46 public static final RelativeTime FOREVER = new RelativeTime(Long.MAX_VALUE);
46 47
47 private final long rel_value; 48 /**
49 * Time offset in microseconds.
50 */
51 private final long rel_value_us;
48 52
49 /** 53 /**
50 * Create a new RelativeTime value, with a given time in milliseconds. 54 * Create a new RelativeTime value, with a given time in milliseconds.
@@ -52,7 +56,7 @@ public final class RelativeTime implements Comparable<RelativeTime> {
52 * @param abs_value time in milliseconds 56 * @param abs_value time in milliseconds
53 */ 57 */
54 public RelativeTime(final long abs_value) { 58 public RelativeTime(final long abs_value) {
55 this.rel_value = abs_value; 59 this.rel_value_us = abs_value;
56 } 60 }
57 61
58 /** 62 /**
@@ -64,13 +68,13 @@ public final class RelativeTime implements Comparable<RelativeTime> {
64 * @return this + other 68 * @return this + other
65 */ 69 */
66 public RelativeTime add(final RelativeTime other) { 70 public RelativeTime add(final RelativeTime other) {
67 if (this.rel_value == Long.MAX_VALUE 71 if (this.rel_value_us == Long.MAX_VALUE
68 || other.rel_value == Long.MAX_VALUE) { 72 || other.rel_value_us == Long.MAX_VALUE) {
69 return RelativeTime.FOREVER; 73 return RelativeTime.FOREVER;
70 } 74 }
71 final long new_rel_value = this.rel_value + other.rel_value; 75 final long new_rel_value = this.rel_value_us + other.rel_value_us;
72 // check for numeric overflow 76 // check for numeric overflow
73 if (new_rel_value < this.rel_value) { 77 if (new_rel_value < this.rel_value_us) {
74 logger.warn("time overflow"); 78 logger.warn("time overflow");
75 return RelativeTime.FOREVER; 79 return RelativeTime.FOREVER;
76 } 80 }
@@ -85,10 +89,10 @@ public final class RelativeTime implements Comparable<RelativeTime> {
85 * @return FOREVER if this=FOREVER or factor=0; otherwise this/factor 89 * @return FOREVER if this=FOREVER or factor=0; otherwise this/factor
86 */ 90 */
87 public RelativeTime divide(final int factor) { 91 public RelativeTime divide(final int factor) {
88 if (factor == 0 || this.rel_value == Long.MAX_VALUE) { 92 if (factor == 0 || this.rel_value_us == Long.MAX_VALUE) {
89 return RelativeTime.FOREVER; 93 return RelativeTime.FOREVER;
90 } 94 }
91 return new RelativeTime(this.rel_value / factor); 95 return new RelativeTime(this.rel_value_us / factor);
92 } 96 }
93 97
94 /** 98 /**
@@ -96,8 +100,8 @@ public final class RelativeTime implements Comparable<RelativeTime> {
96 * 100 *
97 * @return the amount of time in milliseconds 101 * @return the amount of time in milliseconds
98 */ 102 */
99 public long getMilliseconds() { 103 public long getMicroseconds() {
100 return rel_value; 104 return rel_value_us;
101 } 105 }
102 106
103 /** 107 /**
@@ -106,7 +110,7 @@ public final class RelativeTime implements Comparable<RelativeTime> {
106 * @return max(t1, t2) 110 * @return max(t1, t2)
107 */ 111 */
108 public static RelativeTime max(RelativeTime t1, RelativeTime t2) { 112 public static RelativeTime max(RelativeTime t1, RelativeTime t2) {
109 return t1.rel_value >= t2.rel_value ? t1 : t2; 113 return t1.rel_value_us >= t2.rel_value_us ? t1 : t2;
110 } 114 }
111 115
112 /** 116 /**
@@ -115,7 +119,7 @@ public final class RelativeTime implements Comparable<RelativeTime> {
115 * @return min(this, other) 119 * @return min(this, other)
116 */ 120 */
117 public static RelativeTime min(RelativeTime t1, RelativeTime t2) { 121 public static RelativeTime min(RelativeTime t1, RelativeTime t2) {
118 return t1.rel_value <= t2.rel_value ? t1 : t2; 122 return t1.rel_value_us <= t2.rel_value_us ? t1 : t2;
119 } 123 }
120 124
121 /** 125 /**
@@ -127,9 +131,9 @@ public final class RelativeTime implements Comparable<RelativeTime> {
127 if (factor == 0) { 131 if (factor == 0) {
128 return RelativeTime.ZERO; 132 return RelativeTime.ZERO;
129 } 133 }
130 final long ret = this.rel_value * factor; 134 final long ret = this.rel_value_us * factor;
131 // check for numeric overflow 135 // check for numeric overflow
132 if (ret / factor != rel_value) { 136 if (ret / factor != rel_value_us) {
133 logger.warn("time overflow"); 137 logger.warn("time overflow");
134 return RelativeTime.FOREVER; 138 return RelativeTime.FOREVER;
135 } 139 }
@@ -145,12 +149,12 @@ public final class RelativeTime implements Comparable<RelativeTime> {
145 * this=FOREVER, this-other otherwise 149 * this=FOREVER, this-other otherwise
146 */ 150 */
147 public RelativeTime subtract(final RelativeTime other) { 151 public RelativeTime subtract(final RelativeTime other) {
148 if (this.rel_value >= other.rel_value) { 152 if (this.rel_value_us >= other.rel_value_us) {
149 return RelativeTime.ZERO; 153 return RelativeTime.ZERO;
150 } else if (this.rel_value == Long.MAX_VALUE) { 154 } else if (this.rel_value_us == Long.MAX_VALUE) {
151 return this; 155 return this;
152 } else { 156 } else {
153 return new RelativeTime(this.rel_value - other.rel_value); 157 return new RelativeTime(this.rel_value_us - other.rel_value_us);
154 } 158 }
155 } 159 }
156 160
@@ -165,24 +169,24 @@ public final class RelativeTime implements Comparable<RelativeTime> {
165 } 169 }
166 170
167 public boolean isForever() { 171 public boolean isForever() {
168 return rel_value == FOREVER.rel_value; 172 return rel_value_us == FOREVER.rel_value_us;
169 } 173 }
170 174
171 public boolean equals(Object o) { 175 public boolean equals(Object o) {
172 return (o instanceof RelativeTime) && ((RelativeTime) o).rel_value == rel_value; 176 return (o instanceof RelativeTime) && ((RelativeTime) o).rel_value_us == rel_value_us;
173 } 177 }
174 178
175 @Override 179 @Override
176 public int hashCode() { 180 public int hashCode() {
177 return (int) this.rel_value; 181 return (int) this.rel_value_us;
178 } 182 }
179 183
180 @Override 184 @Override
181 public int compareTo(RelativeTime other) { 185 public int compareTo(RelativeTime other) {
182 if (this.rel_value < other.rel_value) { 186 if (this.rel_value_us < other.rel_value_us) {
183 return -1; 187 return -1;
184 } 188 }
185 if (this.rel_value > other.rel_value) { 189 if (this.rel_value_us > other.rel_value_us) {
186 return 1; 190 return 1;
187 } 191 }
188 return 0; 192 return 0;
@@ -193,16 +197,16 @@ public final class RelativeTime implements Comparable<RelativeTime> {
193 if (this.isForever()) { 197 if (this.isForever()) {
194 return "RelativeTime(FOREVER)"; 198 return "RelativeTime(FOREVER)";
195 } 199 }
196 return "RelativeTime("+this.rel_value+")"; 200 return "RelativeTime("+this.rel_value_us +")";
197 } 201 }
198 202
199 203
200 204
201 205
202 public RelativeTimeMessage toNetwork() { 206 public RelativeTimeMessage toNetwork() {
203 long rval = this.rel_value; 207 long rval = this.rel_value_us;
204 assert rval >= 0; 208 assert rval >= 0;
205 if (rval == FOREVER.rel_value) { 209 if (rval == FOREVER.rel_value_us) {
206 rval = -1L; /* 0xFFFFFFFFFFFFFFFF for network format! */ 210 rval = -1L; /* 0xFFFFFFFFFFFFFFFF for network format! */
207 } 211 }
208 return new RelativeTimeMessage(rval); 212 return new RelativeTimeMessage(rval);