Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.joda.time.format
Class DateTimeParserBucket  view DateTimeParserBucket download DateTimeParserBucket.java

java.lang.Object
  extended byorg.joda.time.format.DateTimeParserBucket

public class DateTimeParserBucket
extends java.lang.Object

DateTimeParserBucket is an advanced class, intended mainly for parser implementations. It can also be used during normal parsing operations to capture more information about the parse.

This class allows fields to be saved in any order, but be physically set in a consistent order. This is useful for parsing against formats that allow field values to contradict each other.

Field values are applied in an order where the "larger" fields are set first, making their value less likely to stick. A field is larger than another when it's range duration is longer. If both ranges are the same, then the larger field has the longer duration. If it cannot be determined which field is larger, then the fields are set in the order they were saved.

For example, these fields were saved in this order: dayOfWeek, monthOfYear, dayOfMonth, dayOfYear. When computeMillis is called, the fields are set in this order: monthOfYear, dayOfYear, dayOfMonth, dayOfWeek.

DateTimeParserBucket is mutable and not thread-safe.

Since:
1.0

Nested Class Summary
(package private) static class DateTimeParserBucket.SavedField
           
(package private)  class DateTimeParserBucket.SavedState
           
 
Field Summary
private  org.joda.time.Chronology iChrono
          The chronology to use for parsing.
private  java.util.Locale iLocale
          The locale to use for parsing.
private  long iMillis
           
private  int iOffset
           
private  java.lang.Integer iPivotYear
          Used for parsing two-digit years.
private  DateTimeParserBucket.SavedField[] iSavedFields
           
private  int iSavedFieldsCount
           
private  boolean iSavedFieldsShared
           
private  java.lang.Object iSavedState
           
private  org.joda.time.DateTimeZone iZone
           
 
Constructor Summary
DateTimeParserBucket(long instantLocal, org.joda.time.Chronology chrono, java.util.Locale locale)
          Constucts a bucket.
DateTimeParserBucket(long instantLocal, org.joda.time.Chronology chrono, java.util.Locale locale, java.lang.Integer pivotYear)
          Constucts a bucket, with the option of specifying the pivot year for two-digit year parsing.
 
Method Summary
 long computeMillis()
          Computes the parsed datetime by setting the saved fields.
 long computeMillis(boolean resetFields)
          Computes the parsed datetime by setting the saved fields.
 org.joda.time.Chronology getChronology()
          Gets the chronology of the bucket, which will be a local (UTC) chronology.
 java.util.Locale getLocale()
          Returns the locale to be used during parsing.
 int getOffset()
          Returns the time zone offset in milliseconds used by computeMillis, unless getZone doesn't return null.
 java.lang.Integer getPivotYear()
          Returns the pivot year used for parsing two-digit years.
 org.joda.time.DateTimeZone getZone()
          Returns the time zone used by computeMillis, or null if an offset is used instead.
 boolean restoreState(java.lang.Object savedState)
          Restores the state of this bucket from a previously saved state.
 void saveField(org.joda.time.DateTimeField field, int value)
          Saves a datetime field value.
 void saveField(org.joda.time.DateTimeFieldType fieldType, int value)
          Saves a datetime field value.
 void saveField(org.joda.time.DateTimeFieldType fieldType, java.lang.String text, java.util.Locale locale)
          Saves a datetime field text value.
private  void saveField(DateTimeParserBucket.SavedField field)
           
 java.lang.Object saveState()
          Saves the state of this bucket, returning it in an opaque object.
 void setOffset(int offset)
          Set a time zone offset to be used when computeMillis is called, which overrides the time zone.
 void setPivotYear(java.lang.Integer pivotYear)
          Sets the pivot year to use when parsing two digit years.
 void setZone(org.joda.time.DateTimeZone zone)
          Set a time zone to be used when computeMillis is called, which overrides any set time zone offset.
private static void sort(java.lang.Comparable[] array, int high)
          Sorts elements [0,high).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

iChrono

private final org.joda.time.Chronology iChrono
The chronology to use for parsing.


iMillis

