ThreeTen / threeten

This project was the home of code used to develop a modern date and time library for JDK8. Development has moved to OpenJDK and a separate backport project, threetenbp.
http://threeten.github.io/
191 stars 37 forks source link

Optimise now() factory method on chronology date classes #324

Closed jodastephen closed 11 years ago

jodastephen commented 11 years ago

The now() factory methods could be slightly optimised.

jodastephen commented 11 years ago
# HG changeset patch
# User scolebourne
# Date 1372350271 -3600
# Node ID 58935f771ca84303bd726eb468af57d11444a5d5
# Parent  73a95812438c7f7f95eb7b4d26ac74f98474edaa
Enhance chronology date class now() methods
See #324

diff --git a/src/share/classes/java/time/chrono/HijrahDate.java b/src/share/classes/java/time/chrono/HijrahDate.java
--- a/src/share/classes/java/time/chrono/HijrahDate.java
+++ b/src/share/classes/java/time/chrono/HijrahDate.java
@@ -204,7 +204,7 @@
      * @throws DateTimeException if the current date cannot be obtained
      */
     public static HijrahDate now(Clock clock) {
-        return HijrahChronology.INSTANCE.date(LocalDate.now(clock));
+        return HijrahDate.ofEpochDay(HijrahChronology.INSTANCE, LocalDate.now(clock).toEpochDay());
     }

     /**
diff --git a/src/share/classes/java/time/chrono/JapaneseDate.java b/src/share/classes/java/time/chrono/JapaneseDate.java
--- a/src/share/classes/java/time/chrono/JapaneseDate.java
+++ b/src/share/classes/java/time/chrono/JapaneseDate.java
@@ -174,7 +174,7 @@
      * @throws DateTimeException if the current date cannot be obtained
      */
     public static JapaneseDate now(Clock clock) {
-        return JapaneseChronology.INSTANCE.date(LocalDate.now(clock));
+        return new JapaneseDate(LocalDate.now(clock));
     }

     /**
diff --git a/src/share/classes/java/time/chrono/MinguoDate.java b/src/share/classes/java/time/chrono/MinguoDate.java
--- a/src/share/classes/java/time/chrono/MinguoDate.java
+++ b/src/share/classes/java/time/chrono/MinguoDate.java
@@ -152,7 +152,7 @@
      * @throws DateTimeException if the current date cannot be obtained
      */
     public static MinguoDate now(Clock clock) {
-        return MinguoChronology.INSTANCE.date(LocalDate.now(clock));
+        return new MinguoDate(LocalDate.now(clock));
     }

     /**
@@ -370,6 +370,11 @@
     }

     @Override
+    MinguoDate plusWeeks(long weeksToAdd) {
+        return super.plusWeeks(weeksToAdd);
+    }
+
+    @Override
     MinguoDate plusDays(long days) {
         return with(isoDate.plusDays(days));
     }
@@ -385,11 +390,6 @@
     }

     @Override
-    MinguoDate plusWeeks(long weeksToAdd) {
-        return super.plusWeeks(weeksToAdd);
-    }
-
-    @Override
     MinguoDate minusYears(long yearsToSubtract) {
         return super.minusYears(yearsToSubtract);
     }
diff --git a/src/share/classes/java/time/chrono/ThaiBuddhistDate.java b/src/share/classes/java/time/chrono/ThaiBuddhistDate.java
--- a/src/share/classes/java/time/chrono/ThaiBuddhistDate.java
+++ b/src/share/classes/java/time/chrono/ThaiBuddhistDate.java
@@ -152,7 +152,7 @@
      * @throws DateTimeException if the current date cannot be obtained
      */
     public static ThaiBuddhistDate now(Clock clock) {
-        return ThaiBuddhistChronology.INSTANCE.date(LocalDate.now(clock));
+        return new ThaiBuddhistDate(LocalDate.now(clock));
     }

     /**
RogerRiggs commented 11 years ago

looks fine.

jodastephen commented 11 years ago

Fixed by http://hg.openjdk.java.net/threeten/threeten/jdk/rev/58935f771ca8