Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2001-2005 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;
 
Identifies a duration field, such as years or minutes, in a chronology-neutral way.

A duration field type defines the type of the field, such as hours. If does not directly enable any calculations, however it does provide a getField(org.joda.time.Chronology) method that returns the actual calculation engine for a particular chronology.

Instances of DurationFieldType are singletons. They can be compared using ==.

If required, you can create your own field, for example a quarters. You must create a subclass of DurationFieldType that defines the field type. This class returns the actual calculation engine from getField(org.joda.time.Chronology).

Author(s):
Stephen Colebourne
Brian S O'Neill
Since:
1.0
 
 public abstract class DurationFieldType implements Serializable {

    
Serialization version
 
     private static final long serialVersionUID = 8765135187319L;
 
     // Ordinals for standard field types.
     static final byte
         ERAS = 1,
         CENTURIES = 2,
         WEEKYEARS = 3,
         YEARS = 4,
         MONTHS = 5,
         WEEKS = 6,
         DAYS = 7,
         HALFDAYS = 8,
         HOURS = 9,
         MINUTES = 10,
         SECONDS = 11,
         MILLIS = 12;

    
The eras field type.
 
     static final DurationFieldType ERAS_TYPE = new StandardDurationFieldType("eras");
    
The centuries field type.
 
     static final DurationFieldType CENTURIES_TYPE = new StandardDurationFieldType("centuries");
    
The weekyears field type.
 
     static final DurationFieldType WEEKYEARS_TYPE = new StandardDurationFieldType("weekyears");
    
The years field type.
 
     static final DurationFieldType YEARS_TYPE = new StandardDurationFieldType("years");
    
The months field type.
 
     static final DurationFieldType MONTHS_TYPE = new StandardDurationFieldType("months");
    
The weeks field type.
 
     static final DurationFieldType WEEKS_TYPE = new StandardDurationFieldType("weeks");
    
The days field type.
 
     static final DurationFieldType DAYS_TYPE = new StandardDurationFieldType("days");
    
The halfdays field type.
 
     static final DurationFieldType HALFDAYS_TYPE = new StandardDurationFieldType("halfdays");
    
The hours field type.
 
     static final DurationFieldType HOURS_TYPE = new StandardDurationFieldType("hours");
    
The minutes field type.
 
     static final DurationFieldType MINUTES_TYPE = new StandardDurationFieldType("minutes");
    
The seconds field type.
 
     static final DurationFieldType SECONDS_TYPE = new StandardDurationFieldType("seconds");
    
The millis field type.
 
     static final DurationFieldType MILLIS_TYPE = new StandardDurationFieldType("millis");

    
The name of the field type.
 
     private final String iName;
 
     //-----------------------------------------------------------------------
     
Constructor.

Parameters:
name the name to use, which by convention, are plural.
 
     protected DurationFieldType(String name) {
         super();
          = name;
     }
 
     //-----------------------------------------------------------------------
     
Get the millis field type.

Returns:
the DateTimeFieldType constant
    public static DurationFieldType millis() {
        return ;
    }

    
Get the seconds field type.

Returns:
the DateTimeFieldType constant
    public static DurationFieldType seconds() {
        return ;
    }

    
Get the minutes field type.

Returns:
the DateTimeFieldType constant
    public static DurationFieldType minutes() {
        return ;
    }

    
Get the hours field type.

Returns:
the DateTimeFieldType constant
    public static DurationFieldType hours() {
        return ;
    }

    
Get the halfdays field type.

Returns:
the DateTimeFieldType constant
    public static DurationFieldType halfdays() {
        return ;
    }
    //-----------------------------------------------------------------------
    
Get the days field type.

Returns:
the DateTimeFieldType constant
    public static DurationFieldType days() {
        return ;
    }

    
Get the weeks field type.

Returns:
the DateTimeFieldType constant
    public static DurationFieldType weeks() {
        return ;
    }

    
Get the weekyears field type.

Returns:
the DateTimeFieldType constant
    public static DurationFieldType weekyears() {
        return ;
    }

    
Get the months field type.

Returns:
the DateTimeFieldType constant
    public static DurationFieldType months() {
        return ;
    }

    
Get the years field type.

Returns:
the DateTimeFieldType constant
    public static DurationFieldType years() {
        return ;
    }

    
Get the centuries field type.

Returns:
the DateTimeFieldType constant
    public static DurationFieldType centuries() {
        return ;
    }

    
Get the eras field type.

Returns:
the DateTimeFieldType constant
    public static DurationFieldType eras() {
        return ;
    }
    //-----------------------------------------------------------------------
    
Get the name of the field. By convention, names are plural.

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

    
Gets a suitable field for this type from the given Chronology.

Parameters:
chronology the chronology to use, null means ISOChronology in default zone
Returns:
a suitable field
    public abstract DurationField getField(Chronology chronology);

    
Checks whether this field supported in the given Chronology.

Parameters:
chronology the chronology to use, null means ISOChronology in default zone
Returns:
true if supported
    public boolean isSupported(Chronology chronology) {
        return getField(chronology).isSupported();
    }

    
Get a suitable debug string.

Returns:
debug string
    public String toString() {
        return getName();
    }
    private static class StandardDurationFieldType extends DurationFieldType {
        
Serialization version
        private static final long serialVersionUID = 31156755687123L;

        
The ordinal of the standard field type, for switch statements
        private final byte iOrdinal;

        
Constructor.

Parameters:
name the name to use
        StandardDurationFieldType(String namebyte ordinal) {
            super(name);
             = ordinal;
        }

        

Inheritdoc:
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj instanceof StandardDurationFieldType) {
                return  == ((StandardDurationFieldTypeobj).;
            }
            return false;
        }

        

Inheritdoc:
        @Override
        public int hashCode() {
            return (1 << );
        }
        public DurationField getField(Chronology chronology) {
            chronology = DateTimeUtils.getChronology(chronology);
            
            switch () {
                case :
                    return chronology.eras();
                case :
                    return chronology.centuries();
                case :
                    return chronology.weekyears();
                case :
                    return chronology.years();
                case :
                    return chronology.months();
                case :
                    return chronology.weeks();
                case :
                    return chronology.days();
                case :
                    return chronology.halfdays();
                case :
                    return chronology.hours();
                case :
                    return chronology.minutes();
                case :
                    return chronology.seconds();
                case :
                    return chronology.millis();
                default:
                    // Shouldn't happen.
                    throw new InternalError();
            }
        }

        
Ensure a singleton is returned.

Returns:
the singleton type
        private Object readResolve() {
            switch () {
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                default:
                    // Shouldn't happen.
                    return this;
            }
        }
    }
New to GrepCode? Check out our FAQ X