Java Code Examples for javax.xml.datatype.Duration#getField()

The following examples show how to use javax.xml.datatype.Duration#getField() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = NullPointerException.class)
public void checkDurationGetFieldNeg() {
    Duration duration67 = datatypeFactory.newDuration("P1Y1M1DT1H1M1S");
    duration67.getField(null);
}
 
Example 2
Source File: DurationImpl.java    From jdk1.8-source-analysis with Apache License 2.0 2 votes vote down vote up
/**
 * <p>Computes a new duration whose value is <code>this+rhs</code>.</p>
 *
 * <p>For example,</p>
 * <pre>
 * "1 day" + "-3 days" = "-2 days"
 * "1 year" + "1 day" = "1 year and 1 day"
 * "-(1 hour,50 minutes)" + "-20 minutes" = "-(1 hours,70 minutes)"
 * "15 hours" + "-3 days" = "-(2 days,9 hours)"
 * "1 year" + "-1 day" = IllegalStateException
 * </pre>
 *
 * <p>Since there's no way to meaningfully subtract 1 day from 1 month,
 * there are cases where the operation fails in
 * {@link IllegalStateException}.</p>
 *
 * <p>
 * Formally, the computation is defined as follows.</p>
 * <p>
 * Firstly, we can assume that two {@link Duration}s to be added
 * are both positive without losing generality (i.e.,
 * <code>(-X)+Y=Y-X</code>, <code>X+(-Y)=X-Y</code>,
 * <code>(-X)+(-Y)=-(X+Y)</code>)
 *
 * <p>
 * Addition of two positive {@link Duration}s are simply defined as
 * field by field addition where missing fields are treated as 0.
 * <p>
 * A field of the resulting {@link Duration} will be unset if and
 * only if respective fields of two input {@link Duration}s are unset.
 * <p>
 * Note that <code>lhs.add(rhs)</code> will be always successful if
 * <code>lhs.signum()*rhs.signum()!=-1</code> or both of them are
 * normalized.</p>
 *
 * @param rhs <code>Duration</code> to add to this <code>Duration</code>
 *
 * @return
 *      non-null valid Duration object.
 *
 * @throws NullPointerException
 *      If the rhs parameter is null.
 * @throws IllegalStateException
 *      If two durations cannot be meaningfully added. For
 *      example, adding negative one day to one month causes
 *      this exception.
 *
 *
 * @see #subtract(Duration)
 */
public Duration add(final Duration rhs) {
    Duration lhs = this;
    BigDecimal[] buf = new BigDecimal[6];

    buf[0] = sanitize((BigInteger) lhs.getField(DatatypeConstants.YEARS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.YEARS),  rhs.getSign()));
    buf[1] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MONTHS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MONTHS), rhs.getSign()));
    buf[2] = sanitize((BigInteger) lhs.getField(DatatypeConstants.DAYS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.DAYS),   rhs.getSign()));
    buf[3] = sanitize((BigInteger) lhs.getField(DatatypeConstants.HOURS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.HOURS),  rhs.getSign()));
    buf[4] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MINUTES),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MINUTES), rhs.getSign()));
    buf[5] = sanitize((BigDecimal) lhs.getField(DatatypeConstants.SECONDS),
            lhs.getSign()).add(sanitize((BigDecimal) rhs.getField(DatatypeConstants.SECONDS), rhs.getSign()));

    // align sign
    alignSigns(buf, 0, 2); // Y,M
    alignSigns(buf, 2, 6); // D,h,m,s

    // make sure that the sign bit is consistent across all 6 fields.
    int s = 0;
    for (int i = 0; i < 6; i++) {
        if (s * buf[i].signum() < 0) {
            throw new IllegalStateException();
        }
        if (s == 0) {
            s = buf[i].signum();
        }
    }

    return new DurationImpl(
        s >= 0,
        toBigInteger(sanitize(buf[0], s),
            lhs.getField(DatatypeConstants.YEARS) == null && rhs.getField(DatatypeConstants.YEARS) == null),
        toBigInteger(sanitize(buf[1], s),
            lhs.getField(DatatypeConstants.MONTHS) == null && rhs.getField(DatatypeConstants.MONTHS) == null),
        toBigInteger(sanitize(buf[2], s),
            lhs.getField(DatatypeConstants.DAYS) == null && rhs.getField(DatatypeConstants.DAYS) == null),
        toBigInteger(sanitize(buf[3], s),
            lhs.getField(DatatypeConstants.HOURS) == null && rhs.getField(DatatypeConstants.HOURS) == null),
        toBigInteger(sanitize(buf[4], s),
            lhs.getField(DatatypeConstants.MINUTES) == null && rhs.getField(DatatypeConstants.MINUTES) == null),
         (buf[5].signum() == 0
         && lhs.getField(DatatypeConstants.SECONDS) == null
         && rhs.getField(DatatypeConstants.SECONDS) == null) ? null : sanitize(buf[5], s));
}
 
Example 3
Source File: DurationImpl.java    From TencentKona-8 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * <p>Computes a new duration whose value is <code>this+rhs</code>.</p>
 *
 * <p>For example,</p>
 * <pre>
 * "1 day" + "-3 days" = "-2 days"
 * "1 year" + "1 day" = "1 year and 1 day"
 * "-(1 hour,50 minutes)" + "-20 minutes" = "-(1 hours,70 minutes)"
 * "15 hours" + "-3 days" = "-(2 days,9 hours)"
 * "1 year" + "-1 day" = IllegalStateException
 * </pre>
 *
 * <p>Since there's no way to meaningfully subtract 1 day from 1 month,
 * there are cases where the operation fails in
 * {@link IllegalStateException}.</p>
 *
 * <p>
 * Formally, the computation is defined as follows.</p>
 * <p>
 * Firstly, we can assume that two {@link Duration}s to be added
 * are both positive without losing generality (i.e.,
 * <code>(-X)+Y=Y-X</code>, <code>X+(-Y)=X-Y</code>,
 * <code>(-X)+(-Y)=-(X+Y)</code>)
 *
 * <p>
 * Addition of two positive {@link Duration}s are simply defined as
 * field by field addition where missing fields are treated as 0.
 * <p>
 * A field of the resulting {@link Duration} will be unset if and
 * only if respective fields of two input {@link Duration}s are unset.
 * <p>
 * Note that <code>lhs.add(rhs)</code> will be always successful if
 * <code>lhs.signum()*rhs.signum()!=-1</code> or both of them are
 * normalized.</p>
 *
 * @param rhs <code>Duration</code> to add to this <code>Duration</code>
 *
 * @return
 *      non-null valid Duration object.
 *
 * @throws NullPointerException
 *      If the rhs parameter is null.
 * @throws IllegalStateException
 *      If two durations cannot be meaningfully added. For
 *      example, adding negative one day to one month causes
 *      this exception.
 *
 *
 * @see #subtract(Duration)
 */
