com.prozima.time
Class TimePeriod

java.lang.Object
  extended by com.prozima.time.TimePeriod
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Comparable<TimePeriod>

public class TimePeriod
extends java.lang.Object
implements java.lang.Cloneable, java.lang.Comparable<TimePeriod>

Immutable class which represents a period of time beginning at a specific time of the day and lasting a specific duration. This class has no concept of Date, only a time period during the day.


Constructor Summary
TimePeriod(int inStart, int inDuration)
          Create a time period.
 
Method Summary
 TimePeriod clone()
           
 int compareTo(TimePeriod inOther)
          A TimePeriod is less than (earlier than) another if it starts before the other.
 boolean contains(TimePeriod inOther)
          Check if this time period totally contains another time period.
 boolean equals(java.lang.Object inObject)
           
 int getDuration()
          Get the duration of a time period in minutes.
 int getEndInMins()
          Get the end time of a time period.
 java.util.List<TimePeriod> getNonOverlaps(TimePeriod inOther)
          Get TimePeriods that represents the non-overlapping parts of this period and another TimePeriod.
 TimePeriod getOverlap(TimePeriod inOther)
          Get a TimePeriod that represents the overlap between this period and another TimePeriod.
static java.util.List<TimePeriod> getOverlappingPeriods(java.util.List<TimePeriod> inPeriodsA, java.util.List<TimePeriod> inPeriodsB)
          Generates a List of TimePeriods which represent the overlapping periods of two Lists of TimePeriods.
 int getStartInMins()
          Get the start time of a time period.
 int hashCode()
           
static boolean listsEqual(java.util.List<TimePeriod> inPeriodsA, java.util.List<TimePeriod> inPeriodsB)
          Compare two lists of TimePeriods to see if they are the same.
 boolean overlaps(TimePeriod inOther)
          Check if two time periods overlap each other.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TimePeriod

public TimePeriod(int inStart,
                  int inDuration)
Create a time period. Time periods should not span two days, create two separate time periods in this situation.

Parameters:
inStart - Starting time of the time period. Expressed in minutes since midnight 00:00. e.g. 1am == 60, 3am == 180.
inDuration - Duration of the time period in minutes. zero or negative duration is impossible.
Method Detail

clone

public TimePeriod clone()
                 throws java.lang.CloneNotSupportedException
Overrides:
clone in class java.lang.Object
Throws:
java.lang.CloneNotSupportedException

compareTo

public int compareTo(TimePeriod inOther)
A TimePeriod is less than (earlier than) another if it starts before the other. If two TimePeriods start at the same time, then the shortest is before the longest. Its only if they have the same start and duration that they are equal.

Specified by:
compareTo in interface java.lang.Comparable<TimePeriod>
Parameters:
inOther - Other TimePeriod object. Cannot be null.

contains

public boolean contains(TimePeriod inOther)
Check if this time period totally contains another time period. It contains the other time period when its start is before and its end is after those of the other.

Parameters:
inOther - Another time period. Cannot be null.
Returns:
true this TimePeriod does contain the other, otherwise false.

equals

public boolean equals(java.lang.Object inObject)
Overrides:
equals in class java.lang.Object

getDuration

public int getDuration()
Get the duration of a time period in minutes.

Returns:
Duration in minutes.

getEndInMins

public int getEndInMins()
Get the end time of a time period. Time of the day is expressed in minutes since midnight.

Returns:
Minutes since previous midnight.

getNonOverlaps

public java.util.List<TimePeriod> getNonOverlaps(TimePeriod inOther)
Get TimePeriods that represents the non-overlapping parts of this period and another TimePeriod.

Parameters:
inOther - Another TimePeriod. Cannot be null.
Returns:
List of TimePeriods - at most 2.

getOverlap

public TimePeriod getOverlap(TimePeriod inOther)
Get a TimePeriod that represents the overlap between this period and another TimePeriod.

Parameters:
inOther - Another TimePeriod. Cannot be null.
Returns:
null if there is no overlap, otherwise a TimePeriod.

getOverlappingPeriods

public static final java.util.List<TimePeriod> getOverlappingPeriods(java.util.List<TimePeriod> inPeriodsA,
                                                                     java.util.List<TimePeriod> inPeriodsB)
Generates a List of TimePeriods which represent the overlapping periods of two Lists of TimePeriods. Current implementation may return a highly fragmented List. TODO improve efficiency of generated List.

Parameters:
inPeriodsA - List of TimePeriods. Cannot be null.
inPeriodsB - Other List of TimePeriods. Cannot be null.
Returns:
List of TimePeriods
Throws:
java.lang.NullPointerException - thrown if one of the input parameters is null.

getStartInMins

public int getStartInMins()
Get the start time of a time period. Time of the day is expressed in minutes since midnight.

Returns:
Minutes since midnight.

hashCode

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

listsEqual

public static boolean listsEqual(java.util.List<TimePeriod> inPeriodsA,
                                 java.util.List<TimePeriod> inPeriodsB)
Compare two lists of TimePeriods to see if they are the same. Lists are copied before comparing because a sort is required.

Parameters:
inPeriodsA - List of TimePeriods. Cannot be null.
inPeriodsB - List of TimePeriods. Cannot be null.
Returns:
true if the two lists match, otherwise false.

overlaps

public boolean overlaps(TimePeriod inOther)
Check if two time periods overlap each other.

Parameters:
inOther - Another time period. Cannot be null.
Returns:
true if they do overlap, otherwise false.

toString

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