time
Class GregorianChronology

java.lang.Object
  extended by time.GregorianChronology
All Implemented Interfaces:
Chronology

public final class GregorianChronology
extends java.lang.Object
implements Chronology

This class provides a concrete gregorian chronology. It is backed by a java.util.GregorianChronology. It is written to make a example of Chronology. This class is immutable and thread-safe.

Please note that this current implementation is not still under work, and not finished.

Author:
Arnaud Roques

Field Summary
static TimeField<java.lang.Integer> DAY_OF_MONTH
          Day of the month, from 1 to 31
 TimeField<DayOfWeek> DAY_OF_WEEK
          Day of the week, which values if a DayOfWeek.
static TimeField<java.lang.Integer> DAY_OF_YEAR
          Day of year, from 1 to 366
static TimeField<java.lang.Integer> HOUR_OF_DAY
          Hour of the day, from 0 to 23
static TimeField<java.lang.Integer> MILLISECOND
          Millisecond, from 0 to 999
static TimeField<java.lang.Integer> MINUTE
          Minute of the hour, from 0 to 59
static TimeField<Month> MONTH
          Month, which values must be a Month object, from JANUARY to DECEMBER
static TimeField<java.lang.Integer> SECOND
          Second, from 0 to 59
static TimeField<java.lang.Integer> WEEK_OF_YEAR
          Week of year, from 1 to 53
static TimeField<java.lang.Integer> YEAR
          Year, between -9999 and +9999.
 
Constructor Summary
GregorianChronology()
          Build a new GregorianChronology backed by a default GregorianCalendar using the default time zone and the default locale.
GregorianChronology(java.util.GregorianCalendar gc)
          Build a new GregorianChronology backed by a specific GregorianCalendar.
 
Method Summary
 java.util.List<Time> asList(Time startingTime, int startingPosition)
          Return a immutable view of some Time objects of this chronology starting at a specified Time.
 java.util.List<Time> asList(TimeMask mask)
          Return a immutable view of all Time objects of this chronology with a specified TimeMask.
 java.util.SortedSet<Time> asSortedSet(TimeMask mask)
          Return a immutable view of all Time objects of this chronology with a specified TimeMask.
 Instant getInstant(Time time)
          Convert a Time object into Instant according to this chronology object.
 Interval getInterval(Time time)
          Convert a Time object into an Interval object, according to this chronology.
 Time getTime(Instant instant)
          Convert an Instant object into a Time object with all known TimeField of this chronology.
 Time getTime(Instant instant, TimeMask mask)
          Convert an Instant object into a Time object with a precise TimeMask according to this chronology.
 Time getTime(Interval interval)
          Convert an Interval object into a Time object with all known TimeField of this chronology.
 Time getTime(Interval interval, TimeMask mask)
          Convert an Interval object into a Time object with a precise TimeMask according to this chronology.
 boolean isConsistentForInstant(Time time)
          Test if a Time object is consistent for instant.
 boolean isConsistentForInterval(Time time)
          Test if a Time object is consistent for interval.
 java.util.SortedSet<TimeField> knownFields()
          Return all TimeField of this chronology.
 Time next(Time time)
          Return the next Time object that directly follows a specified Time object according to this chronology.
 Time previous(Time time)
          Return the previous Time object that is just before a specified Time object according to this chronology.
 java.util.SortedSet<Time> split(Time timeToSplit, TimeField... fieldsToAdd)
          Return a immutable view with Time objects by adding some TimeField to an existing Time object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

YEAR

public static final TimeField<java.lang.Integer> YEAR
Year, between -9999 and +9999. Unlike GregorianCalendar, there is no concept of ERA in this chronology. It means that "1 BC" Year in Gregorian calendar is "0" in this chronology, and "2 BC" is "-1"...


MONTH

public static final TimeField<Month> MONTH
Month, which values must be a Month object, from JANUARY to DECEMBER