public Duration add(final Duration rhs) {
    Duration lhs = this;
    BigDecimal[] buf = new BigDecimal[6];

    buf[0] = sanitize((BigInteger) lhs.getField(DatatypeConstants.YEARS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.YEARS),  rhs.getSign()));
    buf[1] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MONTHS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MONTHS), rhs.getSign()));
    buf[2] = sanitize((BigInteger) lhs.getField(DatatypeConstants.DAYS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.DAYS),   rhs.getSign()));
    buf[3] = sanitize((BigInteger) lhs.getField(DatatypeConstants.HOURS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.HOURS),  rhs.getSign()));
    buf[4] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MINUTES),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MINUTES), rhs.getSign()));
    buf[5] = sanitize((BigDecimal) lhs.getField(DatatypeConstants.SECONDS),
            lhs.getSign()).add(sanitize((BigDecimal) rhs.getField(DatatypeConstants.SECONDS), rhs.getSign()));

    // align sign
    alignSigns(buf, 0, 2); // Y,M
    alignSigns(buf, 2, 6); // D,h,m,s

    // make sure that the sign bit is consistent across all 6 fields.
    int s = 0;
    for (int i = 0; i < 6; i++) {
        if (s * buf[i].signum() < 0) {
            throw new IllegalStateException();
        }
        if (s == 0) {
            s = buf[i].signum();
        }
    }

    return new DurationImpl(
        s >= 0,
        toBigInteger(sanitize(buf[0], s),
            lhs.getField(DatatypeConstants.YEARS) == null && rhs.getField(DatatypeConstants.YEARS) == null),
        toBigInteger(sanitize(buf[1], s),
            lhs.getField(DatatypeConstants.MONTHS) == null && rhs.getField(DatatypeConstants.MONTHS) == null),
        toBigInteger(sanitize(buf[2], s),
            lhs.getField(DatatypeConstants.DAYS) == null && rhs.getField(DatatypeConstants.DAYS) == null),
        toBigInteger(sanitize(buf[3], s),
            lhs.getField(DatatypeConstants.HOURS) == null && rhs.getField(DatatypeConstants.HOURS) == null),
        toBigInteger(sanitize(buf[4], s),
            lhs.getField(DatatypeConstants.MINUTES) == null && rhs.getField(DatatypeConstants.MINUTES) == null),
         (buf[5].signum() == 0
         && lhs.getField(DatatypeConstants.SECONDS) == null
         && rhs.getField(DatatypeConstants.SECONDS) == null) ? null : sanitize(buf[5], s));
}
 
Example 4
Source File: DurationImpl.java    From jdk8u60 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * <p>Computes a new duration whose value is <code>this+rhs</code>.</p>
 *
 * <p>For example,</p>
 * <pre>
 * "1 day" + "-3 days" = "-2 days"
 * "1 year" + "1 day" = "1 year and 1 day"
 * "-(1 hour,50 minutes)" + "-20 minutes" = "-(1 hours,70 minutes)"
 * "15 hours" + "-3 days" = "-(2 days,9 hours)"
 * "1 year" + "-1 day" = IllegalStateException
 * </pre>
 *
 * <p>Since there's no way to meaningfully subtract 1 day from 1 month,
 * there are cases where the operation fails in
 * {@link IllegalStateException}.</p>
 *
 * <p>
 * Formally, the computation is defined as follows.</p>
 * <p>
 * Firstly, we can assume that two {@link Duration}s to be added
 * are both positive without losing generality (i.e.,
 * <code>(-X)+Y=Y-X</code>, <code>X+(-Y)=X-Y</code>,
 * <code>(-X)+(-Y)=-(X+Y)</code>)
 *
 * <p>
 * Addition of two positive {@link Duration}s are simply defined as
 * field by field addition where missing fields are treated as 0.
 * <p>
 * A field of the resulting {@link Duration} will be unset if and
 * only if respective fields of two input {@link Duration}s are unset.
 * <p>
 * Note that <code>lhs.add(rhs)</code> will be always successful if
 * <code>lhs.signum()*rhs.signum()!=-1</code> or both of them are
 * normalized.</p>
 *
 * @param rhs <code>Duration</code> to add to this <code>Duration</code>
 *
 * @return
 *      non-null valid Duration object.
 *
 * @throws NullPointerException
 *      If the rhs parameter is null.
 * @throws IllegalStateException
 *      If two durations cannot be meaningfully added. For
 *      example, adding negative one day to one month causes
 *      this exception.
 *
 *
 * @see #subtract(Duration)
 */
