8141452: Convert between TimeUnit and ChronoUnit
Reviewed-by: martin, psandoz, chegar, rriggs, scolebourne
Contributed-by: nadeesh.tv@oracle.com
--- a/jdk/src/java.base/share/classes/java/util/concurrent/TimeUnit.java Tue Feb 02 18:15:15 2016 +0300
+++ b/jdk/src/java.base/share/classes/java/util/concurrent/TimeUnit.java Tue Feb 02 08:53:49 2016 -0800
@@ -35,6 +35,9 @@
package java.util.concurrent;
+import java.time.temporal.ChronoUnit;
+import java.util.Objects;
+
/**
* A {@code TimeUnit} represents time durations at a given unit of
* granularity and provides utility methods to convert across units,
@@ -390,4 +393,48 @@
}
}
+ /**
+ * Converts this {@code TimeUnit} to the equivalent {@code ChronoUnit}.
+ *
+ * @return the converted equivalent ChronoUnit
+ * @since 9
+ */
+ public ChronoUnit toChronoUnit() {
+ switch (this) {
+ case NANOSECONDS: return ChronoUnit.NANOS;
+ case MICROSECONDS: return ChronoUnit.MICROS;
+ case MILLISECONDS: return ChronoUnit.MILLIS;
+ case SECONDS: return ChronoUnit.SECONDS;
+ case MINUTES: return ChronoUnit.MINUTES;
+ case HOURS: return ChronoUnit.HOURS;
+ case DAYS: return ChronoUnit.DAYS;
+ default: throw new AssertionError();
+ }
+ }
+
+ /**
+ * Converts a {@code ChronoUnit} to the equivalent {@code TimeUnit}.
+ *
+ * @param chronoUnit the ChronoUnit to convert
+ * @return the converted equivalent TimeUnit
+ * @throws IllegalArgumentException if {@code chronoUnit} has no
+ * equivalent TimeUnit
+ * @throws NullPointerException if {@code chronoUnit} is null
+ * @since 9
+ */
+ public static TimeUnit of(ChronoUnit chronoUnit) {
+ switch (Objects.requireNonNull(chronoUnit, "chronoUnit")) {
+ case NANOS: return TimeUnit.NANOSECONDS;
+ case MICROS: return TimeUnit.MICROSECONDS;
+ case MILLIS: return TimeUnit.MILLISECONDS;
+ case SECONDS: return TimeUnit.SECONDS;
+ case MINUTES: return TimeUnit.MINUTES;
+ case HOURS: return TimeUnit.HOURS;
+ case DAYS: return TimeUnit.DAYS;
+ default:
+ throw new IllegalArgumentException(
+ "No TimeUnit equivalent for " + chronoUnit);
+ }
+ }
+
}
--- a/jdk/test/java/util/concurrent/tck/TimeUnitTest.java Tue Feb 02 18:15:15 2016 +0300
+++ b/jdk/test/java/util/concurrent/tck/TimeUnitTest.java Tue Feb 02 08:53:49 2016 -0800
@@ -41,6 +41,7 @@
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
+import java.time.temporal.ChronoUnit;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -456,8 +457,53 @@
* a deserialized serialized unit is the same instance
*/
public void testSerialization() throws Exception {
- TimeUnit x = MILLISECONDS;
- assertSame(x, serialClone(x));
+ for (TimeUnit x : TimeUnit.values())
+ assertSame(x, serialClone(x));
+ }
+
+ /**
+ * tests for toChronoUnit.
+ */
+ public void testToChronoUnit() throws Exception {
+ assertSame(ChronoUnit.NANOS, NANOSECONDS.toChronoUnit());
+ assertSame(ChronoUnit.MICROS, MICROSECONDS.toChronoUnit());
+ assertSame(ChronoUnit.MILLIS, MILLISECONDS.toChronoUnit());
+ assertSame(ChronoUnit.SECONDS, SECONDS.toChronoUnit());
+ assertSame(ChronoUnit.MINUTES, MINUTES.toChronoUnit());
+ assertSame(ChronoUnit.HOURS, HOURS.toChronoUnit());
+ assertSame(ChronoUnit.DAYS, DAYS.toChronoUnit());
+
+ // Every TimeUnit has a defined ChronoUnit equivalent
+ for (TimeUnit x : TimeUnit.values())
+ assertSame(x, TimeUnit.of(x.toChronoUnit()));
+ }
+
+ /**
+ * tests for TimeUnit.of(ChronoUnit).
+ */
+ public void testTimeUnitOf() throws Exception {
+ assertSame(NANOSECONDS, TimeUnit.of(ChronoUnit.NANOS));
+ assertSame(MICROSECONDS, TimeUnit.of(ChronoUnit.MICROS));
+ assertSame(MILLISECONDS, TimeUnit.of(ChronoUnit.MILLIS));
+ assertSame(SECONDS, TimeUnit.of(ChronoUnit.SECONDS));
+ assertSame(MINUTES, TimeUnit.of(ChronoUnit.MINUTES));
+ assertSame(HOURS, TimeUnit.of(ChronoUnit.HOURS));
+ assertSame(DAYS, TimeUnit.of(ChronoUnit.DAYS));
+
+ assertThrows(NullPointerException.class,
+ () -> TimeUnit.of((ChronoUnit)null));
+
+ // ChronoUnits either round trip to their TimeUnit
+ // equivalents, or throw IllegalArgumentException.
+ for (ChronoUnit cu : ChronoUnit.values()) {
+ final TimeUnit tu;
+ try {
+ tu = TimeUnit.of(cu);
+ } catch (IllegalArgumentException acceptable) {
+ continue;
+ }
+ assertSame(cu, tu.toChronoUnit());
+ }
}
}