--- old/src/java.base/share/classes/java/util/concurrent/TimeUnit.java 2016-01-27 19:25:49.657806484 +0300 +++ new/src/java.base/share/classes/java/util/concurrent/TimeUnit.java 2016-01-27 19:25:49.438806484 +0300 @@ -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,62 @@ } } + /** + * Converts this {@code TimeUnit} to an equivalent {@code ChronoUnit}. + * + * @return the converted equivalent ChronoUnit + * @throws IllegalArgumentException if this TimeUnit cannot be converted + * @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 IllegalArgumentException("Unknown TimeUnit"); + } + } + + /** + * Converts a {@code ChronoUnit} to an equivalent {@code TimeUnit}. + * + * @param chronoUnit the ChronoUnit to convert, not null + * @return the converted equivalent TimeUnit + * @throws IllegalArgumentException if {@code chronoUnit} cannot be converted + * @since 9 + */ + public static TimeUnit of(ChronoUnit chronoUnit) { + Objects.requireNonNull(chronoUnit, "chronoUnit"); + switch (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); + } + } + } --- old/test/java/util/concurrent/TimeUnit/Basic.java 2016-01-27 19:25:50.443806483 +0300 +++ new/test/java/util/concurrent/TimeUnit/Basic.java 2016-01-27 19:25:50.164806483 +0300 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -22,7 +22,7 @@ */ /* @test - * @bug 5057341 6363898 + * @bug 5057341 6363898 8141452 * @summary Basic tests for TimeUnit * @author Martin Buchholz */ @@ -41,6 +41,7 @@ import java.io.InputStream; import java.io.ObjectOutputStream; import java.io.ObjectInputStream; +import java.time.temporal.ChronoUnit; import java.util.Arrays; import java.util.concurrent.TimeUnit; @@ -117,4 +118,55 @@ InputStream is = new ByteArrayInputStream(bytes); return new ObjectInputStream(is).readObject(); } + + //----------------------------------------------------------------------- + // toChronoUnit(), of() + //-----------------------------------------------------------------------` + @DataProvider(name = "convertedUnit") + Object[][] data_convertedUnit() { + return new Object[][] { + {NANOSECONDS, ChronoUnit.NANOS, true}, + {MICROSECONDS, ChronoUnit.MICROS, true}, + {MILLISECONDS, ChronoUnit.MILLIS, true}, + {SECONDS, ChronoUnit.SECONDS, true}, + {MINUTES, ChronoUnit.MINUTES, true}, + {HOURS, ChronoUnit.HOURS, true}, + {DAYS, ChronoUnit.DAYS, true}, + + {NANOSECONDS, ChronoUnit.MINUTES, false}, + {MICROSECONDS, ChronoUnit.SECONDS, false}, + {MILLISECONDS, ChronoUnit.HOURS, false}, + {SECONDS, ChronoUnit.MICROS, false}, + {MINUTES, ChronoUnit.MILLIS, false}, + {HOURS, ChronoUnit.DAYS, false}, + {DAYS, ChronoUnit.NANOS, false}, + }; + } + + @Test(dataProvider = "convertedUnit") + public void test_toChronoUnit(TimeUnit timeUnit, ChronoUnit chronoUnit, boolean expected) { + assertEquals(TimeUnit.toChronoUnit(timeUnit).equals(chronoUnit), expected); + } + + @Test(expectedExceptions=NullPointerException.class) + public void test_toChronoUnit_null() { + TimeUnit.toChronoUnit(null); + } + + @Test(dataProvider = "convertedUnit") + public void test_of(TimeUnit timeUnit, ChronoUnit chronoUnit, boolean expected) { + assertEquals(TimeUnit.of(chronoUnit).equals(timeUnit), expected); + + } + + @Test(expectedExceptions=NullPointerException.class) + public void test_of_null() { + TimeUnit.of(null); + } + + @Test(expectedExceptions=IllegalArgumentException.class) + public void test_of_illegalArg() { + TimeUnit.of(ChronoUnit.ERAS); + + } }