8023764: Optimize Period addition
Summary: Optimise plus/minus for common cases
Reviewed-by: sherman
Contributed-by: scolebourne@joda.org
--- a/jdk/src/share/classes/java/time/LocalDate.java Fri Oct 04 16:05:55 2013 +0100
+++ b/jdk/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/jdk/src/share/classes/java/time/LocalDateTime.java Fri Oct 04 16:05:55 2013 +0100
+++ b/jdk/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/jdk/src/share/classes/java/time/ZonedDateTime.java Fri Oct 04 16:05:55 2013 +0100
+++ b/jdk/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);
}