Re: [concurrency-interest] Durations in existing JDK APIs
I am not sure I understand this implementation, but isn’t
long s = convert(duration.getSeconds(), SECONDS);
needing to actually be
long s = convert(duration.getSeconds(), NANOSECONDS);
so that s+n is in a common unit of measure? Gregg
On May 30, 2018, at 7:19 PM, Martin Buchholz via Concurrency-interest <concurrency-interest@cs.oswego.edu> wrote:
v.0.2 has both conversion methods in TimeUnit. The unexpected weirdness is that convert(Duration) saturates while toDuration throws ArithmeticException, but both seem author-culture-consistent. Perhaps TimeUnit#toDuration doesn't provide enough value in view of the existing Duration.of and TimeUnit#toChronoUnit. And most of the time you'd expect to convert from Duration to long, just before calling a TimeUnit based method.
/** * Converts the given time duration to this unit. * * @param duration the time duration * @return the converted duration in this unit, * or {@code Long.MIN_VALUE} if conversion would negatively overflow, * or {@code Long.MAX_VALUE} if it would positively overflow. * @throws NullPointerException if {@code duration} is null */ public long convert(Duration duration) { long s = convert(duration.getSeconds(), SECONDS); if (s == Long.MIN_VALUE) return s; long n = convert(duration.getNano(), NANOSECONDS); assert n >= 0 && n < 1_000_000_000; return (s + n < s) ? Long.MAX_VALUE : s + n; }
/** * Converts the given time duration in this unit to a Duration. * * @param duration the time duration * @return the time duration represented as a Duration * @throws ArithmeticException if the duration cannot be represented * as a Duration due to numeric overflow */ public Duration toDuration(long duration) { return Duration.of(duration, toChronoUnit()); }
_______________________________________________ Concurrency-interest mailing list Concurrency-interest@cs.oswego.edu http://cs.oswego.edu/mailman/listinfo/concurrency-interest
On Wed, May 30, 2018 at 7:43 PM, Gregg Wonderly <gergg@cox.net> wrote:
I am not sure I understand this implementation, but isn’t
long s = convert(duration.getSeconds(), SECONDS);
needing to actually be
long s = convert(duration.getSeconds(), NANOSECONDS);
so that s+n is in a common unit of measure?
I haven't actually run this code yet, but from looking at the javadoc both calls to convert give something in "this" unit, so they should agree? You could write a test to check our understanding? Hmmm ... alright ... here's a passing test: /** * tests for conversion between TimeUnit and Duration */ public void testDuration() throws Exception { ThreadLocalRandom rnd = ThreadLocalRandom.current(); long n = rnd.nextLong(); assertEquals(n, NANOSECONDS.convert(Duration.ofNanos(n))); assertEquals(n, MILLISECONDS.convert(Duration.ofMillis(n))); assertEquals(n, SECONDS.convert(Duration.ofSeconds(n))); assertEquals(n /= 60, MINUTES.convert(Duration.ofMinutes(n))); assertEquals(n /= 60, HOURS.convert(Duration.ofHours(n))); assertEquals(n /= 24, DAYS.convert(Duration.ofDays(n))); }
participants (2)
-
Gregg Wonderly
-
Martin Buchholz