public Duration add(final Duration rhs) {
    Duration lhs = this;
    BigDecimal[] buf = new BigDecimal[6];

    buf[0] = sanitize((BigInteger) lhs.getField(DatatypeConstants.YEARS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.YEARS),  rhs.getSign()));
    buf[1] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MONTHS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MONTHS), rhs.getSign()));
    buf[2] = sanitize((BigInteger) lhs.getField(DatatypeConstants.DAYS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.DAYS),   rhs.getSign()));
    buf[3] = sanitize((BigInteger) lhs.getField(DatatypeConstants.HOURS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.HOURS),  rhs.getSign()));
    buf[4] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MINUTES),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MINUTES), rhs.getSign()));
    buf[5] = sanitize((BigDecimal) lhs.getField(DatatypeConstants.SECONDS),
            lhs.getSign()).add(sanitize((BigDecimal) rhs.getField(DatatypeConstants.SECONDS), rhs.getSign()));

    // align sign
    alignSigns(buf, 0, 2); // Y,M
    alignSigns(buf, 2, 6); // D,h,m,s

    // make sure that the sign bit is consistent across all 6 fields.
    int s = 0;
    for (int i = 0; i < 6; i++) {
        if (s * buf[i].signum() < 0) {
            throw new IllegalStateException();
        }
        if (s == 0) {
            s = buf[i].signum();
        }
    }

    return new DurationImpl(
        s >= 0,
        toBigInteger(sanitize(buf[0], s),
            lhs.getField(DatatypeConstants.YEARS) == null && rhs.getField(DatatypeConstants.YEARS) == null),
        toBigInteger(sanitize(buf[1], s),
            lhs.getField(DatatypeConstants.MONTHS) == null && rhs.getField(DatatypeConstants.MONTHS) == null),
        toBigInteger(sanitize(buf[2], s),
            lhs.getField(DatatypeConstants.DAYS) == null && rhs.getField(DatatypeConstants.DAYS) == null),
        toBigInteger(sanitize(buf[3], s),
            lhs.getField(DatatypeConstants.HOURS) == null && rhs.getField(DatatypeConstants.HOURS) == null),
        toBigInteger(sanitize(buf[4], s),
            lhs.getField(DatatypeConstants.MINUTES) == null && rhs.getField(DatatypeConstants.MINUTES) == null),
         (buf[5].signum() == 0
         && lhs.getField(DatatypeConstants.SECONDS) == null
         && rhs.getField(DatatypeConstants.SECONDS) == null) ? null : sanitize(buf[5], s));
}
 
Example 5
Source File: DurationImpl.java    From JDKSourceCode1.8 with MIT License 2 votes vote down vote up
/**
 * <p>Computes a new duration whose value is <code>this+rhs</code>.</p>
 *
 * <p>For example,</p>
 * <pre>
 * "1 day" + "-3 days" = "-2 days"
 * "1 year" + "1 day" = "1 year and 1 day"
 * "-(1 hour,50 minutes)" + "-20 minutes" = "-(1 hours,70 minutes)"
 * "15 hours" + "-3 days" = "-(2 days,9 hours)"
 * "1 year" + "-1 day" = IllegalStateException
 * </pre>
 *
 * <p>Since there's no way to meaningfully subtract 1 day from 1 month,
 * there are cases where the operation fails in
 * {@link IllegalStateException}.</p>
 *
 * <p>
 * Formally, the computation is defined as follows.</p>
 * <p>
 * Firstly, we can assume that two {@link Duration}s to be added
 * are both positive without losing generality (i.e.,
 * <code>(-X)+Y=Y-X</code>, <code>X+(-Y)=X-Y</code>,
 * <code>(-X)+(-Y)=-(X+Y)</code>)
 *
 * <p>
 * Addition of two positive {@link Duration}s are simply defined as
 * field by field addition where missing fields are treated as 0.
 * <p>
 * A field of the resulting {@link Duration} will be unset if and
 * only if respective fields of two input {@link Duration}s are unset.
 * <p>
 * Note that <code>lhs.add(rhs)</code> will be always successful if
 * <code>lhs.signum()*rhs.signum()!=-1</code> or both of them are
 * normalized.</p>
 *
 * @param rhs <code>Duration</code> to add to this <code>Duration</code>
 *
 * @return
 *      non-null valid Duration object.
 *
 * @throws NullPointerException
 *      If the rhs parameter is null.
 * @throws IllegalStateException
 *      If two durations cannot be meaningfully added. For
 *      example, adding negative one day to one month causes
 *      this exception.
 *
 *
 * @see #subtract(Duration)
 */
public Duration add(final Duration rhs) {
    Duration lhs = this;
    BigDecimal[] buf = new BigDecimal[6];

    buf[0] = sanitize((BigInteger) lhs.getField(DatatypeConstants.YEARS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.YEARS),  rhs.getSign()));
    buf[1] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MONTHS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MONTHS), rhs.getSign()));
    buf[2] = sanitize((BigInteger) lhs.getField(DatatypeConstants.DAYS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.DAYS),   rhs.getSign()));
    buf[3] = sanitize((BigInteger) lhs.getField(DatatypeConstants.HOURS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.HOURS),  rhs.getSign()));
    buf[4] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MINUTES),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MINUTES), rhs.getSign()));
    buf[5] = sanitize((BigDecimal) lhs.getField(DatatypeConstants.SECONDS),
            lhs.getSign()).add(sanitize((BigDecimal) rhs.getField(DatatypeConstants.SECONDS), rhs.getSign()));

    // align sign
    alignSigns(buf, 0, 2); // Y,M
    alignSigns(buf, 2, 6); // D,h,m,s

    // make sure that the sign bit is consistent across all 6 fields.
    int s = 0;
    for (int i = 0; i < 6; i++) {
        if (s * buf[i].signum() < 0) {
            throw new IllegalStateException();
        }
        if (s == 0) {
            s = buf[i].signum();
        }
    }

    return new DurationImpl(
        s >= 0,
        toBigInteger(sanitize(buf[0], s),
            lhs.getField(DatatypeConstants.YEARS) == null && rhs.getField(DatatypeConstants.YEARS) == null),
        toBigInteger(sanitize(buf[1], s),
            lhs.getField(DatatypeConstants.MONTHS) == null && rhs.getField(DatatypeConstants.MONTHS) == null),
        toBigInteger(sanitize(buf[2], s),
            lhs.getField(DatatypeConstants.DAYS) == null && rhs.getField(DatatypeConstants.DAYS) == null),
        toBigInteger(sanitize(buf[3], s),
            lhs.getField(DatatypeConstants.HOURS) == null && rhs.getField(DatatypeConstants.HOURS) == null),
        toBigInteger(sanitize(buf[4], s),
            lhs.getField(DatatypeConstants.MINUTES) == null && rhs.getField(DatatypeConstants.MINUTES) == null),
         (buf[5].signum() == 0
         && lhs.getField(DatatypeConstants.SECONDS) == null
         && rhs.getField(DatatypeConstants.SECONDS) == null) ? null : sanitize(buf[5], s));
}
 