DAY_OF_MONTH

public static final TimeField<java.lang.Integer> DAY_OF_MONTH
Day of the month, from 1 to 31


DAY_OF_YEAR

public static final TimeField<java.lang.Integer> DAY_OF_YEAR
Day of year, from 1 to 366


WEEK_OF_YEAR

public static final TimeField<java.lang.Integer> WEEK_OF_YEAR
Week of year, from 1 to 53


HOUR_OF_DAY

public static final TimeField<java.lang.Integer> HOUR_OF_DAY
Hour of the day, from 0 to 23


MINUTE

public static final TimeField<java.lang.Integer> MINUTE
Minute of the hour, from 0 to 59


SECOND

public static final TimeField<java.lang.Integer> SECOND
Second, from 0 to 59


MILLISECOND

public static final TimeField<java.lang.Integer> MILLISECOND
Millisecond, from 0 to 999


DAY_OF_WEEK

public final TimeField<DayOfWeek> DAY_OF_WEEK
Day of the week, which values if a DayOfWeek. Unlike other TimeField of the GregorianChronology, this field may vary upon different instance of object, as the order of weekday depends on the Locale.

On the other hand, this implementation garantees that if two different GrogorianChronology have the same first dayweek, they will share the same DAY_OF_WEEK field.

Constructor Detail

GregorianChronology

public GregorianChronology()
Build a new GregorianChronology backed by a default GregorianCalendar using the default time zone and the default locale.


GregorianChronology

public GregorianChronology(java.util.GregorianCalendar gc)
Build a new GregorianChronology backed by a specific GregorianCalendar.

To garantee immutability, gc is cloned.

Parameters:
gc - calendar used by this Chronology.
Method Detail

next

public Time next(Time time)
          throws java.lang.ClassCastException
Description copied from interface: Chronology
Return the next Time object that directly follows a specified Time object according to this chronology.

More formally, this method returns a Time object that have the same TimeMask than time, with the following property: the ending instant (according to this chronoloy) of time is the starting instant (according to this chronology) of the resulted Time object.

If no exception is thrown, the following test is always true:

        getInterval(time).getEnd().equals(next(time).getStart())
 

This is equivalent of the following call:

        Iterator

Specified by:
next in interface Chronology
Parameters:
time - the time whose next time is return
Returns:
the Time object that directly follows time.
Throws:
java.lang.ClassCastException - if time does not have a TimeMask compatible with this Chronology object.

previous

public Time previous(Time time)
              throws java.lang.ClassCastException
Description copied from interface: Chronology
Return the previous Time object that is just before a specified Time object according to this chronology.

More formally, this method returns a Time object that have the same TimeMask than time, with the following property: the starting instant (according to this chronoloy) of time is the ending instant (according to this chronology) of the resulted Time object.

If no exception is thrown, the following test is always true:

        getInterval(time).getStart().equals(previous(time).getEnd())
 

This is equivalent of the following call:

        asSortedSet(time.getTimeMask()).headSet(time).last()
 

Specified by:
previous in interface Chronology
Parameters:
time - the time whose previous time is return
Returns:
the Time object that is just before time.
Throws:
java.lang.ClassCastException - if time does not have a TimeMask compatible with this Chronology object.

asSortedSet

public java.util.SortedSet<Time> asSortedSet(TimeMask mask)
Description copied from interface: Chronology
Return a immutable view of all Time objects of this chronology with a specified TimeMask. Such a view provides a bridge with Java Collections Framework.

For example, assuming that myTime is a Time object, calling:

        asSortedSet(myTime.getTimeMask()).contains(myTime)
 

returns true if myTime is consistent for interval.

Another example, assuming that dayMask is a TimeMask object like [YEAR, MONTH, DAY_OF_MONTH], and that day1 and day2 are two differents days, calling:

        asSortedSet(dayMask).subSet(day1, day2).size()
 

