sos.dated.util
Class AbstractDates

java.lang.Object
  |
  +--sos.dated.util.AbstractDates
All Implemented Interfaces:
Dates
Direct Known Subclasses:
TreeDates

public abstract class AbstractDates
extends java.lang.Object
implements Dates

A partial implementation of a series of date ranges. The purpose of this class is to make it easier to implement the Dates interface. To create an unmodifiable date range series, the programmer should subclass this class and define dateIterator(Date). To create a fully modifiable date range series, the programmer must also define addAll, addRange, clear, removeAll, removeRange, and retainAll.

Since:
1.0
See Also:
TreeDates, Dates

Constructor Summary
protected AbstractDates()
          No-argument constructor available only to subclasses.
 
Method Summary
 Dates addAll(Dates series)
          Adds all date ranges given by dates to this series of dates.
 Dates addRange(java.util.Date from, java.util.Date to)
          Adds a date range to this series of dates.
 void clear()
          Removes all date ranges from this series of dates.
 void complement()
          Modifies this Dates object so that it contains only the ranges not currently in this Dates object.
 DateIterator dateIterator()
          Returns a date iterator positioned at the first range in the series.
abstract  DateIterator dateIterator(java.util.Date date)
          Returns an iterator over the date ranges, beginning with from, inclusive, and ending with to, exclusive.
 boolean equals(java.lang.Object obj)
          Indicates if this series of dates is equivalent to the specified object.
 boolean excludesAll(Dates dates)
          Indicates if this series does not overlap another series of dates.
 boolean excludesRange(java.util.Date from, java.util.Date to)
          Indicates if no date range in this series overlaps the specified date range.
 int hashCode()
          Returns the hash code value of this series of dates.
 boolean includes(java.util.Date date)
          Indicates if a date range in this series contains the specified date.
 boolean includesAll(Dates dates)
          Indicates if this series of dates contains all the date ranges given by dates.
 boolean includesRange(java.util.Date from, java.util.Date to)
          Indicates if a date range in this series contains the specified date range.
 boolean isEmpty()
          Indicates if this series of dates is empty.
 Dates removeAll(Dates series)
          Removes all dates ranges given by dates from this series of dates.
 Dates removeRange(java.util.Date from, java.util.Date to)
          Removes the specified range from this series of dates.
 Dates retainAll(Dates series)
          Removes all but the date ranges given by dates from this series of dates.
 int size()
          Returns the number of date ranges included in this series.
 java.util.Date[] toDateArray()
          Converts this series of dates to an array of dates, where a date at an even index represents the start (inclusive) of a range and a date at an odd index represents the end (exclusive) of a range.
 java.util.Date[] toDateArray(java.util.Date[] a)
          Converts this series of dates to an array of dates, where a date at an even index represents the start (inclusive) of a range and a date at an odd index represents the end (exclusive) of a range.
 java.lang.String toString()
          Returns a string representation of this series of dates.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AbstractDates

protected AbstractDates()
No-argument constructor available only to subclasses.

Method Detail

addAll

public Dates addAll(Dates series)
Adds all date ranges given by dates to this series of dates.

This implementation throws an UnsupportedOperationException.

Specified by:
addAll in interface Dates
Parameters:
series - series of dates to be added
Returns:
a series of dates indicating the date ranges that were added as a result of the call
Throws:
java.lang.NullPointerException - if dates is null

addRange

public Dates addRange(java.util.Date from,
                      java.util.Date to)
Adds a date range to this series of dates.

This implementation throws an UnsupportedOperationException.

Specified by:
addRange in interface Dates
Parameters:
from - start of the date range, inclusive
to - end of the date range, exclusive
Returns:
a series of dates indicating the date ranges that were added as a result of the call

clear

public void clear()
Removes all date ranges from this series of dates.

This implementation throws an UnsupportedOperationException.

Specified by:
clear in interface Dates

complement

public void complement()
Modifies this Dates object so that it contains only the ranges not currently in this Dates object.

This implementation throws an UnsupportedOperationException.

Specified by:
complement in interface Dates

dateIterator

public DateIterator dateIterator()
Returns a date iterator positioned at the first range in the series. A call to this method is equivalent to dateIterator(DatedObject.MIN_DATE).

This implementation invokes dateIterator(DatedObject.MIN_DATE).

Specified by:
dateIterator in interface Dates
Returns:
a date iterator positioned at the first range in the series.

dateIterator

public abstract DateIterator dateIterator(java.util.Date date)
Description copied from interface: Dates
Returns an iterator over the date ranges, beginning with from, inclusive, and ending with to, exclusive.

Specified by:
dateIterator in interface Dates
Returns:
an iterator over the date ranges, constrained to the specified range

equals

public boolean equals(java.lang.Object obj)
Indicates if this series of dates is equivalent to the specified object.

This implementation first checks reference equality and then casts obj to another Dates object. Finally, it iterates through the ranges of each, checking for equal ranges.

Specified by:
equals in interface Dates
Overrides:
equals in class java.lang.Object
Parameters:
obj - object to be compared to this series of dates
Returns:
true if this series equals obj