Example 6
Source File: DurationImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 2 votes vote down vote up
/**
 * <p>Computes a new duration whose value is <code>this+rhs</code>.</p>
 *
 * <p>For example,</p>
 * <pre>
 * "1 day" + "-3 days" = "-2 days"
 * "1 year" + "1 day" = "1 year and 1 day"
 * "-(1 hour,50 minutes)" + "-20 minutes" = "-(1 hours,70 minutes)"
 * "15 hours" + "-3 days" = "-(2 days,9 hours)"
 * "1 year" + "-1 day" = IllegalStateException
 * </pre>
 *
 * <p>Since there's no way to meaningfully subtract 1 day from 1 month,
 * there are cases where the operation fails in
 * {@link IllegalStateException}.</p>
 *
 * <p>
 * Formally, the computation is defined as follows.</p>
 * <p>
 * Firstly, we can assume that two {@link Duration}s to be added
 * are both positive without losing generality (i.e.,
 * <code>(-X)+Y=Y-X</code>, <code>X+(-Y)=X-Y</code>,
 * <code>(-X)+(-Y)=-(X+Y)</code>)
 *
 * <p>
 * Addition of two positive {@link Duration}s are simply defined as
 * field by field addition where missing fields are treated as 0.
 * <p>
 * A field of the resulting {@link Duration} will be unset if and
 * only if respective fields of two input {@link Duration}s are unset.
 * <p>
 * Note that <code>lhs.add(rhs)</code> will be always successful if
 * <code>lhs.signum()*rhs.signum()!=-1</code> or both of them are
 * normalized.</p>
 *
 * @param rhs <code>Duration</code> to add to this <code>Duration</code>
 *
 * @return
 *      non-null valid Duration object.
 *
 * @throws NullPointerException
 *      If the rhs parameter is null.
 * @throws IllegalStateException
 *      If two durations cannot be meaningfully added. For
 *      example, adding negative one day to one month causes
 *      this exception.
 *
 *
 * @see #subtract(Duration)
 */
public Duration add(final Duration rhs) {
    Duration lhs = this;
    BigDecimal[] buf = new BigDecimal[6];

    buf[0] = sanitize((BigInteger) lhs.getField(DatatypeConstants.YEARS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.YEARS),  rhs.getSign()));
    buf[1] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MONTHS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MONTHS), rhs.getSign()));
    buf[2] = sanitize((BigInteger) lhs.getField(DatatypeConstants.DAYS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.DAYS),   rhs.getSign()));
    buf[3] = sanitize((BigInteger) lhs.getField(DatatypeConstants.HOURS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.HOURS),  rhs.getSign()));
    buf[4] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MINUTES),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MINUTES), rhs.getSign()));
    buf[5] = sanitize((BigDecimal) lhs.getField(DatatypeConstants.SECONDS),
            lhs.getSign()).add(sanitize((BigDecimal) rhs.getField(DatatypeConstants.SECONDS), rhs.getSign()));

    // align sign
    alignSigns(buf, 0, 2); // Y,M
    alignSigns(buf, 2, 6); // D,h,m,s

    // make sure that the sign bit is consistent across all 6 fields.
    int s = 0;
    for (int i = 0; i < 6; i++) {
        if (s * buf[i].signum() < 0) {
            throw new IllegalStateException();
        }
        if (s == 0) {
            s = buf[i].signum();
        }
    }

    return new DurationImpl(
        s >= 0,
        toBigInteger(sanitize(buf[0], s),
            lhs.getField(DatatypeConstants.YEARS) == null && rhs.getField(DatatypeConstants.YEARS) == null),
        toBigInteger(sanitize(buf[1], s),
            lhs.getField(DatatypeConstants.MONTHS) == null && rhs.getField(DatatypeConstants.MONTHS) == null),
        toBigInteger(sanitize(buf[2], s),
            lhs.getField(DatatypeConstants.DAYS) == null && rhs.getField(DatatypeConstants.DAYS) == null),
        toBigInteger(sanitize(buf[3], s),
            lhs.getField(DatatypeConstants.HOURS) == null && rhs.getField(DatatypeConstants.HOURS) == null),
        toBigInteger(sanitize(buf[4], s),
            lhs.getField(DatatypeConstants.MINUTES) == null && rhs.getField(DatatypeConstants.MINUTES) == null),
         (buf[5].signum() == 0
         && lhs.getField(DatatypeConstants.SECONDS) == null
         && rhs.getField(DatatypeConstants.SECONDS) == null) ? null : sanitize(buf[5], s));
}
 
Example 7
Source File: DurationImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 2 votes vote down vote up
/**
 * <p>Computes a new duration whose value is <code>this+rhs</code>.</p>
 *
 * <p>For example,</p>
 * <pre>
 * "1 day" + "-3 days" = "-2 days"
 * "1 year" + "1 day" = "1 year and 1 day"
 * "-(1 hour,50 minutes)" + "-20 minutes" = "-(1 hours,70 minutes)"
 * "15 hours" + "-3 days" = "-(2 days,9 hours)"
 * "1 year" + "-1 day" = IllegalStateException
 * </pre>
 *
 * <p>Since there's no way to meaningfully subtract 1 day from 1 month,
 * there are cases where the operation fails in
 * {@link IllegalStateException}.</p>
 *
 * <p>
 * Formally, the computation is defined as follows.</p>
 * <p>
 * Firstly, we can assume that two {@link Duration}s to be added
 * are both positive without losing generality (i.e.,
 * <code>(-X)+Y=Y-X</code>, <code>X+(-Y)=X-Y</code>,
 * <code>(-X)+(-Y)=-(X+Y)</code>)
 *
 * <p>
 * Addition of two positive {@link Duration}s are simply defined as
 * field by field addition where missing fields are treated as 0.
 * <p>
 * A field of the resulting {@link Duration} will be unset if and
 * only if respective fields of two input {@link Duration}s are unset.
 * <p>
 * Note that <code>lhs.add(rhs)</code> will be always successful if
 * <code>lhs.signum()*rhs.signum()!=-1</code> or both of them are
 * normalized.</p>
 *
 * @param rhs <code>Duration</code> to add to this <code>Duration</code>
 *
 * @return
 *      non-null valid Duration object.
 *
 * @throws NullPointerException
 *      If the rhs parameter is null.
 * @throws IllegalStateException
 *      If two durations cannot be meaningfully added. For
 *      example, adding negative one day to one month causes
 *      this exception.
 *
 *
 * @see #subtract(Duration)
 */
