8068803: Performance of LocalDate.plusDays could be better

Reviewed-by: rriggs, scolebourne
This commit is contained in:
Nadeesh TV 2016-01-11 11:39:12 -05:00
parent 60015e143f
commit a67a5265d3
2 changed files with 48 additions and 7 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -1369,6 +1369,23 @@ public final class LocalDate
if (daysToAdd == 0) {
return this;
}
long dom = day + daysToAdd;
if (dom > 0) {
if (dom <= 28) {
return new LocalDate(year, month, (int) dom);
} else if (dom <= 59) { // 59th Jan is 28th Feb, 59th Feb is 31st Mar
long monthLen = lengthOfMonth();
if (dom <= monthLen) {
return new LocalDate(year, month, (int) dom);
} else if (month < 12) {
return new LocalDate(year, month + 1, (int) (dom - monthLen));
} else {
YEAR.checkValidValue(year + 1);
return new LocalDate(year + 1, 1, (int) (dom - monthLen));
}
}
}
long mjDay = Math.addExact(toEpochDay(), daysToAdd);
return LocalDate.ofEpochDay(mjDay);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -1285,13 +1285,37 @@ public class TCKLocalDate extends AbstractDateTimeTest {
public void test_plusWeeks_invalidMaxMinusMin() {
LocalDate.of(Year.MAX_VALUE, 12, 25).plusWeeks(Long.MIN_VALUE);
}
@Test
public void test_plusDays_normal() {
LocalDate t = TEST_2007_07_15.plusDays(1);
assertEquals(t, LocalDate.of(2007, 7, 16));
//-----------------------------------------------------------------------
@DataProvider(name="PlusDays")
Object[][] provider_plusDays() {
return new Object[][] {
{LocalDate.of(2007, 7, 15), 1, LocalDate.of(2007, 7, 16)},
{LocalDate.of(2007, 7, 15), 17, LocalDate.of(2007, 8, 1)},
{LocalDate.of(2007, 12, 31), 1, LocalDate.of(2008, 1, 1)},
{LocalDate.of(2007, 1, 1), 58, LocalDate.of(2007, 2, 28)},
{LocalDate.of(2007, 1, 1), 59, LocalDate.of(2007, 3, 1)},
{LocalDate.of(2008, 1, 1), 60, LocalDate.of(2008, 3, 1)},
{LocalDate.of(2007, 2, 1), 27, LocalDate.of(2007, 2, 28)},
{LocalDate.of(2007, 2, 1), 28, LocalDate.of(2007, 3, 1)},
{LocalDate.of(2007, 1, 1), 29, LocalDate.of(2007, 1, 30)},
{LocalDate.of(2007, 1, 1), 30, LocalDate.of(2007, 1, 31)},
{LocalDate.of(2007, 1, 15), 13, LocalDate.of(2007, 1, 28)},
{LocalDate.of(2007, 1, 15), 14, LocalDate.of(2007, 1, 29)},
{LocalDate.of(2007, 1, 15), 15, LocalDate.of(2007, 1, 30)},
{LocalDate.of(2007, 1, 15), 16, LocalDate.of(2007, 1, 31)},
{LocalDate.of(2007, 2, 15), 13, LocalDate.of(2007, 2, 28)},
{LocalDate.of(2007, 2, 15), 14, LocalDate.of(2007, 3, 1)},
{LocalDate.of(2007, 2, 15), 15, LocalDate.of(2007, 3, 2)},
{LocalDate.of(2007, 2, 15), 16, LocalDate.of(2007, 3, 3)},
};
}
@Test(dataProvider="PlusDays")
public void test_plusDays_normal(LocalDate input, int amountsToAdd, LocalDate expected) {
LocalDate actual = input.plusDays(amountsToAdd);
assertEquals(actual, expected);
}
@Test
public void test_plusDays_overMonths() {
LocalDate t = TEST_2007_07_15.plusDays(62);