/* * 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.field; import java.util.Locale; import org.joda.time.Chronology; import org.joda.time.DateTimeField; import org.joda.time.DateTimeFieldType; import org.joda.time.DateTimeUtils; import org.joda.time.DurationField; import org.joda.time.ReadableInstant; import org.joda.time.ReadablePartial; /** * AbstractPartialFieldProperty is a base class for binding a * ReadablePartial to a DateTimeField. *

* It allows the date and time manipulation code to be field based yet * still easy to use. * * @author Stephen Colebourne * @author Brian S O'Neill * @since 1.0 */ public abstract class AbstractPartialFieldProperty { /** * Constructor. */ protected AbstractPartialFieldProperty() { super(); } //----------------------------------------------------------------------- /** * Gets the field being used. * * @return the field */ public abstract DateTimeField getField(); /** * Gets the field type being used. * * @return the field type */ public DateTimeFieldType getFieldType() { return getField().getType(); } /** * Gets the name of the field. * * @return the field name */ public String getName() { return getField().getName(); } /** * Gets the partial instant being used. * * @return the partial instant */ public abstract ReadablePartial getReadablePartial(); //----------------------------------------------------------------------- /** * Gets a value from the partial instant. * * @return the current value */ public abstract int get(); /** * Gets a text value from the partial instant. * * @param locale optional locale to use for selecting a text symbol * @return the current text value * @see DateTimeField#getAsText */ public String getAsText(Locale locale) { return getField().getAsText(getReadablePartial(), get(), locale); } /** * Gets a text value from the partial instant. * * @return the current text value * @see DateTimeField#getAsText */ public String getAsText() { return getAsText(null); } /** * Gets a short text value from the partial instant. * * @param locale optional locale to use for selecting a text symbol * @return the current text value * @see DateTimeField#getAsShortText */ public String getAsShortText(Locale locale) { return getField().getAsShortText(getReadablePartial(), get(), locale); } /** * Gets a short text value from the partial instant. * * @return the current text value * @see DateTimeField#getAsShortText */ public String getAsShortText() { return getAsShortText(null); } //----------------------------------------------------------------------- /** * Returns the duration per unit value of this field. For example, if this * field represents "hour of day", then the duration is an hour. * * @return the duration of this field, or UnsupportedDurationField */ public DurationField getDurationField() { return getField().getDurationField(); } /** * Returns the range duration of this field. For example, if this field * represents "hour of day", then the range duration is a day. * * @return the range duration of this field, or null if field has no range */ public DurationField getRangeDurationField() { return getField().getRangeDurationField(); } //----------------------------------------------------------------------- /** * Gets the minimum value for the field ignoring the current time. * * @return the minimum value * @see DateTimeField#getMinimumValue */ public int getMinimumValueOverall() { return getField().getMinimumValue(); } /** * Gets the minimum value for this field given the current field values. * * @return the minimum value * @see DateTimeField#getMinimumValue */ public int getMinimumValue() { return getField().getMinimumValue(getReadablePartial()); } /** * Gets the maximum value for the field ignoring the current time. * * @return the maximum value * @see DateTimeField#getMaximumValue */ public int getMaximumValueOverall() { return getField().getMaximumValue(); } /** * Gets the maximum value for this field given the current field values. * * @return the maximum value * @see DateTimeField#getMaximumValue */ public int getMaximumValue() { return getField().getMaximumValue(getReadablePartial()); } //----------------------------------------------------------------------- /** * Gets the maximum text length for the field. * * @param locale optional locale to use for selecting a text symbol * @return the maximum length * @see DateTimeField#getMaximumTextLength */ public int getMaximumTextLength(Locale locale) { return getField().getMaximumTextLength(locale); } /** * Gets the maximum short text length for the field. * * @param locale optional locale to use for selecting a text symbol * @return the maximum length * @see DateTimeField#getMaximumShortTextLength */ public int getMaximumShortTextLength(Locale locale) { return getField().getMaximumShortTextLength(locale); } //----------------------------------------------------------------------- /** * Compare this field to the same field on another instant. *

* The comparison is based on the value of the same field type, irrespective * of any difference in chronology. Thus, if this property represents the * hourOfDay field, then the hourOfDay field of the other instant will be queried * whether in the same chronology or not. * * @param instant the instant to compare to * @return negative value if this is less, 0 if equal, or positive value if greater * @throws IllegalArgumentException if the instant is null or the instant * doesn't support the field of this property */ public int compareTo(ReadableInstant instant) { if (instant == null) { throw new IllegalArgumentException("The instant must not be null"); } int thisValue = get(); Chronology chrono = DateTimeUtils.getChronology(instant.getChronology()); int otherValue = getFieldType().getField(chrono).get(instant.getMillis()); if (thisValue < otherValue) { return -1; } else if (thisValue > otherValue) { return 1; } else { return 0; } } /** * Compare this field to the same field on another partial instant. *

* The comparison is based on the value of the same field type, irrespective * of any difference in chronology. Thus, if this property represents the * hourOfDay field, then the hourOfDay field of the other partial will be queried * whether in the same chronology or not. * * @param partial the partial to compare to * @return negative value if this is less, 0 if equal, or positive value if greater * @throws IllegalArgumentException if the instant is null * @throws IllegalArgumentException if the field of this property cannot be queried * on the specified instant */ public int compareTo(ReadablePartial partial) { if (partial == null) { throw new IllegalArgumentException("The instant must not be null"); } int thisValue = get(); int otherValue = partial.get(getFieldType()); if (thisValue < otherValue) { return -1; } else if (thisValue > otherValue) { return 1; } else { return 0; } } //----------------------------------------------------------------------- /** * Compares this property to another. * * @param object the object to compare to * @return true if equal */ public boolean equals(Object object) { if (this == object) { return true; } if (object instanceof AbstractPartialFieldProperty) { AbstractPartialFieldProperty other = (AbstractPartialFieldProperty) object; if (get() == other.get() && getFieldType() == other.getFieldType() && getReadablePartial().getChronology() == other.getReadablePartial().getChronology()) { return true; } } return false; } //----------------------------------------------------------------------- /** * Output a debugging string. * * @return debugging string */ public String toString() { return "Property[" + getName() + "]"; } }