8068803: Performance of LocalDate.plusDays could be better
authorntv
Mon, 11 Jan 2016 11:39:12 -0500
changeset 34945 177014e6bc13
parent 34944 c562e2db2c98
child 34946 33b5b6fa6f5a
8068803: Performance of LocalDate.plusDays could be better Reviewed-by: rriggs, scolebourne
jdk/src/java.base/share/classes/java/time/LocalDate.java
jdk/test/java/time/tck/java/time/TCKLocalDate.java
--- a/jdk/src/java.base/share/classes/java/time/LocalDate.java	Mon Jan 11 17:19:16 2016 +0100
+++ b/jdk/src/java.base/share/classes/java/time/LocalDate.java	Mon Jan 11 11:39:12 2016 -0500
@@ -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 @@
         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);
     }
--- a/jdk/test/java/time/tck/java/time/TCKLocalDate.java	Mon Jan 11 17:19:16 2016 +0100
+++ b/jdk/test/java/time/tck/java/time/TCKLocalDate.java	Mon Jan 11 11:39:12 2016 -0500
@@ -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,12 +1285,36 @@
     public void test_plusWeeks_invalidMaxMinusMin() {
         LocalDate.of(Year.MAX_VALUE, 12, 25).plusWeeks(Long.MIN_VALUE);
     }
+    //-----------------------------------------------------------------------
+    @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
-    public void test_plusDays_normal() {
-        LocalDate t = TEST_2007_07_15.plusDays(1);
-        assertEquals(t, LocalDate.of(2007, 7, 16));
-    }
+    @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() {