Class RegularTimePeriod
- All Implemented Interfaces:
Comparable
,MonthConstants
,TimePeriod
- Direct Known Subclasses:
Day
,FixedMillisecond
,Hour
,Millisecond
,Minute
,Month
,Quarter
,Second
,Week
,Year
This class is immutable, and all subclasses should be immutable also.
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic RegularTimePeriod
createInstance
(Class c, Date millisecond, TimeZone zone, Locale locale) Creates a time period that includes the specified millisecond, assuming the given time zone.static Class
Returns a subclass ofRegularTimePeriod
that is smaller than the specified class.protected static Calendar
Creates or returns a thread-local Calendar instance.getEnd()
Returns the date/time that marks the end of the time period.abstract long
Returns the first millisecond of the time period.abstract long
getFirstMillisecond
(Calendar calendar) Returns the first millisecond of the time period, evaluated using the supplied calendar (which incorporates a timezone).abstract long
Returns the last millisecond of the time period.abstract long
getLastMillisecond
(Calendar calendar) Returns the last millisecond of the time period, evaluated using the supplied calendar (which incorporates a timezone).long
Returns the millisecond closest to the middle of the time period.long
getMiddleMillisecond
(Calendar calendar) Returns the millisecond closest to the middle of the time period, evaluated using the supplied calendar (which incorporates a timezone).long
getMillisecond
(TimePeriodAnchor anchor, Calendar calendar) Returns the millisecond (relative to the epoch) corresponding to the specifiedanchor
using the suppliedcalendar
(which incorporates a time zone).abstract long
Returns a serial index number for the time unit.getStart()
Returns the date/time that marks the start of the time period.abstract RegularTimePeriod
next()
Returns the time period following this one, ornull
if some limit has been reached.abstract void
Recalculates the start date/time and end date/time for this time period relative to the supplied calendar (which incorporates a time zone).abstract RegularTimePeriod
previous()
Returns the time period preceding this one, ornull
if some lower limit has been reached.static void
setCalendarInstancePrototype
(Calendar calendar) Sets a global calendar prototype for time calculations.static void
setThreadLocalCalendarInstance
(Calendar calendar) Sets the thread-local calendar instance for time calculations.toString()
Returns a string representation of the time period.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Comparable
compareTo
-
Constructor Details
-
RegularTimePeriod
public RegularTimePeriod()
-
-
Method Details
-
createInstance
public static RegularTimePeriod createInstance(Class c, Date millisecond, TimeZone zone, Locale locale) Creates a time period that includes the specified millisecond, assuming the given time zone.- Parameters:
c
- the time period class.millisecond
- the time.zone
- the time zone.locale
- the locale.- Returns:
- The time period.
-
downsize
Returns a subclass ofRegularTimePeriod
that is smaller than the specified class.- Parameters:
c
- a subclass ofRegularTimePeriod
.- Returns:
- A class.
-
getCalendarInstance
Creates or returns a thread-local Calendar instance. This function is used by the various subclasses to obtain a calendar for date-time to/from ms-since-epoch conversions (and to determine the first day of the week, in case ofWeek
).If a thread-local calendar was set with
setThreadLocalCalendarInstance(Calendar)
, then it is simply returned.Otherwise, If a global calendar prototype was set with
setCalendarInstancePrototype(Calendar)
, then it is cloned and set as the thread-local calendar instance for future use, as if it was set withsetThreadLocalCalendarInstance(Calendar)
.Otherwise, if neither is set, a new instance will be created every time with
Calendar.getInstance()
, resorting to JFreeChart 1.5.0 behavior (leading to huge load on GC and high memory consumption if many instances are created).- Returns:
- a thread-local Calendar instance
-
setThreadLocalCalendarInstance
Sets the thread-local calendar instance for time calculations.RegularTimePeriod
instances sometimes need aCalendar
to perform time calculations (date-time from/to milliseconds-since-epoch). In JFreeChart 1.5.0, they created a newCalendar
instance every time they needed one. This created a huge load on GC and lead to high memory consumption. To avoid this, a thread-localCalendar
instance can be set, which will then be used for time calculations every time, unless the caller passes a specificCalendar
instance in places where the API allows it.If the specified calendar is
null
, or if this method was never called, then the next time a calendar instance is needed, a new one will be created by cloning the global prototype set withsetCalendarInstancePrototype(Calendar)
. If none was set either, then a new instance will be created every time withCalendar.getInstance()
, resorting to JFreeChart 1.5.0 behavior.- Parameters:
calendar
- the new thread-local calendar instance
-
setCalendarInstancePrototype
Sets a global calendar prototype for time calculations.RegularTimePeriod
instances sometimes need aCalendar
to perform time calculations (date-time from/to milliseconds-since-epoch). In JFreeChart 1.5.0, they created a newCalendar
instance every time they needed one. This created a huge load on GC and lead to high memory consumption. To avoid this, a prototypeCalendar
can be set, which will be then cloned by every thread that needs aCalendar
instance. The prototype is not cloned right away, and stored instead for later cloning, therefore the caller must not alter the prototype after it has been passed to this method.If the prototype is
null
, then thread-local calendars set withsetThreadLocalCalendarInstance(Calendar)
will be used instead. If none was set for some thread, then a new instance will be created withCalendar.getInstance()
every time one is needed. However, if the prototype was already cloned by some thread, then setting it tonull
has no effect, and that thread must explicitly set its own instance tonull
or something else to get rid of the cloned calendar.Calling
setCalendarInstancePrototype(Calendar.getInstance())
somewhere early in an application will effectively mimic JFreeChart 1.5.0 behavior (using the default calendar everywhere unless explicitly specified), while preventing the many-allocations problem. There is one important caveat, however: once a prototype is cloned by some thread, callingTimeZone.setDefault(TimeZone)
orLocale.setDefault(Locale)
} will have no effect on future calculations. To avoid this problem, simply set the default time zone and locale before setting the prototype.- Parameters:
calendar
- the new thread-local calendar instance
-
previous
Returns the time period preceding this one, ornull
if some lower limit has been reached.- Returns:
- The previous time period (possibly
null
).
-
next
Returns the time period following this one, ornull
if some limit has been reached.- Returns:
- The next time period (possibly
null
).
-
getSerialIndex
Returns a serial index number for the time unit.- Returns:
- The serial index number.
-
peg
Recalculates the start date/time and end date/time for this time period relative to the supplied calendar (which incorporates a time zone).- Parameters:
calendar
- the calendar (null
not permitted).
-
getStart
Returns the date/time that marks the start of the time period. This method returns a newDate
instance every time it is called.- Specified by:
getStart
in interfaceTimePeriod
- Returns:
- The start date/time.
- See Also:
-
getEnd
Returns the date/time that marks the end of the time period. This method returns a newDate
instance every time it is called.- Specified by:
getEnd
in interfaceTimePeriod
- Returns:
- The end date/time.
- See Also:
-
getFirstMillisecond
Returns the first millisecond of the time period. This will be determined relative to the time zone specified in the constructor, or in the calendar instance passed in the most recent call to thepeg(Calendar)
method.- Returns:
- The first millisecond of the time period.
- See Also:
-
getFirstMillisecond
Returns the first millisecond of the time period, evaluated using the supplied calendar (which incorporates a timezone).- Parameters:
calendar
- the calendar (null
not permitted).- Returns:
- The first millisecond of the time period.
- Throws:
NullPointerException
- ifcalendar
isnull
.- See Also:
-
getLastMillisecond
Returns the last millisecond of the time period. This will be determined relative to the time zone specified in the constructor, or in the calendar instance passed in the most recent call to thepeg(Calendar)
method.- Returns:
- The last millisecond of the time period.
- See Also:
-
getLastMillisecond
Returns the last millisecond of the time period, evaluated using the supplied calendar (which incorporates a timezone).- Parameters:
calendar
- the calendar (null
not permitted).- Returns:
- The last millisecond of the time period.
- See Also:
-
getMiddleMillisecond
Returns the millisecond closest to the middle of the time period.- Returns:
- The middle millisecond.
-
getMiddleMillisecond
Returns the millisecond closest to the middle of the time period, evaluated using the supplied calendar (which incorporates a timezone).- Parameters:
calendar
- the calendar.- Returns:
- The middle millisecond.
-
getMillisecond
Returns the millisecond (relative to the epoch) corresponding to the specifiedanchor
using the suppliedcalendar
(which incorporates a time zone).- Parameters:
anchor
- the anchor (null
not permitted).calendar
- the calendar (null
not permitted).- Returns:
- Milliseconds since the epoch.
-
toString
Returns a string representation of the time period.
-