Package org.fenixedu.academic.util

Source Code of org.fenixedu.academic.util.HourMinuteSecond$Property

/**
* Copyright © 2002 Instituto Superior Técnico
*
* This file is part of FenixEdu Academic.
*
* FenixEdu Academic is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FenixEdu Academic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FenixEdu Academic.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
*
*/
package org.fenixedu.academic.util;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import org.joda.time.Chronology;
import org.joda.time.DateMidnight;
import org.joda.time.DateTime;
import org.joda.time.DateTimeField;
import org.joda.time.DateTimeFieldType;
import org.joda.time.DateTimeUtils;
import org.joda.time.DateTimeZone;
import org.joda.time.DurationFieldType;
import org.joda.time.Interval;
import org.joda.time.LocalTime;
import org.joda.time.ReadablePartial;
import org.joda.time.ReadablePeriod;
import org.joda.time.TimeOfDay;
import org.joda.time.base.BasePartial;
import org.joda.time.field.AbstractPartialFieldProperty;
import org.joda.time.field.FieldUtils;
import org.joda.time.format.ISODateTimeFormat;

/**
* @author - Shezad Anavarali (shezad@ist.utl.pt)
*
*/
public final class HourMinuteSecond extends BasePartial implements ReadablePartial, Serializable {

    /** The singleton set of field types */
    private static final DateTimeFieldType[] FIELD_TYPES = new DateTimeFieldType[] { DateTimeFieldType.hourOfDay(),
            DateTimeFieldType.minuteOfHour(), DateTimeFieldType.secondOfMinute(), };

    /** The index of the hour field in the field array */
    public static final int HOUR_OF_DAY = 0;

    /** The index of the minute field in the field array */
    public static final int MINUTE_OF_HOUR = 1;

    /** The index of the second field in the field array */
    public static final int SECOND_OF_MINUTE = 2;

