rev 12809 : 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math Summary: Add new methods with long, int signatures. Reviewed-by: XXX

```
1 /*
2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
``` ```
785 * @return the day-of-year, from 1 to 365, or 366 in a leap year
786 */
787 public int getDayOfYear() {
788 return getMonth().firstDayOfYear(isLeapYear()) + day - 1;
789 }
790
791 /**
792 * Gets the day-of-week field, which is an enum {@code DayOfWeek}.
793 * <p>
794 * This method returns the enum {@link DayOfWeek} for the day-of-week.
795 * This avoids confusion as to what {@code int} values mean.
796 * If you need access to the primitive {@code int} value then the enum
797 * provides the {@link DayOfWeek#getValue() int value}.
798 * <p>
799 * Additional information can be obtained from the {@code DayOfWeek}.
800 * This includes textual names of the values.
801 *
802 * @return the day-of-week, not null
803 */
804 public DayOfWeek getDayOfWeek() {
805 int dow0 = (int)Math.floorMod(toEpochDay() + 3, 7);
806 return DayOfWeek.of(dow0 + 1);
807 }
808
809 //-----------------------------------------------------------------------
810 /**
811 * Checks if the year is a leap year, according to the ISO proleptic
812 * calendar system rules.
813 * <p>
814 * This method applies the current rules for leap years across the whole time-line.
815 * In general, a year is a leap year if it is divisible by four without
816 * remainder. However, years divisible by 100, are not leap years, with
817 * the exception of years divisible by 400 which are.
818 * <p>
819 * For example, 1904 is a leap year it is divisible by 4.
820 * 1900 was not a leap year as it is divisible by 100, however 2000 was a
821 * leap year as it is divisible by 400.
822 * <p>
823 * The calculation is proleptic - applying the same rules into the far future and far past.
824 * This is historically inaccurate, but is correct for the ISO-8601 standard.
825 *
``` ```
1288 * <li>Adjust the day-of-month to the last valid day if necessary</li>
1289 * </ol>
1290 * <p>
1291 * For example, 2007-03-31 plus one month would result in the invalid date
1292 * 2007-04-31. Instead of returning an invalid result, the last valid day
1293 * of the month, 2007-04-30, is selected instead.
1294 * <p>
1295 * This instance is immutable and unaffected by this method call.
1296 *
1297 * @param monthsToAdd the months to add, may be negative
1298 * @return a {@code LocalDate} based on this date with the months added, not null
1299 * @throws DateTimeException if the result exceeds the supported date range
1300 */
1301 public LocalDate plusMonths(long monthsToAdd) {
1302 if (monthsToAdd == 0) {
1303 return this;
1304 }
1305 long monthCount = year * 12L + (month - 1);
1306 long calcMonths = monthCount + monthsToAdd; // safe overflow
1307 int newYear = YEAR.checkValidIntValue(Math.floorDiv(calcMonths, 12));
1308 int newMonth = (int)Math.floorMod(calcMonths, 12) + 1;
1309 return resolvePreviousValid(newYear, newMonth, day);
1310 }
1311
1312 /**
1313 * Returns a copy of this {@code LocalDate} with the specified number of weeks added.
1314 * <p>
1315 * This method adds the specified amount in weeks to the days field incrementing
1316 * the month and year fields as necessary to ensure the result remains valid.
1317 * The result is only invalid if the maximum/minimum year is exceeded.
1318 * <p>
1319 * For example, 2008-12-31 plus one week would result in 2009-01-07.
1320 * <p>
1321 * This instance is immutable and unaffected by this method call.
1322 *
1323 * @param weeksToAdd the weeks to add, may be negative
1324 * @return a {@code LocalDate} based on this date with the weeks added, not null
1325 * @throws DateTimeException if the result exceeds the supported date range
1326 */
1327 public LocalDate plusWeeks(long weeksToAdd) {
1328 return plusDays(Math.multiplyExact(weeksToAdd, 7));
``` | ```
1 /*
2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
``` ```
785 * @return the day-of-year, from 1 to 365, or 366 in a leap year
786 */
787 public int getDayOfYear() {
788 return getMonth().firstDayOfYear(isLeapYear()) + day - 1;
789 }
790
791 /**
792 * Gets the day-of-week field, which is an enum {@code DayOfWeek}.
793 * <p>
794 * This method returns the enum {@link DayOfWeek} for the day-of-week.
795 * This avoids confusion as to what {@code int} values mean.
796 * If you need access to the primitive {@code int} value then the enum
797 * provides the {@link DayOfWeek#getValue() int value}.
798 * <p>
799 * Additional information can be obtained from the {@code DayOfWeek}.
800 * This includes textual names of the values.
801 *
802 * @return the day-of-week, not null
803 */
804 public DayOfWeek getDayOfWeek() {
805 int dow0 = Math.floorMod(toEpochDay() + 3, 7);
806 return DayOfWeek.of(dow0 + 1);
807 }
808
809 //-----------------------------------------------------------------------
810 /**
811 * Checks if the year is a leap year, according to the ISO proleptic
812 * calendar system rules.
813 * <p>
814 * This method applies the current rules for leap years across the whole time-line.
815 * In general, a year is a leap year if it is divisible by four without
816 * remainder. However, years divisible by 100, are not leap years, with
817 * the exception of years divisible by 400 which are.
818 * <p>
819 * For example, 1904 is a leap year it is divisible by 4.
820 * 1900 was not a leap year as it is divisible by 100, however 2000 was a
821 * leap year as it is divisible by 400.
822 * <p>
823 * The calculation is proleptic - applying the same rules into the far future and far past.
824 * This is historically inaccurate, but is correct for the ISO-8601 standard.
825 *
``` ```
1288 * <li>Adjust the day-of-month to the last valid day if necessary</li>
1289 * </ol>
1290 * <p>
1291 * For example, 2007-03-31 plus one month would result in the invalid date
1292 * 2007-04-31. Instead of returning an invalid result, the last valid day
1293 * of the month, 2007-04-30, is selected instead.
1294 * <p>
1295 * This instance is immutable and unaffected by this method call.
1296 *
1297 * @param monthsToAdd the months to add, may be negative
1298 * @return a {@code LocalDate} based on this date with the months added, not null
1299 * @throws DateTimeException if the result exceeds the supported date range
1300 */
1301 public LocalDate plusMonths(long monthsToAdd) {
1302 if (monthsToAdd == 0) {
1303 return this;
1304 }
1305 long monthCount = year * 12L + (month - 1);
1306 long calcMonths = monthCount + monthsToAdd; // safe overflow
1307 int newYear = YEAR.checkValidIntValue(Math.floorDiv(calcMonths, 12));
1308 int newMonth = Math.floorMod(calcMonths, 12) + 1;
1309 return resolvePreviousValid(newYear, newMonth, day);
1310 }
1311
1312 /**
1313 * Returns a copy of this {@code LocalDate} with the specified number of weeks added.
1314 * <p>
1315 * This method adds the specified amount in weeks to the days field incrementing
1316 * the month and year fields as necessary to ensure the result remains valid.
1317 * The result is only invalid if the maximum/minimum year is exceeded.
1318 * <p>
1319 * For example, 2008-12-31 plus one week would result in 2009-01-07.
1320 * <p>
1321 * This instance is immutable and unaffected by this method call.
1322 *
1323 * @param weeksToAdd the weeks to add, may be negative
1324 * @return a {@code LocalDate} based on this date with the weeks added, not null
1325 * @throws DateTimeException if the result exceeds the supported date range
1326 */
1327 public LocalDate plusWeeks(long weeksToAdd) {
1328 return plusDays(Math.multiplyExact(weeksToAdd, 7));
``` |