public Duration add(final Duration rhs) {
    Duration lhs = this;
    BigDecimal[] buf = new BigDecimal[6];

    buf[0] = sanitize((BigInteger) lhs.getField(DatatypeConstants.YEARS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.YEARS),  rhs.getSign()));
    buf[1] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MONTHS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MONTHS), rhs.getSign()));
    buf[2] = sanitize((BigInteger) lhs.getField(DatatypeConstants.DAYS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.DAYS),   rhs.getSign()));
    buf[3] = sanitize((BigInteger) lhs.getField(DatatypeConstants.HOURS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.HOURS),  rhs.getSign()));
    buf[4] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MINUTES),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MINUTES), rhs.getSign()));
    buf[5] = sanitize((BigDecimal) lhs.getField(DatatypeConstants.SECONDS),
            lhs.getSign()).add(sanitize((BigDecimal) rhs.getField(DatatypeConstants.SECONDS), rhs.getSign()));

    // align sign
    alignSigns(buf, 0, 2); // Y,M
    alignSigns(buf, 2, 6); // D,h,m,s

    // make sure that the sign bit is consistent across all 6 fields.
    int s = 0;
    for (int i = 0; i < 6; i++) {
        if (s * buf[i].signum() < 0) {
            throw new IllegalStateException();
        }
        if (s == 0) {
            s = buf[i].signum();
        }
    }

    return new DurationImpl(
        s >= 0,
        toBigInteger(sanitize(buf[0], s),
            lhs.getField(DatatypeConstants.YEARS) == null && rhs.getField(DatatypeConstants.YEARS) == null),
        toBigInteger(sanitize(buf[1], s),
            lhs.getField(DatatypeConstants.MONTHS) == null && rhs.getField(DatatypeConstants.MONTHS) == null),
        toBigInteger(sanitize(buf[2], s),
            lhs.getField(DatatypeConstants.DAYS) == null && rhs.getField(DatatypeConstants.DAYS) == null),
        toBigInteger(sanitize(buf[3], s),
            lhs.getField(DatatypeConstants.HOURS) == null && rhs.getField(DatatypeConstants.HOURS) == null),
        toBigInteger(sanitize(buf[4], s),
            lhs.getField(DatatypeConstants.MINUTES) == null && rhs.getField(DatatypeConstants.MINUTES) == null),
         (buf[5].signum() == 0
         && lhs.getField(DatatypeConstants.SECONDS) == null
         && rhs.getField(DatatypeConstants.SECONDS) == null) ? null : sanitize(buf[5], s));
}
 
Example 8
Source File: DurationImpl.java    From Bytecoder with Apache License 2.0 2 votes vote down vote up
/**
 * <p>Computes a new duration whose value is <code>this+rhs</code>.</p>
 *
 * <p>For example,</p>
 * <pre>
 * "1 day" + "-3 days" = "-2 days"
 * "1 year" + "1 day" = "1 year and 1 day"
 * "-(1 hour,50 minutes)" + "-20 minutes" = "-(1 hours,70 minutes)"
 * "15 hours" + "-3 days" = "-(2 days,9 hours)"
 * "1 year" + "-1 day" = IllegalStateException
 * </pre>
 *
 * <p>Since there's no way to meaningfully subtract 1 day from 1 month,
 * there are cases where the operation fails in
 * {@link IllegalStateException}.</p>
 *
 * <p>
 * Formally, the computation is defined as follows.</p>
 * <p>
 * Firstly, we can assume that two {@link Duration}s to be added
 * are both positive without losing generality (i.e.,
 * <code>(-X)+Y=Y-X</code>, <code>X+(-Y)=X-Y</code>,
 * <code>(-X)+(-Y)=-(X+Y)</code>)
 *
 * <p>
 * Addition of two positive {@link Duration}s are simply defined as
 * field by field addition where missing fields are treated as 0.
 * <p>
 * A field of the resulting {@link Duration} will be unset if and
 * only if respective fields of two input {@link Duration}s are unset.
 * <p>
 * Note that <code>lhs.add(rhs)</code> will be always successful if
 * <code>lhs.signum()*rhs.signum()!=-1</code> or both of them are
 * normalized.</p>
 *
 * @param rhs <code>Duration</code> to add to this <code>Duration</code>
 *
 * @return
 *      non-null valid Duration object.
 *
 * @throws NullPointerException
 *      If the rhs parameter is null.
 * @throws IllegalStateException
 *      If two durations cannot be meaningfully added. For
 *      example, adding negative one day to one month causes
 *      this exception.
 *
 *
 * @see #subtract(Duration)
 */
public Duration add(final Duration rhs) {
    Duration lhs = this;
    BigDecimal[] buf = new BigDecimal[6];

    buf[0] = sanitize((BigInteger) lhs.getField(DatatypeConstants.YEARS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.YEARS),  rhs.getSign()));
    buf[1] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MONTHS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MONTHS), rhs.getSign()));
    buf[2] = sanitize((BigInteger) lhs.getField(DatatypeConstants.DAYS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.DAYS),   rhs.getSign()));
    buf[3] = sanitize((BigInteger) lhs.getField(DatatypeConstants.HOURS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.HOURS),  rhs.getSign()));
    buf[4] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MINUTES),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MINUTES), rhs.getSign()));
    buf[5] = sanitize((BigDecimal) lhs.getField(DatatypeConstants.SECONDS),
            lhs.getSign()).add(sanitize((BigDecimal) rhs.getField(DatatypeConstants.SECONDS), rhs.getSign()));

    // align sign
    alignSigns(buf, 0, 2); // Y,M
    alignSigns(buf, 2, 6); // D,h,m,s

    // make sure that the sign bit is consistent across all 6 fields.
    int s = 0;
    for (int i = 0; i < 6; i++) {
        if (s * buf[i].signum() < 0) {
            throw new IllegalStateException();
        }
        if (s == 0) {
            s = buf[i].signum();
        }
    }

    return new DurationImpl(
        s >= 0,
        toBigInteger(sanitize(buf[0], s),
            lhs.getField(DatatypeConstants.YEARS) == null && rhs.getField(DatatypeConstants.YEARS) == null),
        toBigInteger(sanitize(buf[1], s),
            lhs.getField(DatatypeConstants.MONTHS) == null && rhs.getField(DatatypeConstants.MONTHS) == null),
        toBigInteger(sanitize(buf[2], s),
            lhs.getField(DatatypeConstants.DAYS) == null && rhs.getField(DatatypeConstants.DAYS) == null),
        toBigInteger(sanitize(buf[3], s),
            lhs.getField(DatatypeConstants.HOURS) == null && rhs.getField(DatatypeConstants.HOURS) == null),
        toBigInteger(sanitize(buf[4], s),
            lhs.getField(DatatypeConstants.MINUTES) == null && rhs.getField(DatatypeConstants.MINUTES) == null),
         (buf[5].signum() == 0
         && lhs.getField(DatatypeConstants.SECONDS) == null
         && rhs.getField(DatatypeConstants.SECONDS) == null) ? null : sanitize(buf[5], s));
}
 