returns the number of days between day1 and day2 according to this chronology.

To have the day following day1, call:

        Iterator
To have the day before day1, call:
        asSortedSet(dayMask).headSet(day1).last()
 

Specified by:
asSortedSet in interface Chronology
Parameters:
mask - the specified TimeMask
Returns:
a immutable view of all Time objects of this chronology with a specified TimeMask.

asList

public java.util.List<Time> asList(TimeMask mask)
Description copied from interface: Chronology
Return a immutable view of all Time objects of this chronology with a specified TimeMask. Such a view provides a bridge with Java Collections Framework.

For example, assuming that myTime is a Time object, calling:

        asList(myTime.getTimeMask()).contains(myTime)
 

returns true if myTime is consistent for interval.

To have the 100th day following day1, call:

        int idx = asList(dayMask).indexOf(day1);
        asList(dayMask).get(idx+100);
 

Please note than unlike Set, a List cannot have more than Integer.MAX_VALUE. So some mask that would have define a too big list cannot be used with this method.

Specified by:
asList in interface Chronology
Parameters:
mask - the specified TimeMask
Returns:
a immutable view of all Time objects of this chronology with a specified TimeMask.

asList

public java.util.List<Time> asList(Time startingTime,
                                   int startingPosition)
Description copied from interface: Chronology
Return a immutable view of some Time objects of this chronology starting at a specified Time. Such a view provides a bridge with Java Collections Framework.

All Time objects of the returned list have the same TimeMask as startingTime. The first element of the list is startingTime if startingPosition=0. Otherwise, if startingPosition>0, it specifies the position of startingTime in the returned list. So we have:

        asList(myTime, x).get(x).equals(myTime)==true
 

If startingPosition<0, the returned list does not contains startingTime and starts with the (-startingPosition) Time objects that follows logically startingTime.

