Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2001-2006 Stephen Colebourne
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 package org.joda.time.field;
 
 
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(s):
Stephen Colebourne
Brian S O'Neill
Since:
1.0
 
 public abstract class AbstractPartialFieldProperty {

    
Constructor.
 
     protected AbstractPartialFieldProperty() {
         super();
     }
 
     //-----------------------------------------------------------------------
     
Gets the field being used.

Returns:
the field
 
     public abstract DateTimeField getField();

    
Gets the field type being used.

Returns:
the field type
 
     public DateTimeFieldType getFieldType() {
         return getField().getType();
     }

    
Gets the name of the field.

Returns:
the field name
 
     public String getName() {
         return getField().getName();
     }

    
Gets the partial instant being used.

Returns:
the partial instant
 
     protected abstract ReadablePartial getReadablePartial();
 
     //-----------------------------------------------------------------------
     
Gets the value of this property from the instant.

For example, the following two lines of code are equivalent:

 partial.getDayOfMonth();
 partial.dayOfMonth().get();
 

Returns:
the current value
 
     public abstract int get();

    
Gets the value of this property from the instant as a string.

This method returns the value converted to a String using Integer.toString. This method does NOT return textual descriptions such as 'Monday' or 'January'. See getAsText() and getAsShortText() for those.

Returns:
the current value
Since:
1.1
See also:
org.joda.time.DateTimeField.get(long)
    public String getAsString() {
        return Integer.toString(get());
    }

    
Gets the textual value of this property from the instant as a string in the default locale.

This method returns the value converted to a String returning the appropriate textual description wherever possible. Thus, a day of week of 1 would return 'Monday' in English.

Returns:
the current text value
See also:
org.joda.time.DateTimeField.getAsText(long)
    public String getAsText() {
        return getAsText(null);
    }

    
Gets the textual value of this property from the instant as a string in the specified locale.

This method returns the value converted to a String returning the appropriate textual description wherever possible. Thus, a day of week of 1 would return 'Monday' in English.

Parameters:
locale locale to use for selecting a text symbol, null means default
Returns:
the current text value
See also:
org.joda.time.DateTimeField.getAsText(long)
    public String getAsText(Locale locale) {
        return getField().getAsText(getReadablePartial(), get(), locale);
    }

    
Gets the short textual value of this property from the instant as a string in the default locale.

This method returns the value converted to a String returning the appropriate textual description wherever possible. Thus, a day of week of 1 would return 'Mon' in English.

Returns:
the current text value
See also:
org.joda.time.DateTimeField.getAsShortText(long)
    public String getAsShortText() {
        return getAsShortText(null);
    }

    
Gets the short textual value of this property from the instant as a string in the specified locale.

This method returns the value converted to a String returning the appropriate textual description wherever possible. Thus, a day of week of 1 would return 'Mon' in English.

Parameters:
locale locale to use for selecting a text symbol, null means default
Returns:
the current text value
See also:
org.joda.time.DateTimeField.getAsShortText(long)
    public String getAsShortText(Locale locale) {
        return getField().getAsShortText(getReadablePartial(), get(), locale);
    }
    //-----------------------------------------------------------------------
    
Returns the duration per unit value of this field. For example, if this field represents "hour of day", then the duration is an hour.

Returns:
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.

Returns:
the range duration of this field, or null if field has no range
        return getField().getRangeDurationField();
    }
    //-----------------------------------------------------------------------
    
Gets the minimum value for the field ignoring the current time.

Returns:
the minimum value
See also:
org.joda.time.DateTimeField.getMinimumValue()
    public int getMinimumValueOverall() {
        return getField().getMinimumValue();
    }

    
Gets the minimum value for this field given the current field values.

Returns:
the minimum value
See also:
org.joda.time.DateTimeField.getMinimumValue()
    public int getMinimumValue() {
        return getField().getMinimumValue(getReadablePartial());
    }

    
Gets the maximum value for the field ignoring the current time.

Returns:
the maximum value
See also:
org.joda.time.DateTimeField.getMaximumValue()
    public int getMaximumValueOverall() {
        return getField().getMaximumValue();
    }

    
Gets the maximum value for this field given the current field values.

Returns:
the maximum value
See also:
org.joda.time.DateTimeField.getMaximumValue()
    public int getMaximumValue() {
        return getField().getMaximumValue(getReadablePartial());
    }
    //-----------------------------------------------------------------------
    
Gets the maximum text length for the field.

Parameters:
locale optional locale to use for selecting a text symbol
Returns:
the maximum length
See also:
org.joda.time.DateTimeField.getMaximumTextLength(java.util.Locale)
    public int getMaximumTextLength(Locale locale) {
        return getField().getMaximumTextLength(locale);
    }

    
Gets the maximum short text length for the field.

Parameters:
locale optional locale to use for selecting a text symbol
Returns:
the maximum length
See also:
org.joda.time.DateTimeField.getMaximumShortTextLength(java.util.Locale)
    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.

Parameters:
instant the instant to compare to
Returns:
negative value if this is less, 0 if equal, or positive value if greater
Throws:
java.lang.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();
        int otherValue = instant.get(getFieldType());
        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.

Parameters:
partial the partial to compare to
Returns:
negative value if this is less, 0 if equal, or positive value if greater
Throws:
java.lang.IllegalArgumentException if the instant is null
java.lang.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.

Parameters:
object the object to compare to
Returns:
true if equal
    public boolean equals(Object object) {
        if (this == object) {
            return true;
        }
        if (object instanceof AbstractPartialFieldProperty == false) {
            return false;
        }
        return
            get() == other.get() &&
            getFieldType() == other.getFieldType() &&
            FieldUtils.equals(getReadablePartial().getChronology(), other.getReadablePartial().getChronology());
    }
    //-----------------------------------------------------------------------
    
Gets a suitable hashcode for the object.

Returns:
the hashcode
Since:
1.3
    public int hashCode() {
        int hash = 19;
        hash = 13 * hash + get();
        hash = 13 * hash + getFieldType().hashCode();
        hash = 13 * hash + getReadablePartial().getChronology().hashCode();
        return hash;
    }
    //-----------------------------------------------------------------------
    
Output a debugging string.

Returns:
debugging string
    public String toString() {
        return "Property[" + getName() + "]";
    }
New to GrepCode? Check out our FAQ X