src/java.base/share/classes/java/time/YearMonth.java

Print this page
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


 833         int newYear = YEAR.checkValidIntValue(year + yearsToAdd);  // safe overflow
 834         return with(newYear, month);
 835     }
 836 
 837     /**
 838      * Returns a copy of this {@code YearMonth} with the specified number of months added.
 839      * <p>
 840      * This instance is immutable and unaffected by this method call.
 841      *
 842      * @param monthsToAdd  the months to add, may be negative
 843      * @return a {@code YearMonth} based on this year-month with the months added, not null
 844      * @throws DateTimeException if the result exceeds the supported range
 845      */
 846     public YearMonth plusMonths(long monthsToAdd) {
 847         if (monthsToAdd == 0) {
 848             return this;
 849         }
 850         long monthCount = year * 12L + (month - 1);
 851         long calcMonths = monthCount + monthsToAdd;  // safe overflow
 852         int newYear = YEAR.checkValidIntValue(Math.floorDiv(calcMonths, 12));
 853         int newMonth = (int)Math.floorMod(calcMonths, 12) + 1;
 854         return with(newYear, newMonth);
 855     }
 856 
 857     //-----------------------------------------------------------------------
 858     /**
 859      * Returns a copy of this year-month with the specified amount subtracted.
 860      * <p>
 861      * This returns a {@code YearMonth}, based on this one, with the specified amount subtracted.
 862      * The amount is typically {@link Period} but may be any other type implementing
 863      * the {@link TemporalAmount} interface.
 864      * <p>
 865      * The calculation is delegated to the amount object by calling
 866      * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
 867      * to implement the subtraction in any way it wishes, however it typically
 868      * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
 869      * of the amount implementation to determine if it can be successfully subtracted.
 870      * <p>
 871      * This instance is immutable and unaffected by this method call.
 872      *
 873      * @param amountToSubtract  the amount to subtract, not null


   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


 833         int newYear = YEAR.checkValidIntValue(year + yearsToAdd);  // safe overflow
 834         return with(newYear, month);
 835     }
 836 
 837     /**
 838      * Returns a copy of this {@code YearMonth} with the specified number of months added.
 839      * <p>
 840      * This instance is immutable and unaffected by this method call.
 841      *
 842      * @param monthsToAdd  the months to add, may be negative
 843      * @return a {@code YearMonth} based on this year-month with the months added, not null
 844      * @throws DateTimeException if the result exceeds the supported range
 845      */
 846     public YearMonth plusMonths(long monthsToAdd) {
 847         if (monthsToAdd == 0) {
 848             return this;
 849         }
 850         long monthCount = year * 12L + (month - 1);
 851         long calcMonths = monthCount + monthsToAdd;  // safe overflow
 852         int newYear = YEAR.checkValidIntValue(Math.floorDiv(calcMonths, 12));
 853         int newMonth = Math.floorMod(calcMonths, 12) + 1;
 854         return with(newYear, newMonth);
 855     }
 856 
 857     //-----------------------------------------------------------------------
 858     /**
 859      * Returns a copy of this year-month with the specified amount subtracted.
 860      * <p>
 861      * This returns a {@code YearMonth}, based on this one, with the specified amount subtracted.
 862      * The amount is typically {@link Period} but may be any other type implementing
 863      * the {@link TemporalAmount} interface.
 864      * <p>
 865      * The calculation is delegated to the amount object by calling
 866      * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
 867      * to implement the subtraction in any way it wishes, however it typically
 868      * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
 869      * of the amount implementation to determine if it can be successfully subtracted.
 870      * <p>
 871      * This instance is immutable and unaffected by this method call.
 872      *
 873      * @param amountToSubtract  the amount to subtract, not null