durationimpl.java

来自「JAVA 所有包」· Java 代码 · 共 1,826 行 · 第 1/5 页

JAVA
1,826
字号
		if (field == DatatypeConstants.HOURS) {			return hours != null;		}		if (field == DatatypeConstants.MINUTES) {			return minutes != null;		}				if (field == DatatypeConstants.SECONDS) {			return seconds != null;		}        String methodName = "javax.xml.datatype.Duration" + "#isSet(DatatypeConstants.Field field)";                throw new IllegalArgumentException(            DatatypeMessageFormatter.formatMessage(null,"UnknownField", new Object[]{methodName, field.toString()})					);		    }        /**     * Gets the value of a field.      *      * Fields of a duration object may contain arbitrary large value.     * Therefore this method is designed to return a {@link Number} object.     *      * In case of YEARS, MONTHS, DAYS, HOURS, and MINUTES, the returned     * number will be a non-negative integer. In case of seconds,     * the returned number may be a non-negative decimal value.     *      * @param field     *      one of the six Field constants (YEARS,MONTHS,DAYS,HOURS,     *      MINUTES, or SECONDS.)     * @return     *      If the specified field is present, this method returns     *      a non-null non-negative {@link Number} object that     *      represents its value. If it is not present, return null.     *      For YEARS, MONTHS, DAYS, HOURS, and MINUTES, this method     *      returns a {@link BigInteger} object. For SECONDS, this     *      method returns a {@link BigDecimal}.      *      * @throws NullPointerException     *      If the field parameter is null.     */    public Number getField(DatatypeConstants.Field field) {		if (field == null) {            String methodName = "javax.xml.datatype.Duration" + "#isSet(DatatypeConstants.Field field) " ;            			throw new NullPointerException(				                DatatypeMessageFormatter.formatMessage(null,"FieldCannotBeNull", new Object[]{methodName})                );		}    			if (field == DatatypeConstants.YEARS) {			return years;		}		if (field == DatatypeConstants.MONTHS) {			return months;		}		if (field == DatatypeConstants.DAYS) {			return days;		}		if (field == DatatypeConstants.HOURS) {			return hours;		}		if (field == DatatypeConstants.MINUTES) {			return minutes;		}				if (field == DatatypeConstants.SECONDS) {			return seconds;		}		/**		throw new IllegalArgumentException(			"javax.xml.datatype.Duration"			+ "#(getSet(DatatypeConstants.Field field) called with an unknown field: "			+ field.toString()		);        */        String methodName = "javax.xml.datatype.Duration" + "#(getSet(DatatypeConstants.Field field)";                throw new IllegalArgumentException(            DatatypeMessageFormatter.formatMessage(null,"UnknownField", new Object[]{methodName, field.toString()})					);            }        /**     * Obtains the value of the YEARS field as an integer value,     * or 0 if not present.     *      * <p>     * This method is a convenience method around the      * {@link #getField(DatatypeConstants.Field)} method.     *      * <p>     * Note that since this method returns <tt>int</tt>, this     * method will return an incorrect value for {@link Duration}s     * with the year field that goes beyond the range of <tt>int</tt>.     * Use <code>getField(YEARS)</code> to avoid possible loss of precision.</p>     *      * @return     *      If the YEARS field is present, return     *      its value as an integer by using the {@link Number#intValue()}     *      method. If the YEARS field is not present, return 0.     */    public int getYears() {        return getInt(DatatypeConstants.YEARS);    }        /**     * Obtains the value of the MONTHS field as an integer value,     * or 0 if not present.     *      * This method works just like {@link #getYears()} except     * that this method works on the MONTHS field.     *      * @return Months of this <code>Duration</code>.     */    public int getMonths() {        return getInt(DatatypeConstants.MONTHS);    }        /**     * Obtains the value of the DAYS field as an integer value,     * or 0 if not present.     *      * This method works just like {@link #getYears()} except     * that this method works on the DAYS field.     *      * @return Days of this <code>Duration</code>.     */    public int getDays() {        return getInt(DatatypeConstants.DAYS);    }        /**     * Obtains the value of the HOURS field as an integer value,     * or 0 if not present.     *      * This method works just like {@link #getYears()} except     * that this method works on the HOURS field.     *      * @return Hours of this <code>Duration</code>.     *      */    public int getHours() {        return getInt(DatatypeConstants.HOURS);    }        /**     * Obtains the value of the MINUTES field as an integer value,     * or 0 if not present.     *      * This method works just like {@link #getYears()} except     * that this method works on the MINUTES field.     *      * @return Minutes of this <code>Duration</code>.     *      */    public int getMinutes() {        return getInt(DatatypeConstants.MINUTES);    }        /**     * Obtains the value of the SECONDS field as an integer value,     * or 0 if not present.     *      * This method works just like {@link #getYears()} except     * that this method works on the SECONDS field.     *      * @return seconds in the integer value. The fraction of seconds     *   will be discarded (for example, if the actual value is 2.5,     *   this method returns 2)     */    public int getSeconds() {        return getInt(DatatypeConstants.SECONDS);    }        /**     * <p>Return the requested field value as an int.</p>     *      * <p>If field is not set, i.e. == null, 0 is returned.</p>     *      * @param field To get value for.     *      * @return int value of field or 0 if field is not set.     */    private int getInt(DatatypeConstants.Field field) {        Number n = getField(field);        if (n == null) {            return 0;        } else {            return n.intValue();        }    }            /**     * <p>Returns the length of the duration in milli-seconds.</p>     *      * <p>If the seconds field carries more digits than milli-second order,     * those will be simply discarded (or in other words, rounded to zero.)       * For example, for any Calendar value <code>x<code>,</p>     * <pre>     * <code>new Duration("PT10.00099S").getTimeInMills(x) == 10000</code>.     * <code>new Duration("-PT10.00099S").getTimeInMills(x) == -10000</code>.     * </pre>     *      * <p>     * Note that this method uses the {@link #addTo(Calendar)} method,     * which may work incorectly with {@link Duration} objects with     * very large values in its fields. See the {@link #addTo(Calendar)}     * method for details.     *      * @param startInstant     *      The length of a month/year varies. The <code>startInstant</code> is     *      used to disambiguate this variance. Specifically, this method     *      returns the difference between <code>startInstant</code> and     *      <code>startInstant+duration</code>     *      * @return milliseconds between <code>startInstant</code> and     *   <code>startInstant</code> plus this <code>Duration</code>     *     * @throws NullPointerException if <code>startInstant</code> parameter      * is null.     *      */    public long getTimeInMillis(final Calendar startInstant) {        Calendar cal = (Calendar) startInstant.clone();        addTo(cal);        return getCalendarTimeInMillis(cal)                    - getCalendarTimeInMillis(startInstant);    }        /**     * <p>Returns the length of the duration in milli-seconds.</p>     *      * <p>If the seconds field carries more digits than milli-second order,     * those will be simply discarded (or in other words, rounded to zero.)     * For example, for any <code>Date</code> value <code>x<code>,</p>        * <pre>     * <code>new Duration("PT10.00099S").getTimeInMills(x) == 10000</code>.     * <code>new Duration("-PT10.00099S").getTimeInMills(x) == -10000</code>.     * </pre>     *      * <p>     * Note that this method uses the {@link #addTo(Date)} method,     * which may work incorectly with {@link Duration} objects with     * very large values in its fields. See the {@link #addTo(Date)}     * method for details.     *      * @param startInstant     *      The length of a month/year varies. The <code>startInstant</code> is     *      used to disambiguate this variance. Specifically, this method     *      returns the difference between <code>startInstant</code> and     *      <code>startInstant+duration</code>.     *      * @throws NullPointerException     *      If the startInstant parameter is null.     *      * @return milliseconds between <code>startInstant</code> and     *   <code>startInstant</code> plus this <code>Duration</code>     *     * @see #getTimeInMillis(Calendar)     */    public long getTimeInMillis(final Date startInstant) {        Calendar cal = new GregorianCalendar();        cal.setTime(startInstant);        this.addTo(cal);        return getCalendarTimeInMillis(cal) - startInstant.getTime();    }    //    /**//     * Returns an equivalent but "normalized" duration value.//     * //     * Intuitively, the normalization moves YEARS into//     * MONTHS (by x12) and moves DAYS, HOURS, and MINUTES fields//     * into SECONDS (by x86400, x3600, and x60 respectively.)//     * //     * //     * Formally, this method satisfies the following conditions://     * <ul>//     *  <li>x.normalize().equals(x)//     *  <li>!x.normalize().isSet(Duration.YEARS)//     *  <li>!x.normalize().isSet(Duration.DAYS)//     *  <li>!x.normalize().isSet(Duration.HOURS)//     *  <li>!x.normalize().isSet(Duration.MINUTES)//     * </ul>//     * //     * @return//     *      always return a non-null valid value. //     *///    public Duration normalize() {//        return null;//    }        /**     * <p>Converts the years and months fields into the days field     * by using a specific time instant as the reference point.</p>     *      * <p>For example, duration of one month normalizes to 31 days     * given the start time instance "July 8th 2003, 17:40:32".</p>     *      * <p>Formally, the computation is done as follows:</p>     * <ol>     *  <li>The given Calendar object is cloned.     *  <li>The years, months and days fields will be added to     *      the {@link Calendar} object     *      by using the {@link Calendar#add(int,int)} method.      *  <li>The difference between two Calendars are computed in terms of days.     *  <li>The computed days, along with the hours, minutes and seconds     *      fields of this duration object is used to construct a new     *      Duration object.     * </ol>     *      * <p>Note that since the Calendar class uses <code>int</code> to     * hold the value of year and month, this method may produce     * an unexpected result if this duration object holds     * a very large value in the years or months fields.</p>     *     * @param startTimeInstant <code>Calendar</code> reference point.     *       * @return <code>Duration</code> of years and months of this <code>Duration</code> as days.     *      * @throws NullPointerException If the startTimeInstant parameter is null.     */    public Duration normalizeWith(Calendar startTimeInstant) {                Calendar c = (Calendar) startTimeInstant.clone();                // using int may cause overflow, but         // Calendar internally treats value as int anyways.	c.add(Calendar.YEAR, getYears() * signum);        c.add(Calendar.MONTH, getMonths() * signum);        c.add(Calendar.DAY_OF_MONTH, getDays() * signum);                // obtain the difference in terms of days        long diff = getCalendarTimeInMillis(c) - getCalendarTimeInMillis(startTimeInstant);        int days = (int) (diff / (1000L * 60L * 60L * 24L));                return new DurationImpl(            days >= 0,            null,            null,            wrap(Math.abs(days)),            (BigInteger) getField(DatatypeConstants.HOURS),            (BigInteger) getField(DatatypeConstants.MINUTES),            (BigDecimal) getField(DatatypeConstants.SECONDS));    }        /**     * <p>Computes a new duration whose value is <code>factor</code> times     * longer than the value of this duration.</p>     *      * <p>This method is provided for the convenience.     * It is functionally equivalent to the following code:</p>     * <pre>     * multiply(new BigDecimal(String.valueOf(factor)))     * </pre>     *      * @param factor Factor times longer of new <code>Duration</code> to create.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?