/* * Joda Software License, Version 1.0 * * * Copyright (c) 2001-2004 Stephen Colebourne. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Joda project (http://www.joda.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The name "Joda" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact licence@joda.org. * * 5. Products derived from this software may not be called "Joda", * nor may "Joda" appear in their name, without prior written * permission of the Joda project. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE JODA AUTHORS OR THE PROJECT * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Joda project and was originally * created by Stephen Colebourne . For more * information on the Joda project, please see . */ package org.joda.time.base; import org.joda.time.DurationFieldType; import org.joda.time.MutablePeriod; import org.joda.time.Period; import org.joda.time.ReadablePeriod; import org.joda.time.format.ISOPeriodFormat; /** * AbstractPeriod provides the common behaviour for period classes. *

* This class should generally not be used directly by API users. The * {@link ReadablePeriod} interface should be used when different * kinds of periods are to be referenced. *

* AbstractPeriod subclasses may be mutable and not thread-safe. * * @author Brian S O'Neill * @author Stephen Colebourne * @since 1.0 */ public abstract class AbstractPeriod implements ReadablePeriod { /** * Constructor. */ protected AbstractPeriod() { super(); } //----------------------------------------------------------------------- /** * Gets an array of the field types that this period supports. *

* The fields are returned largest to smallest, for example Hours, Minutes, Seconds. * * @return the fields supported in an array that may be altered, largest to smallest */ public DurationFieldType[] getFieldTypes() { DurationFieldType[] result = new DurationFieldType[size()]; for (int i = 0; i < result.length; i++) { result[i] = getFieldType(i); } return result; } /** * Gets an array of the value of each of the fields that this period supports. *

* The fields are returned largest to smallest, for example Hours, Minutes, Seconds. * Each value corresponds to the same array index as getFields() * * @return the current values of each field in an array that may be altered, largest to smallest */ public int[] getValues() { int[] result = new int[size()]; for (int i = 0; i < result.length; i++) { result[i] = getValue(i); } return result; } //----------------------------------------------------------------------- /** * Gets the value of one of the fields. *

* If the field type specified is not supported by the period then zero * is returned. * * @param type the field type to query, null returns zero * @return the value of that field, zero if field not supported */ public int get(DurationFieldType type) { int index = indexOf(type); if (index == -1) { return 0; } return getValue(index); } /** * Checks whether the field specified is supported by this period. * * @param type the type to check, may be null which returns false * @return true if the field is supported */ public boolean isSupported(DurationFieldType type) { return getPeriodType().isSupported(type); } /** * Gets the index of the field in this period. * * @param type the type to check, may be null which returns -1 * @return the index of -1 if not supported */ public int indexOf(DurationFieldType type) { return getPeriodType().indexOf(type); } //----------------------------------------------------------------------- /** * Get this period as an immutable Period object. * * @return a Period using the same field set and values */ public Period toPeriod() { return new Period(this); } /** * Get this object as a MutablePeriod. *

* This will always return a new MutablePeriod with the same fields. * * @return a MutablePeriod using the same field set and values */ public MutablePeriod toMutablePeriod() { return new MutablePeriod(this); } //----------------------------------------------------------------------- /** * Compares this object with the specified object for equality based * on the value of each field. All ReadablePeriod instances are accepted. * * @param period a readable period to check against * @return true if all the field values are equal, false if * not or the period is null or of an incorrect type */ public boolean equals(Object period) { if (this == period) { return true; } if (period instanceof ReadablePeriod == false) { return false; } ReadablePeriod other = (ReadablePeriod) period; if (size() != other.size()) { return false; } for (int i = 0, isize = size(); i < isize; i++) { if (getValue(i) != other.getValue(i) || getFieldType(i) != other.getFieldType(i)) { return false; } } return true; } /** * Gets a hash code for the period as defined by ReadablePeriod. * * @return a hash code */ public int hashCode() { int total = 17; for (int i = 0, isize = size(); i < isize; i++) { total = 27 * total + getValue(i); total = 27 * total + getFieldType(i).hashCode(); } return total; } //----------------------------------------------------------------------- /** * Gets the value as a String in the ISO8601 duration format. *

* For example, "P6H3M7S" represents 6 hours, 3 minutes, 7 seconds. *

* For more control over the output, see * {@link org.joda.time.format.PeriodFormatterBuilder PeriodFormatterBuilder}. * * @return the value as an ISO8601 string */ public String toString() { return ISOPeriodFormat.getInstance().standard().print(this); } }