com.emarsys.ecommon.time
Class TimeInterval

java.lang.Object
  extended by com.emarsys.ecommon.time.TimeInterval

public class TimeInterval
extends java.lang.Object

Represents a time slot specified by two Calendar objects.

The start date must always be less or equal to the end date according to Calendar.compareTo(Calendar).

Note that a TimeInterval instances are immutable. Every operation that alters a TimeInterval will actually return new instance.

Author:
Michael "kULO" Kulovits

See Also:
Dates,


Field Summary
protected  java.util.Calendar end
           
protected  java.text.DateFormat format
           
protected  java.util.Calendar mean
           
protected  java.util.Calendar start
           
 
Constructor Summary
TimeInterval(java.util.Calendar start, java.util.Calendar end)
           
 
Method Summary
 boolean equals(java.lang.Object that)
           
static java.util.Calendar getDateIfWithinInterval(java.util.Calendar date, TimeInterval interval)
           Returns the passed Calendar object if the date is within the passed interval, otherwise null.
 long getDuration()
           
 java.util.Calendar getEnd()
           
static java.util.Calendar getFirstDateWithinInterval(java.util.List<java.util.Calendar> dates, TimeInterval interval)
           Returns the first of the passed dates that is within the passed interval.
 java.util.Calendar getMean()
           
 java.util.Calendar getStart()
           
 int hashCode()
           
 TimeInterval intersect(TimeInterval that)
           
 boolean isOverlapping(TimeInterval that)
           
 boolean isWithinInterval(java.util.Calendar date)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

start

protected java.util.Calendar start

end

protected java.util.Calendar end

mean

protected java.util.Calendar mean

format

protected java.text.DateFormat format
Constructor Detail

TimeInterval

public TimeInterval(java.util.Calendar start,
                    java.util.Calendar end)
Parameters:
start -
end -
Method Detail

getDateIfWithinInterval

public static java.util.Calendar getDateIfWithinInterval(java.util.Calendar date,
                                                         TimeInterval interval)

Returns the passed Calendar object if the date is within the passed interval, otherwise null.

This method can be used to check if a date is inside of a confidence interval which is specified by the passed TimeInterval.

If the passed date is within the passed, non-null TimeInterval then it will be returned, otherwise if the interval is null or the date is not within it null will be returned.
If the passed date is not set then null will be returned, too.

Parameters:
date - - the date to be returned if its within the interval
interval - - the interval to be
Returns:
a Calendar representing a date within the specified interval or null.

getFirstDateWithinInterval

public static java.util.Calendar getFirstDateWithinInterval(java.util.List<java.util.Calendar> dates,
                                                            TimeInterval interval)

Returns the first of the passed dates that is within the passed interval.

This method can be used to select a date out of a List of dates (which should be sorted by priority) that fits into a specified confidence TimeInterval.

If the passed List of Calendars is null or emtpy then in any case null will be returned.
If the passed TimeInterval is null then the first non-null date from the passed ones will be returned.

It might be a good practive to append a Calendar representing the current time and/or getMean() at the end of the passed List in order to get fallback values.

Parameters:
dates - - the dates to select the first one within the confidence interval from
interval - - the confidence interval for to select the passed dates from
Returns:
either a valid Calendar instance or null.
See Also:
getDateIfWithinInterval(Calendar, TimeInterval)

getEnd

public java.util.Calendar getEnd()
Returns:
the end of the TimeInterval

getStart

public java.util.Calendar getStart()
Returns:
the start of the TimeInterval

getMean

public java.util.Calendar getMean()
Returns:

getDuration

public long getDuration()
Returns:
the duration in milliseconds from the start to the end of this TimeInterval

isWithinInterval

public boolean isWithinInterval(java.util.Calendar date)
Parameters:
date -
Returns:
true iff date >= getStart() && date <= getEnd() according to Calendar.compareTo(Calendar), false otherwise.
See Also:
Calendar.compareTo(Calendar)

isOverlapping

public boolean isOverlapping(TimeInterval that)
Parameters:
that -
Returns:

intersect

public TimeInterval intersect(TimeInterval that)
Parameters:
that -
Returns:
null if the passed TimeInterval is null or if its not overlapping with this one, otherwise a new TimeInterval instance beginning with the latest beginning and the earliest end of this and the passed interval.

equals

public boolean equals(java.lang.Object that)
Overrides:
equals in class java.lang.Object
See Also:
Object.equals(java.lang.Object)

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object
See Also:
Object.hashCode()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
See Also:
Object.toString()


Copyright © 2010 emarsys AG. All Rights Reserved.