Example 9
Source File: DurationImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * <p>Computes a new duration whose value is <code>this+rhs</code>.</p>
 *
 * <p>For example,</p>
 * <pre>
 * "1 day" + "-3 days" = "-2 days"
 * "1 year" + "1 day" = "1 year and 1 day"
 * "-(1 hour,50 minutes)" + "-20 minutes" = "-(1 hours,70 minutes)"
 * "15 hours" + "-3 days" = "-(2 days,9 hours)"
 * "1 year" + "-1 day" = IllegalStateException
 * </pre>
 *
 * <p>Since there's no way to meaningfully subtract 1 day from 1 month,
 * there are cases where the operation fails in
 * {@link IllegalStateException}.</p>
 *
 * <p>
 * Formally, the computation is defined as follows.</p>
 * <p>
 * Firstly, we can assume that two {@link Duration}s to be added
 * are both positive without losing generality (i.e.,
 * <code>(-X)+Y=Y-X</code>, <code>X+(-Y)=X-Y</code>,
 * <code>(-X)+(-Y)=-(X+Y)</code>)
 *
 * <p>
 * Addition of two positive {@link Duration}s are simply defined as
 * field by field addition where missing fields are treated as 0.
 * <p>
 * A field of the resulting {@link Duration} will be unset if and
 * only if respective fields of two input {@link Duration}s are unset.
 * <p>
 * Note that <code>lhs.add(rhs)</code> will be always successful if
 * <code>lhs.signum()*rhs.signum()!=-1</code> or both of them are
 * normalized.</p>
 *
 * @param rhs <code>Duration</code> to add to this <code>Duration</code>
 *
 * @return
 *      non-null valid Duration object.
 *
 * @throws NullPointerException
 *      If the rhs parameter is null.
 * @throws IllegalStateException
 *      If two durations cannot be meaningfully added. For
 *      example, adding negative one day to one month causes
 *      this exception.
 *
 *
 * @see #subtract(Duration)
 */
public Duration add(final Duration rhs) {
    Duration lhs = this;
    BigDecimal[] buf = new BigDecimal[6];

    buf[0] = sanitize((BigInteger) lhs.getField(DatatypeConstants.YEARS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.YEARS),  rhs.getSign()));
    buf[1] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MONTHS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MONTHS), rhs.getSign()));
    buf[2] = sanitize((BigInteger) lhs.getField(DatatypeConstants.DAYS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.DAYS),   rhs.getSign()));
    buf[3] = sanitize((BigInteger) lhs.getField(DatatypeConstants.HOURS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.HOURS),  rhs.getSign()));
    buf[4] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MINUTES),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MINUTES), rhs.getSign()));
    buf[5] = sanitize((BigDecimal) lhs.getField(DatatypeConstants.SECONDS),
            lhs.getSign()).add(sanitize((BigDecimal) rhs.getField(DatatypeConstants.SECONDS), rhs.getSign()));

    // align sign
    alignSigns(buf, 0, 2); // Y,M
    alignSigns(buf, 2, 6); // D,h,m,s

    // make sure that the sign bit is consistent across all 6 fields.
    int s = 0;
    for (int i = 0; i < 6; i++) {
        if (s * buf[i].signum() < 0) {
            throw new IllegalStateException();
        }
        if (s == 0) {
            s = buf[i].signum();
        }
    }

    return new DurationImpl(
        s >= 0,
        toBigInteger(sanitize(buf[0], s),
            lhs.getField(DatatypeConstants.YEARS) == null && rhs.getField(DatatypeConstants.YEARS) == null),
        toBigInteger(sanitize(buf[1], s),
            lhs.getField(DatatypeConstants.MONTHS) == null && rhs.getField(DatatypeConstants.MONTHS) == null),
        toBigInteger(sanitize(buf[2], s),
            lhs.getField(DatatypeConstants.DAYS) == null && rhs.getField(DatatypeConstants.DAYS) == null),
        toBigInteger(sanitize(buf[3], s),
            lhs.getField(DatatypeConstants.HOURS) == null && rhs.getField(DatatypeConstants.HOURS) == null),
        toBigInteger(sanitize(buf[4], s),
            lhs.getField(DatatypeConstants.MINUTES) == null && rhs.getField(DatatypeConstants.MINUTES) == null),
         (buf[5].signum() == 0
         && lhs.getField(DatatypeConstants.SECONDS) == null
         && rhs.getField(DatatypeConstants.SECONDS) == null) ? null : sanitize(buf[5], s));
}
 
Example 10
Source File: DurationImpl.java    From hottub with GNU General Public License v2.0 2 votes vote down vote up
/**
 * <p>Computes a new duration whose value is <code>this+rhs</code>.</p>
 *
 * <p>For example,</p>
 * <pre>
 * "1 day" + "-3 days" = "-2 days"
 * "1 year" + "1 day" = "1 year and 1 day"
 * "-(1 hour,50 minutes)" + "-20 minutes" = "-(1 hours,70 minutes)"
 * "15 hours" + "-3 days" = "-(2 days,9 hours)"
 * "1 year" + "-1 day" = IllegalStateException
 * </pre>
 *
 * <p>Since there's no way to meaningfully subtract 1 day from 1 month,
 * there are cases where the operation fails in
 * {@link IllegalStateException}.</p>
 *
 * <p>
 * Formally, the computation is defined as follows.</p>
 * <p>
 * Firstly, we can assume that two {@link Duration}s to be added
 * are both positive without losing generality (i.e.,
 * <code>(-X)+Y=Y-X</code>, <code>X+(-Y)=X-Y</code>,
 * <code>(-X)+(-Y)=-(X+Y)</code>)
 *
 * <p>
 * Addition of two positive {@link Duration}s are simply defined as
 * field by field addition where missing fields are treated as 0.
 * <p>
 * A field of the resulting {@link Duration} will be unset if and
 * only if respective fields of two input {@link Duration}s are unset.
 * <p>
 * Note that <code>lhs.add(rhs)</code> will be always successful if
 * <code>lhs.signum()*rhs.signum()!=-1</code> or both of them are
 * normalized.</p>
 *
 * @param rhs <code>Duration</code> to add to this <code>Duration</code>
 *
 * @return
 *      non-null valid Duration object.
 *
 * @throws NullPointerException
 *      If the rhs parameter is null.
 * @throws IllegalStateException
 *      If two durations cannot be meaningfully added. For
 *      example, adding negative one day to one month causes
 *      this exception.
 *
 *
 * @see #subtract(Duration)
 */