    // -----------------------------------------------------------------------
    /**
     * Constructs a HourMinuteSecond from a <code>java.util.Calendar</code> using exactly the same field values avoiding any time
     * zone effects.
     * <p>
     * Each field is queried from the Calendar and assigned to the HourMinuteSecond. This is useful if you have been using the
     * Calendar as a local date, ignoing the zone.
     * <p>
     * This factory method ignores the type of the calendar and always creates a HourMinuteSecond with ISO chronology. It is
     * expected that you will only pass in instances of <code>GregorianCalendar</code> however this is not validated.
     *
     * @param calendar
     *            the Calendar to extract fields from
     * @return the created HourMinuteSecond
     * @throws IllegalArgumentException
     *             if the calendar is null
     * @throws IllegalArgumentException
     *             if the date is invalid for the ISO chronology
     */
    public static HourMinuteSecond fromCalendarFields(Calendar calendar) {
        if (calendar == null) {
            throw new IllegalArgumentException("The calendar must not be null");
        }
        return new HourMinuteSecond(calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE),
                calendar.get(Calendar.SECOND));
    }

    /**
     * Constructs a HourMinuteSecond from a <code>java.util.Date</code> using
     * exactly the same field values avoiding any time zone effects.
     * <p>
     * Each field is queried from the Date and assigned to the HourMinuteSecond. This is useful if you have been using the Date as
     * a local date, ignoing the zone.
     * <p>
     * This factory method always creates a HourMinuteSecond with ISO chronology.
     *
     * @param date
     *            the Date to extract fields from
     * @return the created HourMinuteSecond
     * @throws IllegalArgumentException
     *             if the calendar is null
     * @throws IllegalArgumentException
     *             if the date is invalid for the ISO chronology
     */
    public static HourMinuteSecond fromDateFields(Date date) {
        if (date == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        return new HourMinuteSecond(date.getHours(), date.getMinutes(), date.getSeconds());
    }

    // -----------------------------------------------------------------------
    /**
     * Constructs a HourMinuteSecond with the current date, using ISOChronology
     * in the default zone to extract the fields.
     * <p>
     * The constructor uses the default time zone, resulting in the local time being initialised. Once the constructor is
     * complete, all further calculations are performed without reference to a timezone (by switching to UTC).
     */
    public HourMinuteSecond() {
        super();
    }

    /**
     * Constructs a HourMinuteSecond with the current date, using the specified
     * chronology and zone to extract the fields.
     * <p>
     * The constructor uses the time zone of the chronology specified. Once the constructor is complete, all further calculations
     * are performed without reference to a timezone (by switching to UTC).
     *
     * @param chronology
     *            the chronology, null means ISOChronology in the default zone
     */
    public HourMinuteSecond(Chronology chronology) {
        super(chronology);
    }

    /**
     * Constructs a HourMinuteSecond extracting the partial fields from the
     * specified milliseconds using the ISOChronology in the default zone.
     * <p>
     * The constructor uses the default time zone, resulting in the local time being initialised. Once the constructor is
     * complete, all further calculations are performed without reference to a timezone (by switching to UTC).
     *
     * @param instant
     *            the milliseconds from 1970-01-01T00:00:00Z
     */
    public HourMinuteSecond(long instant) {
        super(instant);
    }

    /**
     * Constructs a HourMinuteSecond extracting the partial fields from the
     * specified milliseconds using the chronology provided.
     * <p>
     * The constructor uses the time zone of the chronology specified. Once the constructor is complete, all further calculations
     * are performed without reference to a timezone (by switching to UTC).
     *
     * @param instant
     *            the milliseconds from 1970-01-01T00:00:00Z
     * @param chronology
     *            the chronology, null means ISOChronology in the default zone
     */
    public HourMinuteSecond(long instant, Chronology chronology) {
        super(instant, chronology);
    }

    /**
     * Constructs a HourMinuteSecond from an Object that represents a time,
     * using the specified chronology.
     * <p>
     * The recognised object types are defined in {@link org.joda.time.convert.ConverterManager ConverterManager} and include
     * ReadableInstant, String, Calendar and Date.
     * <p>
     * The constructor uses the time zone of the chronology specified. Once the constructor is complete, all further calculations
     * are performed without reference to a timezone (by switching to UTC). The specified chronology overrides that of the object.
     *
     * @param instant
     *            the datetime object, null means now
     * @param chronology
     *            the chronology, null means ISO default
     * @throws IllegalArgumentException
     *             if the instant is invalid
     */
    public HourMinuteSecond(Object instant, Chronology chronology) {
        super(instant, DateTimeUtils.getChronology(chronology));
    }

    /**
     * Constructs a HourMinuteSecond from an Object that represents a time.
     * <p>
     * The recognised object types are defined in {@link org.joda.time.convert.ConverterManager ConverterManager} and include
     * ReadableInstant, String, Calendar and Date.
     * <p>
     * The chronology used will be derived from the object, defaulting to ISO.
     *
     * @param instant
     *            the datetime object, null means now
     * @throws IllegalArgumentException
     *             if the instant is invalid
     */
    public HourMinuteSecond(Object instant) {
        super(instant, null);
    }

    /**
     * Constructs a HourMinuteSecond with specified time field values using <code>ISOChronology</code> in the default zone.
     * <p>
     * The constructor uses the no time zone initialising the fields as provided. Once the constructor is complete, all further
     * calculations are performed without reference to a timezone (by switching to UTC).
     *
     * @param hour
     *            the hour
     * @param minute
     *            the minute of the hour
     * @param second
     *            the second of the minute
     */
    public HourMinuteSecond(int hour, int minute, int second) {
        this(hour, minute, second, null);
    }

    /**
     * Constructs a HourMinuteSecond with specified time field values.
     * <p>
     * The constructor uses the time zone of the chronology specified. Once the constructor is complete, all further calculations
     * are performed without reference to a timezone (by switching to UTC).
     *
     * @param hour
     *            the hour
     * @param minute
     *            the minute of the hour
     * @param second
     *            the second of the minute
     * @param chronology
     *            the chronology, null means ISOChronology in the default zone
     */
    public HourMinuteSecond(int hour, int minute, int second, Chronology chronology) {
        super(new int[] { hour, minute, second }, chronology);
    }

    /**
     * Constructs a HourMinuteSecond with specified time field values.
     * <p>
     * The constructor uses the time zone of the chronology specified. Once the constructor is complete, all further calculations
     * are performed without reference to a timezone (by switching to UTC).
     *
     * @param values
     *            the new set of values
     * @param chronology
     *            the chronology, null means ISOChronology in the default zone
     */
    public HourMinuteSecond(int[] values, Chronology chronology) {
        super(values, chronology);
    }

    /**
     * Constructs a HourMinuteSecond with chronology from this instance and new
     * values.
     *
     * @param partial
     *            the partial to base this new instance on
     * @param values
     *            the new set of values
     */
    public HourMinuteSecond(BasePartial partial, int[] values) {
        super(partial, values);
    }

    /**
     * Constructs a HourMinuteSecond with values from this instance and a new
     * chronology.
     *
     * @param partial
     *            the partial to base this new instance on
     * @param chrono
     *            the new chronology
     */
    public HourMinuteSecond(BasePartial partial, Chronology chrono) {
        super(partial, chrono);
    }

    /**
     * Gets the number of fields in this partial.
     *
     * @return the field count
     */
    @Override
    public int size() {
        return 3;
    }

    /**
     * Gets the field for a specific index in the chronology specified.
     * <p>
     * This method must not use any instance variables.
     *
     * @param index
     *            the index to retrieve
     * @param chrono
     *            the chronology to use
     * @return the field
     */
    @Override
    protected DateTimeField getField(int index, Chronology chrono) {
        switch (index) {
        case HOUR_OF_DAY:
            return chrono.hourOfDay();
        case MINUTE_OF_HOUR:
            return chrono.minuteOfHour();
        case SECOND_OF_MINUTE:
            return chrono.secondOfMinute();
        default:
            throw new IndexOutOfBoundsException("Invalid index: " + index);
        }
    }

    /**
     * Gets the field type at the specified index.
     *
     * @param index
     *            the index to retrieve
     * @return the field at the specified index
     * @throws IndexOutOfBoundsException
     *             if the index is invalid
     */
    @Override
    public DateTimeFieldType getFieldType(int index) {
        return FIELD_TYPES[index];
    }

    /**
     * Gets an array of the field type of each of the fields that this partial
     * supports.
     * <p>
     * The fields are returned largest to smallest, Hour, Minute, Second
     *
     * @return the array of field types (cloned), largest to smallest
     */
    @Override
    public DateTimeFieldType[] getFieldTypes() {
        return FIELD_TYPES.clone();
    }

    // -----------------------------------------------------------------------
    /**
     * Creates a new HourMinuteSecond instance with the specified chronology.
     * This instance is immutable and unaffected by this method call.
     * <p>
     * This method retains the values of the fields, thus the result will typically refer to a different instant.
     * <p>
     * The time zone of the specified chronology is ignored, as HourMinuteSecond operates without a time zone.
     *
     * @param newChronology
     *            the new chronology, null means ISO
     * @return a copy of this datetime with a different chronology
     * @throws IllegalArgumentException
     *             if the values are invalid for the new chronology
     */
    public HourMinuteSecond withChronologyRetainFields(Chronology newChronology) {
        newChronology = DateTimeUtils.getChronology(newChronology);
        newChronology = newChronology.withUTC();
        if (newChronology == getChronology()) {
            return this;
        } else {
            HourMinuteSecond newHourMinuteSecond = new HourMinuteSecond(this, newChronology);
            newChronology.validate(newHourMinuteSecond, getValues());
            return newHourMinuteSecond;
        }
    }

    /**
     * Gets a copy of this date with the specified field set to a new value.
     * <p>
     * For example, if the field type is <code>minuteOfHour</code> then the minute would be changed in the returned instance.
     * <p>
     * These three lines are equivalent:
     *
     * <pre>
     * HourMinuteSecond updated = hms.withField(DateTimeFieldType.minuteOfHour(), 6);
     *
     * HourMinuteSecond updated = hms.minuteOfHour().setCopy(6);
     *
     * HourMinuteSecond updated = hms.property(DateTimeFieldType.minuteOfHour()).setCopy(6);
     * </pre>
     *
     * @param fieldType
     *            the field type to set, not null
     * @param value
     *            the value to set
     * @return a copy of this instance with the field set
     * @throws IllegalArgumentException
     *             if the value is null or invalid
     */
    public HourMinuteSecond withField(DateTimeFieldType fieldType, int value) {
        int index = indexOfSupported(fieldType);
        if (value == getValue(index)) {
            return this;
        }
        int[] newValues = getValues();
        newValues = getField(index).set(this, index, newValues, value);
        return new HourMinuteSecond(this, newValues);
    }

    /**
     * Gets a copy of this date with the value of the specified field increased.
     * <p>
     * If the addition is zero, then <code>this</code> is returned.
     * <p>
     * These three lines are equivalent:
     *
     * <pre>
     * HourMinuteSecond added = hms.withFieldAdded(DurationFieldType.hours(), 6);
     *
     * HourMinuteSecond added = hms.plusHours(6);
     *
     * HourMinuteSecond added = hms.hour().addToCopy(6);
     * </pre>
     *
     * @param fieldType
     *            the field type to add to, not null
     * @param amount
     *            the amount to add
     * @return a copy of this instance with the field updated
     * @throws IllegalArgumentException
     *             if the value is null or invalid
     * @throws ArithmeticException
     *             if the new datetime exceeds the capacity
     */
    public HourMinuteSecond withFieldAdded(DurationFieldType fieldType, int amount) {
        int index = indexOfSupported(fieldType);
        if (amount == 0) {
            return this;
        }
        int[] newValues = getValues();
        newValues = getField(index).add(this, index, newValues, amount);
        return new HourMinuteSecond(this, newValues);
    }

    /**
     * Gets a copy of this date with the specified period added.
     * <p>
     * If the addition is zero, then <code>this</code> is returned. Fields in the period that aren't present in the partial are
     * ignored.
     * <p>
     * This method is typically used to add multiple copies of complex period instances. Adding one field is best achieved using
     * methods like {@link #withFieldAdded(DurationFieldType, int)} or {@link #plusHours(int)}.
     *
     * @param period
     *            the period to add to this one, null means zero
     * @param scalar
     *            the amount of times to add, such as -1 to subtract once
     * @return a copy of this instance with the period added
     * @throws ArithmeticException
     *             if the new datetime exceeds the capacity
     */
    public HourMinuteSecond withPeriodAdded(ReadablePeriod period, int scalar) {
        if (period == null || scalar == 0) {
            return this;
        }
        int[] newValues = getValues();
        for (int i = 0; i < period.size(); i++) {
            DurationFieldType fieldType = period.getFieldType(i);
            int index = indexOf(fieldType);
            if (index >= 0) {
                newValues = getField(index).add(this, index, newValues, FieldUtils.safeMultiply(period.getValue(i), scalar));
            }
        }
        return new HourMinuteSecond(this, newValues);
    }

    // -----------------------------------------------------------------------
    /**
     * Gets a copy of this instance with the specified period added.
     * <p>
     * If the amount is zero or null, then <code>this</code> is returned.
     * <p>
     * This method is typically used to add complex period instances. Adding one field is best achieved using methods like
     * {@link #plusHours(int)}.
     *
     * @param period
     *            the duration to add to this one, null means zero
     * @return a copy of this instance with the period added
     * @throws ArithmeticException
     *             if the new datetime exceeds the capacity of a long
     */
    public HourMinuteSecond plus(ReadablePeriod period) {
        return withPeriodAdded(period, 1);
    }

    // -----------------------------------------------------------------------
    /**
     * Returns a new date plus the specified number of hours.
     * <p>
     * This date instance is immutable and unaffected by this method call.
     * <p>
     * The following three lines are identical in effect:
     *
     * <pre>
     * HourMinuteSecond added = hms.plusHours(6);
     *
     * HourMinuteSecond added = hms.plus(Period.hours(6));
     *
     * HourMinuteSecond added = hms.withFieldAdded(DurationFieldType.hours(), 6);
     * </pre>
     *
     * @param hours
     *            the amount of hours to add, may be negative
     * @return the new date plus the increased hours
     */
    public HourMinuteSecond plusHours(int hours) {
        return withFieldAdded(DurationFieldType.hours(), hours);
    }

    /**
     * Returns a new date plus the specified number of minutes.
     * <p>
     * This date instance is immutable and unaffected by this method call.
     * <p>
     * The following three lines are identical in effect:
     *
     * <pre>
     * HourMinuteSecond added = hms.plusMinutes(6);
     *
     * HourMinuteSecond added = hms.plus(Period.minutes(6));
     *
     * HourMinuteSecond added = hms.withFieldAdded(DurationFieldType.minutes(), 6);
     * </pre>
     *
     * @param minutes
     *            the amount of minutes to add, may be negative
     * @return the new date plus the increased minutes
     */
    public HourMinuteSecond plusMinutes(int minutes) {
        return withFieldAdded(DurationFieldType.minutes(), minutes);
    }

    /**
     * Returns a new date plus the specified number of seconds.
     * <p>
     * This date instance is immutable and unaffected by this method call.
     * <p>
     * The following three lines are identical in effect:
     *
     * <pre>
     * HourMinuteSecond added = hms.plusSeconds(6);
     *
     * HourMinuteSecond added = hms.plus(Period.seconds(6));
     *
     * HourMinuteSecond added = hms.withFieldAdded(DurationFieldType.seconds(), 6);
     * </pre>
     *
     * @param seconds
     *            the amount of seconds to add, may be negative
     * @return the new date plus the increased seconds
     */
    public HourMinuteSecond plusSeconds(int seconds) {
        return withFieldAdded(DurationFieldType.seconds(), seconds);
    }

    // -----------------------------------------------------------------------
    /**
     * Gets a copy of this instance with the specified period take away.
     * <p>
     * If the amount is zero or null, then <code>this</code> is returned.
     * <p>
     * This method is typically used to subtract complex period instances. Subtracting one field is best achieved using methods
     * like {@link #minusHours(int)}.
     *
     * @param period
     *            the period to reduce this instant by
     * @return a copy of this instance with the period taken away
     * @throws ArithmeticException
     *             if the new datetime exceeds the capacity of a long
     */
    public HourMinuteSecond minus(ReadablePeriod period) {
        return withPeriodAdded(period, -1);
    }

    // -----------------------------------------------------------------------
    /**
     * Returns a new datetime minus the specified number of hours.
     * <p>
     * This datetime instance is immutable and unaffected by this method call.
     * <p>
     * The following three lines are identical in effect:
     *
     * <pre>
     * HourMinuteSecond subtracted = hms.minusHours(6);
     *
     * HourMinuteSecond subtracted = hms.minus(Period.hours(6));
     *
     * HourMinuteSecond subtracted = hms.withFieldAdded(DurationFieldType.hours(), -6);
     * </pre>
     *
     * @param hours
     *            the amount of hours to subtract, may be negative
     * @return the new datetime minus the increased hours
     */
    public HourMinuteSecond minusHours(int hours) {
        return withFieldAdded(DurationFieldType.hours(), FieldUtils.safeNegate(hours));
    }

    /**
     * Returns a new datetime minus the specified number of minutes.
     * <p>
     * This datetime instance is immutable and unaffected by this method call.
     * <p>
     * The following three lines are identical in effect:
     *
     * <pre>
     * HourMinuteSecond subtracted = hms.minusMinutes(6);
     *
     * HourMinuteSecond subtracted = hms.minus(Period.minutes(6));
     *
     * HourMinuteSecond subtracted = hms.withFieldAdded(DurationFieldType.minutes(), -6);
     * </pre>
     *
     * @param minutes
     *            the amount of minutes to subtract, may be negative
     * @return the new datetime minus the increased minutes
     */
    public HourMinuteSecond minusMinutes(int minutes) {
        return withFieldAdded(DurationFieldType.minutes(), FieldUtils.safeNegate(minutes));
    }

    /**
     * Returns a new datetime minus the specified number of seconds.
     * <p>
     * This datetime instance is immutable and unaffected by this method call.
     * <p>
     * The following three lines are identical in effect:
     *
     * <pre>
     * HourMinuteSecond subtracted = hms.minusSeconds(6);
     *
     * HourMinuteSecond subtracted = hms.minus(Period.seconds(6));
     *
     * HourMinuteSecond subtracted = hms.withFieldAdded(DurationFieldType.seconds(), -6);
     * </pre>
     *
     * @param seconds
     *            the amount of seconds to subtract, may be negative
     * @return the new datetime minus the increased seconds
     */
    public HourMinuteSecond minusSeconds(int seconds) {
        return withFieldAdded(DurationFieldType.seconds(), FieldUtils.safeNegate(seconds));
    }

    // -----------------------------------------------------------------------
    /**
     * Gets the property object for the specified type, which contains many
     * useful methods.
     *
     * @param type
     *            the field type to get the property for
     * @return the property object
     * @throws IllegalArgumentException
     *             if the field is null or unsupported
     */
    public Property property(DateTimeFieldType type) {
        return new Property(this, indexOfSupported(type));
    }

    // -----------------------------------------------------------------------
    /**
     * Converts this HourMinuteSecond to a full datetime at midnight using the
     * default time zone.
     *
     * @return this date as a datetime at midnight
     */
    public DateTime toDateTimeAtMidnight() {
        return toDateTimeAtMidnight(null);
    }

    /**
     * Converts this HourMinuteSecond to a full datetime at midnight using the
     * specified time zone.
     * <p>
     * This method uses the chronology from this instance plus the time zone specified.
     *
     * @param zone
     *            the zone to use, null means default
     * @return this date as a datetime at midnight
     */
    public DateTime toDateTimeAtMidnight(DateTimeZone zone) {
        Chronology chrono = getChronology().withZone(zone);
        return new DateTime(0, 0, 0, 0, 0, 0, 0, chrono);
    }

    // -----------------------------------------------------------------------
    /**
     * Converts this partial to a full datetime using the default time zone
     * setting the date fields from this instance and the time fields from the
     * current time.
     *
     * @return this date as a datetime with the time as the current time
     */
    public DateTime toDateTimeAtCurrentTime() {
        return toDateTimeAtCurrentTime(null);
    }

    /**
     * Converts this partial to a full datetime using the specified time zone
     * setting the date fields from this instance and the time fields from the
     * current time.
     * <p>
     * This method uses the chronology from this instance plus the time zone specified.
     *
     * @param zone
     *            the zone to use, null means default
     * @return this date as a datetime with the time as the current time
     */
    public DateTime toDateTimeAtCurrentTime(DateTimeZone zone) {
        Chronology chrono = getChronology().withZone(zone);
        long instantMillis = DateTimeUtils.currentTimeMillis();
        long resolved = chrono.set(this, instantMillis);
        return new DateTime(resolved, chrono);
    }

    // -----------------------------------------------------------------------
    /**
     * Converts this object to a DateMidnight in the default time zone.
     *
     * @return the DateMidnight instance in the default zone
     */
    public DateMidnight toDateMidnight() {
        return toDateMidnight(null);
    }

    /**
     * Converts this object to a DateMidnight.
     *
     * @param zone
     *            the zone to get the DateMidnight in, null means default
     * @return the DateMidnight instance
     */
    public DateMidnight toDateMidnight(DateTimeZone zone) {
        Chronology chrono = getChronology().withZone(zone);
        return new DateMidnight(0, 0, 0, chrono);
    }

    // -----------------------------------------------------------------------
    /**
     * Converts this object to a DateTime using a TimeOfDay to fill in the
     * missing fields and using the default time zone. This instance is
     * immutable and unaffected by this method call.
     * <p>
     * The resulting chronology is determined by the chronology of this HourMinuteSecond plus the time zone. The chronology of the
     * time is ignored - only the field values are used.
     *
     * @param time
     *            the time of day to use, null means current time
     * @return the DateTime instance
     */
    public DateTime toDateTime(TimeOfDay time) {
        return toDateTime(time, null);
    }

    /**
     * Converts this object to a DateTime using a TimeOfDay to fill in the
     * missing fields. This instance is immutable and unaffected by this method
     * call.
     * <p>
     * The resulting chronology is determined by the chronology of this HourMinuteSecond plus the time zone. The chronology of the
     * time is ignored - only the field values are used.
     *
     * @param time
     *            the time of day to use, null means current time
     * @param zone
     *            the zone to get the DateTime in, null means default
     * @return the DateTime instance
     */
    public DateTime toDateTime(TimeOfDay time, DateTimeZone zone) {
        Chronology chrono = getChronology().withZone(zone);
        long instant = DateTimeUtils.currentTimeMillis();
        instant = chrono.set(this, instant);
        if (time != null) {
            instant = chrono.set(time, instant);
        }
        return new DateTime(instant, chrono);
    }

    // -----------------------------------------------------------------------
    /**
     * Converts this object to an Interval representing the whole day in the
     * default time zone.
     *
     * @return a interval over the day
     */
    public Interval toInterval() {
        return toInterval(null);
    }

    /**
     * Converts this object to an Interval representing the whole day.
     *
     * @param zone
     *            the zone to get the Interval in, null means default
     * @return a interval over the day
     */
    public Interval toInterval(DateTimeZone zone) {
        zone = DateTimeUtils.getZone(zone);
        return toDateMidnight(zone).toInterval();
    }

    // -----------------------------------------------------------------------
    /**
     * Converts this object to a LocalTime.
     *
     * @return the LocalTime instance
     */
    public LocalTime toLocalTime() {
        return new LocalTime(getHour(), getMinuteOfHour(), getSecondOfMinute());
    }

    /**
     * Constructs a HourMinuteSecond from a LocalTime, using exactly the same
     * fields, but ignoring the millis.
     *
     * @return the created HourMinuteSecond
     */
    public static HourMinuteSecond fromLocalTime(LocalTime time) {
        return new HourMinuteSecond(time.getHourOfDay(), time.getMinuteOfHour(), time.getSecondOfMinute());
    }

    // -----------------------------------------------------------------------
    /**
     * Get the hour field value.
     *
     * @return the hour
     */
    public int getHour() {
        return getValue(HOUR_OF_DAY);
    }

    /**
     * Get the minute of hour field value.
     *
     * @return the minute of hour
     */
    public int getMinuteOfHour() {
        return getValue(MINUTE_OF_HOUR);
    }

    /**
     * Get the second of minute field value.
     *
     * @return the second of minute
     */
    public int getSecondOfMinute() {
        return getValue(SECOND_OF_MINUTE);
    }

    // -----------------------------------------------------------------------
    /**
     * Get the hour field property
     *
     * @return the hour property
     */
    public Property hour() {
        return new Property(this, HOUR_OF_DAY);
    }

    /**
     * Get the minute of hour field property
     *
     * @return the minute of hour property
     */
    public Property minuteOfHour() {
        return new Property(this, MINUTE_OF_HOUR);
    }

    /**
     * Get the second of minute field property
     *
     * @return the second of minute property
     */
    public Property secondOfMinute() {
        return new Property(this, SECOND_OF_MINUTE);
    }

    // -----------------------------------------------------------------------
    /**
     * Output the date in the ISO8601 format HH:mm:ss.
     *
     * @return ISO8601 formatted string
     */
    @Override
    public String toString() {
        return ISODateTimeFormat.hourMinuteSecond().print(this);
    }

    /**
     * Set the second of minute field property
     *
     * @return the new HourMinuteSecond
     */
    public HourMinuteSecond setSecondOfMinute(int seconds) {
        return new HourMinuteSecond(getHour(), getMinuteOfHour(), seconds);
    }

    // -----------------------------------------------------------------------
    /**
     * The property class for <code>HourMinuteSecond</code>.
     * <p>
     * This class binds a <code>HourMinuteSecond</code> to a <code>DateTimeField</code>.
     *
     */
    public static class Property extends AbstractPartialFieldProperty implements Serializable {

        /** Serialization version */
        private static final long serialVersionUID = 5727734012190224363L;

        /** The partial */
        private final HourMinuteSecond iHourMinuteSecond;

        /** The field index */
        private final int iFieldIndex;

        /**
         * Constructs a property.
         *
         * @param partial
         *            the partial instance
         * @param fieldIndex
         *            the index in the partial
         */
        Property(HourMinuteSecond partial, int fieldIndex) {
            super();
            iHourMinuteSecond = partial;
            iFieldIndex = fieldIndex;
        }

        /**
         * Gets the field that this property uses.
         *
         * @return the field
         */
        @Override
        public DateTimeField getField() {
            return iHourMinuteSecond.getField(iFieldIndex);
        }

        /**
         * Gets the partial that this property belongs to.
         *
         * @return the partial
         */
        @Override
        protected ReadablePartial getReadablePartial() {
            return iHourMinuteSecond;
        }

        /**
         * Gets the partial that this property belongs to.
         *
         * @return the partial
         */
        public HourMinuteSecond getHourMinuteSecond() {
            return iHourMinuteSecond;
        }

        /**
         * Gets the value of this field.
         *
         * @return the field value
         */
        @Override
        public int get() {
            return iHourMinuteSecond.getValue(iFieldIndex);
        }

        // ----------------------------------------------------------------------
        // -
        /**
         * Adds to the value of this field in a copy of this HourMinuteSecond.
         * <p>
         * The value will be added to this field. If the value is too large to be added solely to this field then it will affect
         * larger fields. Smaller fields are unaffected.
         * <p>
         * The HourMinuteSecond attached to this property is unchanged by this call. Instead, a new instance is returned.
         *
         * @param valueToAdd
         *            the value to add to the field in the copy
         * @return a copy of the HourMinuteSecond with the field value changed
         * @throws IllegalArgumentException
         *             if the value isn't valid
         */
        public HourMinuteSecond addToCopy(int valueToAdd) {
            int[] newValues = iHourMinuteSecond.getValues();
            newValues = getField().add(iHourMinuteSecond, iFieldIndex, newValues, valueToAdd);
            return new HourMinuteSecond(iHourMinuteSecond, newValues);
        }

        /**
         * Adds to the value of this field in a copy of this HourMinuteSecond
         * wrapping within this field if the maximum value is reached.
         * <p>
         * The value will be added to this field. If the value is too large to be added solely to this field then it wraps within
         * this field. Other fields are unaffected.
         * <p>
         * The HourMinuteSecond attached to this property is unchanged by this call. Instead, a new instance is returned.
         *
         * @param valueToAdd
         *            the value to add to the field in the copy
         * @return a copy of the HourMinuteSecond with the field value changed
         * @throws IllegalArgumentException
         *             if the value isn't valid
         */
        public HourMinuteSecond addWrapFieldToCopy(int valueToAdd) {
            int[] newValues = iHourMinuteSecond.getValues();
            newValues = getField().addWrapField(iHourMinuteSecond, iFieldIndex, newValues, valueToAdd);
            return new HourMinuteSecond(iHourMinuteSecond, newValues);
        }

        // ----------------------------------------------------------------------
        // -
        /**
         * Sets this field in a copy of the HourMinuteSecond.
         * <p>
         * The HourMinuteSecond attached to this property is unchanged by this call. Instead, a new instance is returned.
         *
         * @param value
         *            the value to set the field in the copy to
         * @return a copy of the HourMinuteSecond with the field value changed
         * @throws IllegalArgumentException
         *             if the value isn't valid
         */
        public HourMinuteSecond setCopy(int value) {
            int[] newValues = iHourMinuteSecond.getValues();
            newValues = getField().set(iHourMinuteSecond, iFieldIndex, newValues, value);
            return new HourMinuteSecond(iHourMinuteSecond, newValues);
        }

        /**
         * Sets this field in a copy of the HourMinuteSecond to a parsed text
         * value.
         * <p>
         * The HourMinuteSecond attached to this property is unchanged by this call. Instead, a new instance is returned.
         *
         * @param text
         *            the text value to set
         * @param locale
         *            optional locale to use for selecting a text symbol
         * @return a copy of the HourMinuteSecond with the field value changed
         * @throws IllegalArgumentException
         *             if the text value isn't valid
         */
        public HourMinuteSecond setCopy(String text, Locale locale) {
            int[] newValues = iHourMinuteSecond.getValues();
            newValues = getField().set(iHourMinuteSecond, iFieldIndex, newValues, text, locale);
            return new HourMinuteSecond(iHourMinuteSecond, newValues);
        }

        /**
         * Sets this field in a copy of the HourMinuteSecond to a parsed text
         * value.
         * <p>
         * The HourMinuteSecond attached to this property is unchanged by this call. Instead, a new instance is returned.
         *
         * @param text
         *            the text value to set
         * @return a copy of the HourMinuteSecond with the field value changed
         * @throws IllegalArgumentException
         *             if the text value isn't valid
         */
        public HourMinuteSecond setCopy(String text) {
            return setCopy(text, null);
        }

        // ----------------------------------------------------------------------
        // -
        /**
         * Returns a new HourMinuteSecond with this field set to the maximum
         * value for this field.
         *
         * <p>
         * The HourMinuteSecond attached to this property is unchanged by this call.
         *
         * @return a copy of the HourMinuteSecond with this field set to its
         *         maximum
         */
        public HourMinuteSecond withMaximumValue() {
            return setCopy(getMaximumValue());
        }

        /**
         * Returns a new HourMinuteSecond with this field set to the minimum
         * value for this field.
         * <p>
         * The HourMinuteSecond attached to this property is unchanged by this call.
         *
         * @return a copy of the HourMinuteSecond with this field set to its
         *         minimum
         */
        public HourMinuteSecond withMinimumValue() {
            return setCopy(getMinimumValue());
        }
    }
}
TOP

Related Classes of org.fenixedu.academic.util.HourMinuteSecond$Property

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.