To sumurize, startingPosition is always the logically (even if it's negative) position of startingTime in the returned list.

To have the 100th day following day1, call:

        asList(day1, 0).get(100);
 
or:
        asList(day1, 100).get(0);
 

Please note than unlike Set, a List cannot have more than Integer.MAX_VALUE.

Specified by:
asList in interface Chronology
Parameters:
startingTime - an absolute reference for Time
startingPosition - the logical position of startingTime in the returned list
Returns:
a immutable view of some Time objects of this chronology.

split

public java.util.SortedSet<Time> split(Time timeToSplit,
                                       TimeField... fieldsToAdd)
Description copied from interface: Chronology
Return a immutable view with Time objects by adding some TimeField to an existing Time object.

The provided timeToSplit must be consistent for interval. The returned set is a view of timeToSplit as set of its component. For example:

The returned set contains Time objects whose TimeMask is the time mask of timeToExplode merged with all fieldsToAdd. All possibles values are contained in the returned set.

For example, if one have:

        timeToSplit = new Time(DAY_MASK, 2005, Month.NOVEMBER, 11);
 

calling the following method:
        split(timeToSplit, HOUR, MINUTE);
 

return a set that contains all hours/minutes from 00:00 to 23:59.
[2005, NOVEMBER, 11, 0 , 0] ... [2005, NOVEMBER, 11, 0 , 59],
[2005, NOVEMBER, 11, 1 , 0] ... [2005, NOVEMBER, 11, 1 , 59],
[2005, NOVEMBER, 11, 2 , 0] ... [2005, NOVEMBER, 11, 2 , 59],
...
[2005, NOVEMBER, 11, 22 , 0] ... [2005, NOVEMBER, 11, 22 , 59],
[2005, NOVEMBER, 11, 23 , 0] ... [2005, NOVEMBER, 11, 23 , 59]


Another example, if you want to know the number of days in a month:
        Time myMonth = new Time(MONTH_MASK, 2005, Month.DECEMBER);
        int nbDays = myChronology.split(myMonth, DAY_OF_MONTH).size();
 

Specified by:
split in interface Chronology
Parameters:
timeToSplit - the time to split.
fieldsToAdd - all TimeField to add.
Returns:
an immutable view of Time objects.

getInstant

public Instant getInstant(Time time)
                   throws java.lang.ClassCastException
Description copied from interface: Chronology
Convert a Time object into Instant according to this chronology object.

Specified by:
getInstant in interface Chronology
Parameters:
time - the Time object to be converted.
Returns:
Instant corresponding to the t object.
Throws:
java.lang.ClassCastException - if time does not have a TimeMask compatible with this Chronology object.

getInterval

public Interval getInterval(Time time)
                     throws java.lang.ClassCastException
Description copied from interface: Chronology
Convert a Time object into an Interval object, according to this chronology.

Specified by:
getInterval in interface Chronology
Parameters:
time - the Time object to be converted
Returns:
the Interval corresponding to t
Throws:
java.lang.ClassCastException - if time is not compatible with this chronology.

getTime

public Time getTime(Instant instant)
Description copied from interface: Chronology
Convert an Instant object into a Time object with all known TimeField of this chronology.

Specified by:
getTime in interface Chronology
Parameters:
instant - the Instant object to be converted.
Returns:
a Time object with all known TimeField of this chronology.

getTime

public Time getTime(Instant instant,
                    TimeMask mask)
             throws java.lang.ClassCastException
Description copied from interface: Chronology
Convert an Instant object into a Time object with a precise TimeMask according to this chronology.

Calling this method should be equivalent as the following:

        getTime(instant).subMap(mask)
 

Specified by:
getTime in interface Chronology
Parameters:
instant - the Instant object to be converted.
mask - the TimeMask to use.
Returns:
a Time object with mask as TimeMask.
Throws:
java.lang.ClassCastException - if mask is not compatible with this chronology.

getTime

public Time getTime(Interval interval)
Description copied from interface: Chronology
Convert an Interval object into a Time object with all known TimeField of this chronology.

Specified by:
getTime in interface Chronology
Parameters:
interval - the Interval object to be converted.
Returns:
a Time object with all known TimeField of this chronology.

getTime

public Time getTime(Interval interval,
                    TimeMask mask)
             throws java.lang.ClassCastException
Description copied from interface: Chronology
Convert an Interval object into a Time object with a precise TimeMask according to this chronology.

Calling this method should be equivalent as the following:

        getTime(interval).subMap(mask)
 

Specified by:
getTime in interface Chronology
Parameters:
interval - the Interval object to be converted.
mask - the TimeMask to use.
Returns:
a Time object with mask as TimeMask..
Throws:
java.lang.ClassCastException - if mask is not compatible with this chronology.

isConsistentForInstant

public boolean isConsistentForInstant(Time time)
Description copied from interface: Chronology
Test if a Time object is consistent for instant.

A Time object is consistent for instant if it provides enough information to point a unique Instant in the whole Time.

Specified by:
isConsistentForInstant in interface Chronology
Parameters:
time - the Time object to be tested.
Returns:
true if time is consistant for instant.

isConsistentForInterval

public boolean isConsistentForInterval(Time time)
Description copied from interface: Chronology
Test if a Time object is consistent for interval.

A Time object is consistent for interval, if it provides enough information to point a unique Interval in the whole Time (for example, a day, a week, a month...).

Specified by:
isConsistentForInterval in interface Chronology
Parameters:
time - the Time object to be tested.
Returns:
true if time is consistant for interval.

knownFields

public java.util.SortedSet<TimeField> knownFields()
Description copied from interface: Chronology
Return all TimeField of this chronology.

All TimeMask and Time object used with this chronology should only use one or several of thoses TimeField objects, otherwise a ClassCastException will be thrown by the other methods of this chronology when such an unappropriate TimeField will be used.

Specified by:
knownFields in interface Chronology
Returns:
all TimeField of this chronology