public Duration add(final Duration rhs) {
    Duration lhs = this;
    BigDecimal[] buf = new BigDecimal[6];

    buf[0] = sanitize((BigInteger) lhs.getField(DatatypeConstants.YEARS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.YEARS),  rhs.getSign()));
    buf[1] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MONTHS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MONTHS), rhs.getSign()));
    buf[2] = sanitize((BigInteger) lhs.getField(DatatypeConstants.DAYS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.DAYS),   rhs.getSign()));
    buf[3] = sanitize((BigInteger) lhs.getField(DatatypeConstants.HOURS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.HOURS),  rhs.getSign()));
    buf[4] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MINUTES),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MINUTES), rhs.getSign()));
    buf[5] = sanitize((BigDecimal) lhs.getField(DatatypeConstants.SECONDS),
            lhs.getSign()).add(sanitize((BigDecimal) rhs.getField(DatatypeConstants.SECONDS), rhs.getSign()));

    // align sign
    alignSigns(buf, 0, 2); // Y,M
    alignSigns(buf, 2, 6); // D,h,m,s

    // make sure that the sign bit is consistent across all 6 fields.
    int s = 0;
    for (int i = 0; i < 6; i++) {
        if (s * buf[i].signum() < 0) {
            throw new IllegalStateException();
        }
        if (s == 0) {
            s = buf[i].signum();
        }
    }

    return new DurationImpl(
        s >= 0,
        toBigInteger(sanitize(buf[0], s),
            lhs.getField(DatatypeConstants.YEARS) == null && rhs.getField(DatatypeConstants.YEARS) == null),
        toBigInteger(sanitize(buf[1], s),
            lhs.getField(DatatypeConstants.MONTHS) == null && rhs.getField(DatatypeConstants.MONTHS) == null),
        toBigInteger(sanitize(buf[2], s),
            lhs.getField(DatatypeConstants.DAYS) == null && rhs.getField(DatatypeConstants.DAYS) == null),
        toBigInteger(sanitize(buf[3], s),
            lhs.getField(DatatypeConstants.HOURS) == null && rhs.getField(DatatypeConstants.HOURS) == null),
        toBigInteger(sanitize(buf[4], s),
            lhs.getField(DatatypeConstants.MINUTES) == null && rhs.getField(DatatypeConstants.MINUTES) == null),
         (buf[5].signum() == 0
         && lhs.getField(DatatypeConstants.SECONDS) == null
         && rhs.getField(DatatypeConstants.SECONDS) == null) ? null : sanitize(buf[5], s));
}
 
Example 11
Source File: DurationImpl.java    From openjdk-8-source with GNU General Public License v2.0 2 votes vote down vote up
/**
 * <p>Computes a new duration whose value is <code>this+rhs</code>.</p>
 *
 * <p>For example,</p>
 * <pre>
 * "1 day" + "-3 days" = "-2 days"
 * "1 year" + "1 day" = "1 year and 1 day"
 * "-(1 hour,50 minutes)" + "-20 minutes" = "-(1 hours,70 minutes)"
 * "15 hours" + "-3 days" = "-(2 days,9 hours)"
 * "1 year" + "-1 day" = IllegalStateException
 * </pre>
 *
 * <p>Since there's no way to meaningfully subtract 1 day from 1 month,
 * there are cases where the operation fails in
 * {@link IllegalStateException}.</p>
 *
 * <p>
 * Formally, the computation is defined as follows.</p>
 * <p>
 * Firstly, we can assume that two {@link Duration}s to be added
 * are both positive without losing generality (i.e.,
 * <code>(-X)+Y=Y-X</code>, <code>X+(-Y)=X-Y</code>,
 * <code>(-X)+(-Y)=-(X+Y)</code>)
 *
 * <p>
 * Addition of two positive {@link Duration}s are simply defined as
 * field by field addition where missing fields are treated as 0.
 * <p>
 * A field of the resulting {@link Duration} will be unset if and
 * only if respective fields of two input {@link Duration}s are unset.
 * <p>
 * Note that <code>lhs.add(rhs)</code> will be always successful if
 * <code>lhs.signum()*rhs.signum()!=-1</code> or both of them are
 * normalized.</p>
 *
 * @param rhs <code>Duration</code> to add to this <code>Duration</code>
 *
 * @return
 *      non-null valid Duration object.
 *
 * @throws NullPointerException
 *      If the rhs parameter is null.
 * @throws IllegalStateException
 *      If two durations cannot be meaningfully added. For
 *      example, adding negative one day to one month causes
 *      this exception.
 *
 *
 * @see #subtract(Duration)
 */
