[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r18015 - in gnunet-java: . src/org/gnunet/util/time
From: |
gnunet |
Subject: |
[GNUnet-SVN] r18015 - in gnunet-java: . src/org/gnunet/util/time |
Date: |
Sat, 5 Nov 2011 11:36:30 +0100 |
Author: dold
Date: 2011-11-05 11:36:30 +0100 (Sat, 05 Nov 2011)
New Revision: 18015
Modified:
gnunet-java/ISSUES
gnunet-java/src/org/gnunet/util/time/AbsoluteTime.java
gnunet-java/src/org/gnunet/util/time/RelativeTime.java
Log:
implementation of time
Modified: gnunet-java/ISSUES
===================================================================
--- gnunet-java/ISSUES 2011-11-05 09:07:09 UTC (rev 18014)
+++ gnunet-java/ISSUES 2011-11-05 10:36:30 UTC (rev 18015)
@@ -15,7 +15,10 @@
* wrt the scheduler: can you select on anything else than a Connection? pipes,
sockets (tcp/udp), files, timeout
+* logging / assertions (use guava?)
+* unit testing (JUnit vs NGUnit)
+
Used Libraries:
* https://github.com/magnuss/java-bloomfilter (LGPL) => freeway!
* http://code.google.com/p/junixsocket/ (Apache License 2.0)
Modified: gnunet-java/src/org/gnunet/util/time/AbsoluteTime.java
===================================================================
--- gnunet-java/src/org/gnunet/util/time/AbsoluteTime.java 2011-11-05
09:07:09 UTC (rev 18014)
+++ gnunet-java/src/org/gnunet/util/time/AbsoluteTime.java 2011-11-05
10:36:30 UTC (rev 18015)
@@ -1,6 +1,5 @@
package org.gnunet.util.time;
-
public class AbsoluteTime {
public final static AbsoluteTime ZERO = new AbsoluteTime(0);
@@ -9,19 +8,19 @@
private static long offset;
private final long abs_value;
-
+
public static void setOffset(long offset) {
AbsoluteTime.offset = offset;
}
-
- public AbsoluteTime() {
- this.abs_value = System.currentTimeMillis();
+
+ public static AbsoluteTime now() {
+ return new AbsoluteTime(System.currentTimeMillis());
}
public AbsoluteTime(long abs_value) {
this.abs_value = abs_value;
}
-
+
public long getMilliseconds() {
return abs_value;
}
@@ -40,9 +39,30 @@
}
+ /**
+ * Calculate the estimate time of arrival/completion for an operation.
+ *
+ * @param start
+ * when did the operation start?
+ * @param finished
+ * how much has been done?
+ * @param total
+ * how much must be done overall (same unit as for
"finished")
+ * @return remaining duration for the operation, assuming it continues
at
+ * the same speed
+ */
public RelativeTime calculateETA(AbsoluteTime start, long finished,
long total) {
- throw new UnsupportedOperationException();
+ if (finished >= total) {
+ return RelativeTime.ZERO;
+ }
+ if (finished == 0) {
+ return RelativeTime.FOREVER;
+ }
+ RelativeTime dur = start.getDuration(start);
+ double exp = ((double) dur.getMilliseconds()) * ((double) total)
+ / ((double) finished);
+ return new RelativeTime((long) exp);
}
public RelativeTime getDifference(AbsoluteTime other) {
Modified: gnunet-java/src/org/gnunet/util/time/RelativeTime.java
===================================================================
--- gnunet-java/src/org/gnunet/util/time/RelativeTime.java 2011-11-05
09:07:09 UTC (rev 18014)
+++ gnunet-java/src/org/gnunet/util/time/RelativeTime.java 2011-11-05
10:36:30 UTC (rev 18015)
@@ -1,7 +1,11 @@
package org.gnunet.util.time;
+/**
+ *
+ * @author Florian Dold
+ *
+ */
public class RelativeTime {
-
public final static RelativeTime MILLISECOND = new RelativeTime(1);
public final static RelativeTime SECOND = MILLISECOND.multiply(1000);
public final static RelativeTime MINUTE = SECOND.multiply(60);
@@ -11,45 +15,127 @@
public final static RelativeTime MONTH = DAY.multiply(30);
public final static RelativeTime YEAR = DAY.multiply(365);
+ public final static RelativeTime ZERO = new RelativeTime(0);
public final static RelativeTime FOREVER = new
RelativeTime(Long.MAX_VALUE);
- private final long abs_value;
+ private final long rel_value;
public RelativeTime(long abs_value) {
- this.abs_value = abs_value;
+ this.rel_value = abs_value;
}
-
+
public long getMilliseconds() {
- return abs_value;
+ return rel_value;
}
+ /**
+ * Convert relative time to an absolute time in the
+ * future.
+ *
+ * @return timestamp that is "rel" in the future, or FOREVER if
rel==FOREVER (or if we would overflow)
+ */
public AbsoluteTime toAbsolute() {
- throw new UnsupportedOperationException();
+ return AbsoluteTime.now().add(this);
}
+ /**
+ * Return the minimum of two relative time values.
+ */
public RelativeTime min(RelativeTime other) {
- throw new UnsupportedOperationException();
+ // XXX: use guava for null checks?
+ if (this.rel_value < other.rel_value) {
+ return this;
+ } else {
+ return other;
+ }
}
public RelativeTime max(RelativeTime other) {
- throw new UnsupportedOperationException();
+ if (this.rel_value > other.rel_value) {
+ return this;
+ } else {
+ return other;
+ }
}
+ /**
+ * Multiply relative time by a given factor.
+ *
+ * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
+ */
public RelativeTime multiply(int factor) {
- throw new UnsupportedOperationException();
+ if (factor == 0) {
+ return RelativeTime.ZERO;
+ }
+ // XXX: better way of overflow checking??
+ long ret = this.rel_value * factor;
+ if (ret / factor != rel_value) {
+ // XXX: logging
+ return RelativeTime.FOREVER;
+ }
+ return new RelativeTime(ret);
}
+ /**
+ * Divide relative time by a given factor.
+ *
+ * @param rel
+ * some duration
+ * @param factor
+ * integer to divide by
+ * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
+ */
public RelativeTime divide(int factor) {
- throw new UnsupportedOperationException();
+ if (factor == 0 || this.rel_value == Long.MAX_VALUE) {
+ return RelativeTime.FOREVER;
+ }
+ return new RelativeTime(this.rel_value / (long) factor);
}
+ /**
+ * Add relative times together.
+ */
public RelativeTime add(RelativeTime other) {
- throw new UnsupportedOperationException();
+ if (this.rel_value == Long.MAX_VALUE
+ || other.rel_value == Long.MAX_VALUE) {
+ return RelativeTime.FOREVER;
+ }
+ long new_rel_value = this.rel_value + other.rel_value;
+ // check for numeric overflow
+ if (new_rel_value < this.rel_value) {
+ // XXX: do logging
+ return RelativeTime.FOREVER;
+ }
+ return new RelativeTime(new_rel_value);
}
+ /**
+ * Subtract relative timestamp from the other.
+ *
+ * @param other
+ * second timestamp
+ * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is
+ * FOREVER, a1-a2 otherwise
+ */
public RelativeTime subtract(RelativeTime other) {
- throw new UnsupportedOperationException();
+ if (this.rel_value >= other.rel_value) {
+ return RelativeTime.ZERO;
+ } else if (this.rel_value == Long.MAX_VALUE) {
+ return this;
+ } else {
+ return new RelativeTime(this.rel_value -
other.rel_value);
+ }
}
+
+ /**
+ * Convert a relative time to a string.
+ *
+ * @return string form of the time (as milliseconds)
+ */
+ @Override
+ public String toString() {
+ return "" + this.rel_value;
+ }
public byte[] toNetwork() {
throw new UnsupportedOperationException();
@@ -59,13 +145,5 @@
throw new UnsupportedOperationException();
}
- /**
- * Convert a relative time to a string.
- *
- * @return string form of the time (as milliseconds)
- */
- @Override
- public String toString() {
- throw new UnsupportedOperationException();
- }
+
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r18015 - in gnunet-java: . src/org/gnunet/util/time,
gnunet <=