8073394: Clock.systemUTC() should return a constant
authordfuchs
Tue, 24 Feb 2015 21:51:45 +0100
changeset 29108 57b3b196d3a6
parent 29107 e9b83d4118d8
child 29109 f37e7cc91bac
8073394: Clock.systemUTC() should return a constant Summary: Clock.systemUTC() now returns SystemClock.UTC Reviewed-by: scolebourne, rriggs, plevart, lancea
jdk/src/java.base/share/classes/java/time/Clock.java
jdk/test/java/time/test/java/time/TestClock_System.java
--- a/jdk/src/java.base/share/classes/java/time/Clock.java	Tue Feb 24 10:52:02 2015 -0800
+++ b/jdk/src/java.base/share/classes/java/time/Clock.java	Tue Feb 24 21:51:45 2015 +0100
@@ -155,7 +155,7 @@
      * @return a clock that uses the best available system clock in the UTC zone, not null
      */
     public static Clock systemUTC() {
-        return new SystemClock(ZoneOffset.UTC);
+        return SystemClock.UTC;
     }
 
     /**
@@ -198,6 +198,9 @@
      */
     public static Clock system(ZoneId zone) {
         Objects.requireNonNull(zone, "zone");
+        if (zone == ZoneOffset.UTC) {
+            return SystemClock.UTC;
+        }
         return new SystemClock(zone);
     }
 
@@ -451,6 +454,8 @@
         private static final long serialVersionUID = 6740630888130243051L;
         private static final long OFFSET_SEED =
                 System.currentTimeMillis()/1000 - 1024; // initial offest
+        static final SystemClock UTC = new SystemClock(ZoneOffset.UTC);
+
         private final ZoneId zone;
         // We don't actually need a volatile here.
         // We don't care if offset is set or read concurrently by multiple
--- a/jdk/test/java/time/test/java/time/TestClock_System.java	Tue Feb 24 10:52:02 2015 -0800
+++ b/jdk/test/java/time/test/java/time/TestClock_System.java	Tue Feb 24 21:51:45 2015 +0100
@@ -66,8 +66,10 @@
 import java.time.Clock;
 import java.time.Instant;
 import java.time.ZoneId;
+import java.time.ZoneOffset;
 
 import org.testng.annotations.Test;
+import org.testng.annotations.DataProvider;
 
 /**
  * Test system clock.
@@ -76,6 +78,7 @@
 public class TestClock_System {
 
     private static final ZoneId PARIS = ZoneId.of("Europe/Paris");
+    private static final Clock systemUTC = Clock.systemUTC();
 
     public void test_withZone_same() {
         Clock test = Clock.system(PARIS);
@@ -90,6 +93,32 @@
     }
 
     //-----------------------------------------------------------------------
+    @DataProvider(name="sampleSystemUTC")
+    Object[][] provider_sampleSystemUTC() {
+        return new Object[][] {
+            {"Clock.systemUTC()#1",  Clock.systemUTC()},
+            {"Clock.systemUTC()#2",  Clock.systemUTC()},
+            {"Clock.system(ZoneOffset.UTC)#1",  Clock.system(ZoneOffset.UTC)},
+            {"Clock.system(ZoneOffset.UTC)#2",  Clock.system(ZoneOffset.UTC)}
+        };
+    }
+
+    // Test for 8073394
+    @Test(dataProvider="sampleSystemUTC")
+    public void test_systemUTC(String s, Clock clock) {
+        if (clock != systemUTC) {
+            throw new RuntimeException("Unexpected clock instance for " + s + ": "
+                + "\n\texpected: " + toString(systemUTC)
+                + "\n\tactual:   " + toString(clock));
+        }
+    }
+
+    private static String toString(Clock c) {
+        return c == null ? null :
+               c + " " + c.getClass().getName() + "@" + System.identityHashCode(c);
+    }
+
+    //-----------------------------------------------------------------------
 
     private static String formatTime(String prefix, Instant time) {
         return prefix + ": " + time + " - seconds: "