Side of Software
Dated Collections Library 2.0

sos.dated.util
Class TreeDates<D>

java.lang.Object
  extended by sos.dated.util.AbstractDates<D>
      extended by sos.dated.util.TreeDates<D>
Type Parameters:
D - the type of dates used in this series
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, Dates<D>

public class TreeDates<D>
extends AbstractDates<D>
implements Dates<D>, java.io.Serializable, java.lang.Cloneable

An implementation of Dates that uses a Red-Black tree to achieve logarithmic insertions and deletions. The following illustration shows a conceptual view of a small TreeDates where the "dates" are integers:

This implementation is serializable and cloneable; however, it is not synchronized. To use it safely in a multi-threaded situation, wrap an instance of the dated set with a call to DatedCollections.synchronizedDates.

Since:
1.0
See Also:
Dates, DatedCollections.synchronizedDates(Dates)

Constructor Summary
TreeDates()
          Creates a TreeDates with no dates.
TreeDates(Dates<D> dates)
          Creates a TreeDates initialized with the ranges of dates.
 
Method Summary
 Dates<D> addAll(Dates<D> dates)
          Adds all date ranges given by dates to this series of dates.
 Dates<D> addRange(D from, D to)
          Adds a date range to this series of dates.
 void clear()
          Removes all date ranges from this series of dates.
 java.lang.Object clone()
          Creates and returns a shallow copy of this object.
 void complement(D from, D to)
          Modifies this Dates object so that it contains only the ranges not currently in this Dates object.
 DateIterator<D> dateIterator(D at)
          Returns an iterator over the date ranges, starting at the range that contains the specified date.
 boolean includes(D at)
          Indicates if a date range in this series contains the specified date.
 boolean isEmpty()
          Indicates if this series of dates is empty.
 Dates<D> removeAll(Dates<D> dates)
          Removes all dates ranges given by dates from this series of dates.
 Dates<D> removeRange(D from, D to)
          Removes the specified range from this series of dates.
 Dates<D> retainAll(Dates<D> dates)
          Removes all but the date ranges given by dates from this series of dates.
 
Methods inherited from class sos.dated.util.AbstractDates
dateIterator, equals, excludesAll, excludesRange, hashCode, includesAll, includesRange, size, toDateArray, toDateArray, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface sos.dated.util.Dates
dateIterator, equals, excludesAll, excludesRange, hashCode, includesAll, includesRange, size, toDateArray, toDateArray
 

Constructor Detail

TreeDates

public TreeDates()
Creates a TreeDates with no dates.


TreeDates

public TreeDates(Dates<D> dates)
Creates a TreeDates initialized with the ranges of dates.

Parameters:
dates - date ranges for which to initialize this new TreeDates
Throws:
java.lang.NullPointerException - if dates is null
Method Detail

addAll

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

Specified by:
addAll in interface Dates<D>
Overrides:
addAll in class AbstractDates<D>
Parameters:
dates - 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<D> addRange(D from,
                         D to)
Adds a date range to this series of dates.

Specified by:
addRange in interface Dates<D>
Overrides:
addRange in class AbstractDates<D>
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
Throws:
java.lang.IllegalArgumentException - if from does not precede to
java.lang.NullPointerException - if from or to is null

clear

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

Specified by:
clear in interface Dates<D>
Overrides:
clear in class AbstractDates<D>

clone

public java.lang.Object clone()
Creates and returns a shallow copy of this object.

Overrides:
clone in class java.lang.Object
Returns:
a clone of this instance

complement

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

Specified by:
complement in interface Dates<D>
Overrides:
complement in class AbstractDates<D>
Throws:
java.lang.IllegalArgumentException - if from does not precede to
java.lang.NullPointerException - if from or to is null

dateIterator

public DateIterator<D> dateIterator(D at)
Description copied from interface: Dates
Returns an iterator over the date ranges, starting at the range that contains the specified date. Passing null as the argument will return an iterator positioned at the beginning of the series.

Specified by:
dateIterator in interface Dates<D>
Specified by:
dateIterator in class AbstractDates<D>
Parameters:
at - date to position the iteration
Returns:
an iterator over the date ranges, positioned immediately before the date range that contains the specified date

includes

public boolean includes(D at)
Indicates if a date range in this series contains the specified date.

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

isEmpty

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

Specified by:
isEmpty in interface Dates<D>
Overrides:
isEmpty in class AbstractDates<D>
Returns:
true if no date ranges exist in this series

removeAll

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

Specified by:
removeAll in interface Dates<D>
Overrides:
removeAll in class AbstractDates<D>
Parameters:
dates - date ranges to be removed
Returns:
a series of dates indicating the date ranges that were removed as a result of the call
Throws:
java.lang.NullPointerException - if dates is null

removeRange

public Dates<D> removeRange(D from,
                            D to)
Removes the specified range from this series of dates.

Specified by:
removeRange in interface Dates<D>
Overrides:
removeRange in class AbstractDates<D>
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
Throws:
java.lang.IllegalArgumentException - if from does not precede to
java.lang.NullPointerException - if from or to is null

retainAll

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

Specified by:
retainAll in interface Dates<D>
Overrides:
retainAll in class AbstractDates<D>
Parameters:
dates - date ranges to retain
Returns:
a series of dates indicating the date ranges that were removed as a result of the call
Throws:
java.lang.NullPointerException - if dates is null

Side of Software
Dated Collections Library 2.0

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