changeset 8300:1de0fac9b962

8023764: Optimize Period addition Summary: Optimise plus/minus for common cases Reviewed-by: sherman Contributed-by: scolebourne@joda.org
author rriggs
date Thu, 29 Aug 2013 20:38:46 +0100
parents b5aad88cbf12
children 356df1b99976
files src/share/classes/java/time/LocalDate.java src/share/classes/java/time/LocalDateTime.java src/share/classes/java/time/ZonedDateTime.java
diffstat 3 files changed, 30 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/time/LocalDate.java	Fri Oct 04 16:05:55 2013 +0100
+++ b/src/share/classes/java/time/LocalDate.java	Thu Aug 29 20:38:46 2013 +0100
@@ -1125,6 +1125,11 @@
      */
     @Override
     public LocalDate plus(TemporalAmount amountToAdd) {
+        Objects.requireNonNull(amountToAdd, "amountToAdd");
+        if (amountToAdd instanceof Period) {
+            Period periodToAdd = (Period) amountToAdd;
+            return plusMonths(periodToAdd.toTotalMonths()).plusDays(periodToAdd.getDays());
+        }
         return (LocalDate) amountToAdd.addTo(this);
     }
 
@@ -1353,6 +1358,11 @@
      */
     @Override
     public LocalDate minus(TemporalAmount amountToSubtract) {
+        Objects.requireNonNull(amountToSubtract, "amountToSubtract");
+        if (amountToSubtract instanceof Period) {
+            Period periodToSubtract = (Period) amountToSubtract;
+            return minusMonths(periodToSubtract.toTotalMonths()).minusDays(periodToSubtract.getDays());
+        }
         return (LocalDate) amountToSubtract.subtractFrom(this);
     }
 
--- a/src/share/classes/java/time/LocalDateTime.java	Fri Oct 04 16:05:55 2013 +0100
+++ b/src/share/classes/java/time/LocalDateTime.java	Thu Aug 29 20:38:46 2013 +0100
@@ -1129,6 +1129,11 @@
      */
     @Override
     public LocalDateTime plus(TemporalAmount amountToAdd) {
+        Objects.requireNonNull(amountToAdd, "amountToAdd");
+        if (amountToAdd instanceof Period) {
+            Period periodToAdd = (Period) amountToAdd;
+            return with(date.plus(periodToAdd), time);
+        }
         return (LocalDateTime) amountToAdd.addTo(this);
     }
 
@@ -1343,6 +1348,11 @@
      */
     @Override
     public LocalDateTime minus(TemporalAmount amountToSubtract) {
+        Objects.requireNonNull(amountToSubtract, "amountToSubtract");
+        if (amountToSubtract instanceof Period) {
+            Period periodToSubtract = (Period) amountToSubtract;
+            return with(date.minus(periodToSubtract), time);
+        }
         return (LocalDateTime) amountToSubtract.subtractFrom(this);
     }
 
--- a/src/share/classes/java/time/ZonedDateTime.java	Fri Oct 04 16:05:55 2013 +0100
+++ b/src/share/classes/java/time/ZonedDateTime.java	Thu Aug 29 20:38:46 2013 +0100
@@ -1540,6 +1540,11 @@
      */
     @Override
     public ZonedDateTime plus(TemporalAmount amountToAdd) {
+        Objects.requireNonNull(amountToAdd, "amountToAdd");
+        if (amountToAdd instanceof Period) {
+            Period periodToAdd = (Period) amountToAdd;
+            return resolveLocal(dateTime.plus(periodToAdd));
+        }
         return (ZonedDateTime) amountToAdd.addTo(this);
     }
 
@@ -1787,6 +1792,11 @@
      */
     @Override
     public ZonedDateTime minus(TemporalAmount amountToSubtract) {
+        Objects.requireNonNull(amountToSubtract, "amountToSubtract");
+        if (amountToSubtract instanceof Period) {
+            Period periodToSubtract = (Period) amountToSubtract;
+            return resolveLocal(dateTime.minus(periodToSubtract));
+        }
         return (ZonedDateTime) amountToSubtract.subtractFrom(this);
     }