excludesAll

public boolean excludesAll(Dates dates)
Indicates if this series does not overlap another series of dates.

This implementation iterates through the ranges of dates and checks excludesRange for each range.

Specified by:
excludesAll in interface Dates
Parameters:
dates - date ranges to test for exclusion
Returns:
true if no date range of this series overlaps a date range of the specified series
Throws:
java.lang.NullPointerException - if dates is null

excludesRange

public boolean excludesRange(java.util.Date from,
                             java.util.Date to)
Indicates if no date range in this series overlaps the specified date range.

This implementation checks if the date range (if one exists) of an iterator positioned at from does not contain from or to.

Specified by:
excludesRange in interface Dates
Parameters:
from - start of date range (inclusive) to test for exclusion
to - end of date range (exclusive) to test for exclusion
Returns:
true if no date range of this series overlaps the specified date range

hashCode

public int hashCode()
Description copied from interface: Dates
Returns the hash code value of this series of dates. The value of a Dates object is defined to be
   hashCode += 31 * ( from.hashCode() + to.hashCode() );
 
for each range [from,to) in this object.

Specified by:
hashCode in interface Dates
Overrides:
hashCode in class java.lang.Object
Returns:
the hash code value of this series of dates.

includes

public boolean includes(java.util.Date date)
Indicates if a date range in this series contains the specified date.

This implementation gets a date iterator at date and checks if nextFrom is less than or equal to date.

Specified by:
includes in interface Dates
Parameters:
date - date to test for inclusion
Returns:
true if this series of dates contains the specified date
Throws:
java.lang.NullPointerException - if date is null

includesAll

public boolean includesAll(Dates dates)
Indicates if this series of dates contains all the date ranges given by dates.

This implementation iterates through the date ranges of dates, invoking includesRange for each range.

Specified by:
includesAll in interface Dates
Parameters:
dates - date ranges to test for inclusion
Returns:
true if this series of dates contains all of the specified date ranges
Throws:
java.lang.NullPointerException - if dates is null

includesRange

public boolean includesRange(java.util.Date from,
                             java.util.Date to)
Indicates if a date range in this series contains the specified date range.

This implementation checks if the first range of the iterator produced by dateIterator(from) contains from and to.

Specified by:
includesRange in interface Dates
Parameters:
from - start of date range (inclusive) to test for inclusion
to - end of date range (exclusive) to test for inclusion
Returns:
true if this series of dates contains the specified date range

isEmpty

public boolean isEmpty()
Indicates if this series of dates is empty.

This implementation executes

   return !dateIterator().hasNext();
 

Specified by:
isEmpty in interface Dates
Returns:
true if no date ranges exist in this series

removeAll

public Dates removeAll(Dates series)
Removes all dates ranges given by dates from this series of dates.

This implementation throws an UnsupportedOperationException.

Specified by:
removeAll in interface Dates
Parameters:
series - date ranges to be removed
Returns:
a series of dates indicating the date ranges that were removed as a result of the call

removeRange

public Dates removeRange(java.util.Date from,
                         java.util.Date to)
Removes the specified range from this series of dates.

This implementation throws an UnsupportedOperationException.

Specified by:
removeRange in interface Dates
Parameters:
from - start of date range to remove (inclusive)
to - end of date range to remove (exclusive)
Returns:
a series of dates indicating the date ranges that were removed as a result of the call

retainAll

public Dates retainAll(Dates series)
Removes all but the date ranges given by dates from this series of dates.

This implementation throws an UnsupportedOperationException.

Specified by:
retainAll in interface Dates
Parameters:
series - date ranges to retain
Returns:
a series of dates indicating the date ranges that were removed as a result of the call

size

public int size()
Returns the number of date ranges included in this series.

This implementation counts the number of ranges returned by an iterator positioned at the beginning.

Specified by:
size in interface Dates
Returns:
the number of date ranges included in this series.

toDateArray

public java.util.Date[] toDateArray()
Description copied from interface: Dates
Converts this series of dates to an array of dates, where a date at an even index represents the start (inclusive) of a range and a date at an odd index represents the end (exclusive) of a range.

Specified by:
toDateArray in interface Dates
Returns:
an array of dates representing this series of dates

toDateArray

public java.util.Date[] toDateArray(java.util.Date[] a)
Description copied from interface: Dates
Converts this series of dates to an array of dates, where a date at an even index represents the start (inclusive) of a range and a date at an odd index represents the end (exclusive) of a range.

Specified by:
toDateArray in interface Dates
Returns:
an array of dates representing this series of dates

toString

public java.lang.String toString()
Returns a string representation of this series of dates.

This implementation uses a StringBuffer to form the returned string. It first appends "{". For each range returned by an iterator positioned at the beginning, it appends "[from,to)" where from and to denote the start and end of the range, respectively. It separates each range with "," and ends the string with "}".

Overrides:
toString in class java.lang.Object
Returns:
a string representation of this series of dates.



Copyright 2003 Side of Software (SOS). All rights reserved.