private final long iMillis

iZone

private org.joda.time.DateTimeZone iZone

iOffset

private int iOffset

iLocale

private java.util.Locale iLocale
The locale to use for parsing.


iPivotYear

private java.lang.Integer iPivotYear
Used for parsing two-digit years.


iSavedFields

private DateTimeParserBucket.SavedField[] iSavedFields

iSavedFieldsCount

private int iSavedFieldsCount

iSavedFieldsShared

private boolean iSavedFieldsShared

iSavedState

private java.lang.Object iSavedState
Constructor Detail

DateTimeParserBucket

public DateTimeParserBucket(long instantLocal,
                            org.joda.time.Chronology chrono,
                            java.util.Locale locale)
Constucts a bucket.


DateTimeParserBucket

public DateTimeParserBucket(long instantLocal,
                            org.joda.time.Chronology chrono,
                            java.util.Locale locale,
                            java.lang.Integer pivotYear)
Constucts a bucket, with the option of specifying the pivot year for two-digit year parsing.

Since:
1.1
Method Detail

getChronology

public org.joda.time.Chronology getChronology()
Gets the chronology of the bucket, which will be a local (UTC) chronology.


getLocale

public java.util.Locale getLocale()
Returns the locale to be used during parsing.


getZone

public org.joda.time.DateTimeZone getZone()
Returns the time zone used by computeMillis, or null if an offset is used instead.


setZone

public void setZone(org.joda.time.DateTimeZone zone)
Set a time zone to be used when computeMillis is called, which overrides any set time zone offset.


getOffset

public int getOffset()
Returns the time zone offset in milliseconds used by computeMillis, unless getZone doesn't return null.


setOffset

public void setOffset(int offset)
Set a time zone offset to be used when computeMillis is called, which overrides the time zone.


getPivotYear

public java.lang.Integer getPivotYear()
Returns the pivot year used for parsing two-digit years.

If null is returned, this indicates default behaviour

Since:
1.1

setPivotYear

public void setPivotYear(java.lang.Integer pivotYear)
Sets the pivot year to use when parsing two digit years.

If the value is set to null, this will indicate that default behaviour should be used.

Since:
1.1

saveField

public void saveField(org.joda.time.DateTimeField field,
                      int value)
Saves a datetime field value.


saveField

public void saveField(org.joda.time.DateTimeFieldType fieldType,
                      int value)
Saves a datetime field value.


saveField

public void saveField(org.joda.time.DateTimeFieldType fieldType,
                      java.lang.String text,
                      java.util.Locale locale)
Saves a datetime field text value.


saveField

private void saveField(DateTimeParserBucket.SavedField field)

saveState

public java.lang.Object saveState()
Saves the state of this bucket, returning it in an opaque object. Call restoreState to undo any changes that were made since the state was saved. Calls to saveState may be nested.


restoreState

public boolean restoreState(java.lang.Object savedState)
Restores the state of this bucket from a previously saved state. The state object passed into this method is not consumed, and it can be used later to restore to that state again.


computeMillis

public long computeMillis()
Computes the parsed datetime by setting the saved fields. This method is idempotent, but it is not thread-safe.


computeMillis

public long computeMillis(boolean resetFields)
Computes the parsed datetime by setting the saved fields. This method is idempotent, but it is not thread-safe.


sort

private static void sort(java.lang.Comparable[] array,
                         int high)
Sorts elements [0,high). Calling java.util.Arrays isn't always the right choice since it always creates an internal copy of the array, even if it doesn't need to. If the array slice is small enough, an insertion sort is chosen instead, but it doesn't need a copy!

This method has a modified version of that insertion sort, except it doesn't create an unnecessary array copy. If high is over 10, then java.util.Arrays is called, which will perform a merge sort, which is faster than insertion sort on large lists.

The end result is much greater performace when computeMillis is called. Since the amount of saved fields is small, the insertion sort is a better choice. Additional performance is gained since there is no extra array allocation and copying. Also, the insertion sort here does not perform any casting operations. The version in java.util.Arrays performs casts within the insertion sort loop.