public Duration add(final Duration rhs) {
    Duration lhs = this;
    BigDecimal[] buf = new BigDecimal[6];

    buf[0] = sanitize((BigInteger) lhs.getField(DatatypeConstants.YEARS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.YEARS),  rhs.getSign()));
    buf[1] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MONTHS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MONTHS), rhs.getSign()));
    buf[2] = sanitize((BigInteger) lhs.getField(DatatypeConstants.DAYS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.DAYS),   rhs.getSign()));
    buf[3] = sanitize((BigInteger) lhs.getField(DatatypeConstants.HOURS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.HOURS),  rhs.getSign()));
    buf[4] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MINUTES),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MINUTES), rhs.getSign()));
    buf[5] = sanitize((BigDecimal) lhs.getField(DatatypeConstants.SECONDS),
            lhs.getSign()).add(sanitize((BigDecimal) rhs.getField(DatatypeConstants.SECONDS), rhs.getSign()));

    // align sign
    alignSigns(buf, 0, 2); // Y,M
    alignSigns(buf, 2, 6); // D,h,m,s

    // make sure that the sign bit is consistent across all 6 fields.
    int s = 0;
    for (int i = 0; i < 6; i++) {
        if (s * buf[i].signum() < 0) {
            throw new IllegalStateException();
        }
        if (s == 0) {
            s = buf[i].signum();
        }
    }

    return new DurationImpl(
        s >= 0,
        toBigInteger(sanitize(buf[0], s),
            lhs.getField(DatatypeConstants.YEARS) == null && rhs.getField(DatatypeConstants.YEARS) == null),
        toBigInteger(sanitize(buf[1], s),
            lhs.getField(DatatypeConstants.MONTHS) == null && rhs.getField(DatatypeConstants.MONTHS) == null),
        toBigInteger(sanitize(buf[2], s),
            lhs.getField(DatatypeConstants.DAYS) == null && rhs.getField(DatatypeConstants.DAYS) == null),
        toBigInteger(sanitize(buf[3], s),
            lhs.getField(DatatypeConstants.HOURS) == null && rhs.getField(DatatypeConstants.HOURS) == null),
        toBigInteger(sanitize(buf[4], s),
            lhs.getField(DatatypeConstants.MINUTES) == null && rhs.getField(DatatypeConstants.MINUTES) == null),
         (buf[5].signum() == 0
         && lhs.getField(DatatypeConstants.SECONDS) == null
         && rhs.getField(DatatypeConstants.SECONDS) == null) ? null : sanitize(buf[5], s));
}
 
Example 12
Source File: DurationImpl.java    From openjdk-8 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * <p>Computes a new duration whose value is <code>this+rhs</code>.</p>
 *
 * <p>For example,</p>
 * <pre>
 * "1 day" + "-3 days" = "-2 days"
 * "1 year" + "1 day" = "1 year and 1 day"
 * "-(1 hour,50 minutes)" + "-20 minutes" = "-(1 hours,70 minutes)"
 * "15 hours" + "-3 days" = "-(2 days,9 hours)"
 * "1 year" + "-1 day" = IllegalStateException
 * </pre>
 *
 * <p>Since there's no way to meaningfully subtract 1 day from 1 month,
 * there are cases where the operation fails in
 * {@link IllegalStateException}.</p>
 *
 * <p>
 * Formally, the computation is defined as follows.</p>
 * <p>
 * Firstly, we can assume that two {@link Duration}s to be added
 * are both positive without losing generality (i.e.,
 * <code>(-X)+Y=Y-X</code>, <code>X+(-Y)=X-Y</code>,
 * <code>(-X)+(-Y)=-(X+Y)</code>)
 *
 * <p>
 * Addition of two positive {@link Duration}s are simply defined as
 * field by field addition where missing fields are treated as 0.
 * <p>
 * A field of the resulting {@link Duration} will be unset if and
 * only if respective fields of two input {@link Duration}s are unset.
 * <p>
 * Note that <code>lhs.add(rhs)</code> will be always successful if
 * <code>lhs.signum()*rhs.signum()!=-1</code> or both of them are
 * normalized.</p>
 *
 * @param rhs <code>Duration</code> to add to this <code>Duration</code>
 *
 * @return
 *      non-null valid Duration object.
 *
 * @throws NullPointerException
 *      If the rhs parameter is null.
 * @throws IllegalStateException
 *      If two durations cannot be meaningfully added. For
 *      example, adding negative one day to one month causes
 *      this exception.
 *
 *
 * @see #subtract(Duration)
 */
public Duration add(final Duration rhs) {
    Duration lhs = this;
    BigDecimal[] buf = new BigDecimal[6];

    buf[0] = sanitize((BigInteger) lhs.getField(DatatypeConstants.YEARS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.YEARS),  rhs.getSign()));
    buf[1] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MONTHS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MONTHS), rhs.getSign()));
    buf[2] = sanitize((BigInteger) lhs.getField(DatatypeConstants.DAYS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.DAYS),   rhs.getSign()));
    buf[3] = sanitize((BigInteger) lhs.getField(DatatypeConstants.HOURS),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.HOURS),  rhs.getSign()));
    buf[4] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MINUTES),
            lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MINUTES), rhs.getSign()));
    buf[5] = sanitize((BigDecimal) lhs.getField(DatatypeConstants.SECONDS),
            lhs.getSign()).add(sanitize((BigDecimal) rhs.getField(DatatypeConstants.SECONDS), rhs.getSign()));

    // align sign
    alignSigns(buf, 0, 2); // Y,M
    alignSigns(buf, 2, 6); // D,h,m,s

    // make sure that the sign bit is consistent across all 6 fields.
    int s = 0;
    for (int i = 0; i < 6; i++) {
        if (s * buf[i].signum() < 0) {
            throw new IllegalStateException();
        }
        if (s == 0) {
            s = buf[i].signum();
        }
    }

    return new DurationImpl(
        s >= 0,
        toBigInteger(sanitize(buf[0], s),
            lhs.getField(DatatypeConstants.YEARS) == null && rhs.getField(DatatypeConstants.YEARS) == null),
        toBigInteger(sanitize(buf[1], s),
            lhs.getField(DatatypeConstants.MONTHS) == null && rhs.getField(DatatypeConstants.MONTHS) == null),
        toBigInteger(sanitize(buf[2], s),
            lhs.getField(DatatypeConstants.DAYS) == null && rhs.getField(DatatypeConstants.DAYS) == null),
        toBigInteger(sanitize(buf[3], s),
            lhs.getField(DatatypeConstants.HOURS) == null && rhs.getField(DatatypeConstants.HOURS) == null),
        toBigInteger(sanitize(buf[4], s),
            lhs.getField(DatatypeConstants.MINUTES) == null && rhs.getField(DatatypeConstants.MINUTES) == null),
         (buf[5].signum() == 0
         && lhs.getField(DatatypeConstants.SECONDS) == null
         && rhs.getField(DatatypeConstants.SECONDS) == null) ? null : sanitize(buf[5], s));
}