From a67a5265d3ab1739697c16333d9ddf7ceabe7632 Mon Sep 17 00:00:00 2001 From: Nadeesh TV Date: Mon, 11 Jan 2016 11:39:12 -0500 Subject: [PATCH] 8068803: Performance of LocalDate.plusDays could be better Reviewed-by: rriggs, scolebourne --- .../share/classes/java/time/LocalDate.java | 19 +++++++++- .../java/time/tck/java/time/TCKLocalDate.java | 36 +++++++++++++++---- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/jdk/src/java.base/share/classes/java/time/LocalDate.java b/jdk/src/java.base/share/classes/java/time/LocalDate.java index 0c6c755b813..3d8c034fa91 100644 --- a/jdk/src/java.base/share/classes/java/time/LocalDate.java +++ b/jdk/src/java.base/share/classes/java/time/LocalDate.java @@ -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); } diff --git a/jdk/test/java/time/tck/java/time/TCKLocalDate.java b/jdk/test/java/time/tck/java/time/TCKLocalDate.java index afe7ddcdb58..96290401c97 100644 --- a/jdk/test/java/time/tck/java/time/TCKLocalDate.java +++ b/jdk/test/java/time/tck/java/time/TCKLocalDate.java @@ -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);