提交 4a1975b0 编写于 作者: R rriggs

8023764: Optimize Period addition

Summary: Optimise plus/minus for common cases
Reviewed-by: sherman
Contributed-by: scolebourne@joda.org
上级 6d43dca5
...@@ -1125,6 +1125,11 @@ public final class LocalDate ...@@ -1125,6 +1125,11 @@ public final class LocalDate
*/ */
@Override @Override
public LocalDate plus(TemporalAmount amountToAdd) { 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); return (LocalDate) amountToAdd.addTo(this);
} }
...@@ -1353,6 +1358,11 @@ public final class LocalDate ...@@ -1353,6 +1358,11 @@ public final class LocalDate
*/ */
@Override @Override
public LocalDate minus(TemporalAmount amountToSubtract) { 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); return (LocalDate) amountToSubtract.subtractFrom(this);
} }
......
...@@ -1129,6 +1129,11 @@ public final class LocalDateTime ...@@ -1129,6 +1129,11 @@ public final class LocalDateTime
*/ */
@Override @Override
public LocalDateTime plus(TemporalAmount amountToAdd) { 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); return (LocalDateTime) amountToAdd.addTo(this);
} }
...@@ -1343,6 +1348,11 @@ public final class LocalDateTime ...@@ -1343,6 +1348,11 @@ public final class LocalDateTime
*/ */
@Override @Override
public LocalDateTime minus(TemporalAmount amountToSubtract) { 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); return (LocalDateTime) amountToSubtract.subtractFrom(this);
} }
......
...@@ -1540,6 +1540,11 @@ public final class ZonedDateTime ...@@ -1540,6 +1540,11 @@ public final class ZonedDateTime
*/ */
@Override @Override
public ZonedDateTime plus(TemporalAmount amountToAdd) { 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); return (ZonedDateTime) amountToAdd.addTo(this);
} }
...@@ -1787,6 +1792,11 @@ public final class ZonedDateTime ...@@ -1787,6 +1792,11 @@ public final class ZonedDateTime
*/ */
@Override @Override
public ZonedDateTime minus(TemporalAmount amountToSubtract) { 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); return (ZonedDateTime) amountToSubtract.subtractFrom(this);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册