Evaluating Software Design Patterns
— the "Gang of Four" patterns implemented in Java 6

dk.rode.thesis.observer
Class DateSequence

java.lang.Object
  extended by dk.rode.thesis.meta.model.AbstractSequence<E>
      extended by dk.rode.thesis.observer.SequenceObserversSequence<Date,Sequence.State>
          extended by dk.rode.thesis.observer.DateSequence
All Implemented Interfaces:
Sequence<Date>, AspectObservable<SequenceObserver<Sequence.State>,Sequence.State>, AspectObservableSequence<SequenceObserver<Sequence.State>,Sequence.State,Date>, Observable<SequenceObserver<Sequence.State>>, ObservableSequence<SequenceObserver<Sequence.State>,Sequence.State,Date>, SequenceObserver<Sequence.State>, Copyable<Sequence<Date>>, StrictCopyable<Sequence<Date>>, Stringable<Sequence<Date>>

@Participant(value={"ConcreteSubject","ConcreteObserver"})
public class DateSequence
extends SequenceObserversSequence<Date,Sequence.State>

An observable date sequence is a sequence that will advance an initial date by one day each time next() is invoked and then notify relevant observers of its state changes.

The returned dates only contain hours, day, month, and year.

Modifying returned dates will not affect a date sequence.

A date sequence may be bounded, but is consistent and unique.

Author:
Gunni Rode / rode.dk

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.meta.model.Sequence
Sequence.State
 
Field Summary
private  long date
          The current date in milliseconds.
private  Date end
          The end date of this sequence, if any.
private  Date start
          The start date of this sequence.
 
Fields inherited from class dk.rode.thesis.meta.model.AbstractSequence
state
 
Constructor Summary
DateSequence()
          No-arg constructor.
DateSequence(Date start)
          Constructor.
DateSequence(Date start, Date end)
          Constructor.
DateSequence(Date start, int days)
          Constructor.
DateSequence(DateSequence sequence)
          Copy constructor.
 
Method Summary
 boolean bounded()
          Returns true if this sequence is bounded, i.e.
 boolean consistent()
          Returns true.
 DateSequence copy()
          Returns a deep copy of this object.
 Date current()
          Returns the current element from this sequence.
protected  Date doNext()
          Hook for sub-classes to perform the actual next() operation without notifying observers.
 Sequence.State getAspect()
          Hook for sub-classes to deliver the changed aspect value to notify observers about immediately after SequenceObserversSequence.doNext() has been invoked, if any.
private static Date getDate(Date date)
          Returns a date corresponding to date, but where minutes, seconds, and milliseconds have been cleared.
private static Date getDate(Date date, int days)
          Returns a date corresponding to date plus the number of days supplied as days, but where minutes, seconds, and milliseconds have been cleared.
 void reset()
          Resets this sequence to start over if it is consistent.
 boolean unique()
          Returns true.
 
Methods inherited from class dk.rode.thesis.observer.SequenceObserversSequence
addObserver, addObserver, getAspects, getObservers, getObservers, next, removeObserver, removeObserver, sequenceEvent
 
Methods inherited from class dk.rode.thesis.meta.model.AbstractSequence
getStringablePolicy, state, toString, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface dk.rode.thesis.meta.model.Sequence
state
 
Methods inherited from interface dk.rode.thesis.strategy.Stringable
getStringablePolicy, toString
 

Field Detail

date

private long date
The current date in milliseconds.

Hours, minutes, seconds, and milliseconds are ignored, i.e. always zero.

Never null.


end

private final Date end
The end date of this sequence, if any. Null means no end date.

Invariant: start <= date [<= end]


start

private final Date start
The start date of this sequence.

Invariant: start <= date [<= end].

Never null.

Constructor Detail

DateSequence

public DateSequence()
No-arg constructor.

The start date is set to "now", and no end date is used.


DateSequence

public DateSequence(Date start)
Constructor.

