changeset 11490:13c749cb2edf

8073394: Clock.systemUTC() should return a constant Summary: Clock.systemUTC() now returns SystemClock.UTC Reviewed-by: scolebourne, rriggs, plevart, lancea
author dfuchs
date Tue, 24 Feb 2015 21:51:45 +0100
parents d5f7442dbd63
children f5a9533bd747
files src/java.base/share/classes/java/time/Clock.java test/java/time/test/java/time/TestClock_System.java
diffstat 2 files changed, 35 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/time/Clock.java	Tue Feb 24 10:52:02 2015 -0800
+++ b/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/test/java/time/test/java/time/TestClock_System.java	Tue Feb 24 10:52:02 2015 -0800
+++ b/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: "