This date sequence will not have an end date.

Parameters:
start - The date to start this sequence from; cannot be null. Only hours, days, months, and years are used.
Throws:
NullPointerException - If start is null.

DateSequence

public DateSequence(Date start,
                    Date end)
Constructor.

If end is null, this date sequence is unbounded.

Parameters:
start - The date to start this sequence from; cannot be null. Only hours, days, months, and years are used.
end - The end date of this sequence; can be null. Only hours, days, months, and years are used.
Throws:
NullPointerException - If start is null.
IllegalArgumentException - If end is before start.

DateSequence

public DateSequence(Date start,
                    int days)
Constructor.

This date sequence will have an end date days from start. If days is smaller than one, one day will be used.

Parameters:
start - The date to start this sequence from; cannot be null. Only hours, days, months, and years are used.
days - The number of days in this sequence.
Throws:
NullPointerException - If start is null.

DateSequence

public DateSequence(DateSequence sequence)
Copy constructor.

Parameters:
sequence - The date sequence to copy; cannot be null.
Throws:
NullPointerException - If sequence is null.
Method Detail

bounded

public boolean bounded()
Description copied from interface: Sequence
Returns true if this sequence is bounded, i.e. deliver values between the initial sequence value and some upper bound.

The same type of sequence may represent both bounded and unbounded sequences and the behaviour is determined and fixed at construction time. Bounded sequences will restart if they deliver consistent values.

Returns:
True if bounded, false if not.
See Also:
Sequence.unique()

consistent

public boolean consistent()
Returns true.

Returns:
True.

copy

public DateSequence copy()
Description copied from interface: Copyable
Returns a deep copy of this object.

Returns:
The copy; never null.

current

public Date current()
Description copied from interface: Sequence
Returns the current element from this sequence.

This method can be invoked even if Sequence.next() has not been invoked yet, thus delivering the initial value of this sequence.

Returns:
The current element; never null.

doNext

protected Date doNext()
Description copied from class: SequenceObserversSequence
Hook for sub-classes to perform the actual next() operation without notifying observers.

Specified by:
doNext in class SequenceObserversSequence<Date,Sequence.State>
Returns:
The next element; never null.

getAspect

public Sequence.State getAspect()
Description copied from class: SequenceObserversSequence
Hook for sub-classes to deliver the changed aspect value to notify observers about immediately after SequenceObserversSequence.doNext() has been invoked, if any.

If this method returns null, no notification is performed as the aspect has not changed.

Specified by:
getAspect in class SequenceObserversSequence<Date,Sequence.State>
Returns:
The changed aspect; or null if not changed.

getDate

private static final Date getDate(Date date)
Returns a date corresponding to date, but where minutes, seconds, and milliseconds have been cleared.

Parameters:
date - The date; cannot be null.
Returns:
A new corresponding Date instance; never null.

getDate

private static final Date getDate(Date date,
                                  int days)
Returns a date corresponding to date plus the number of days supplied as days, but where minutes, seconds, and milliseconds have been cleared.

Parameters:
date - The date; cannot be null.
days - Days to add to the date; cannot be negative.
Returns:
A new corresponding Date instance; never null.
Throws:
IllegalArgumentException - If days is negative.

reset

public void reset()
Description copied from interface: Sequence
Resets this sequence to start over if it is consistent.

If this sequence is consistent, the sequence will restart.

Specified by:
reset in interface Sequence<Date>
Overrides:
reset in class AbstractSequence<Date>

unique

public boolean unique()
Returns true.

Returns:
True.
See Also:
Sequence.consistent()

Gunni Rode / rode.dk

Feel free to use and/or modify the Java 6 source code developed for this thesis AT YOUR OWN RISK, but note that the source code comes WITHOUT ANY — and I do mean WITHOUT ANY — form of warranty WHAT SO EVER!

The original thesis and source code are available at rode.dk/thesis.