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

dk.rode.thesis.facade
Class FibonacciSequence

java.lang.Object
  extended by dk.rode.thesis.meta.model.AbstractSequence<BigInteger>
      extended by dk.rode.thesis.facade.FibonacciSequence
All Implemented Interfaces:
Sequence<BigInteger>, Copyable<Sequence<BigInteger>>, StrictCopyable<Sequence<BigInteger>>, Stringable<Sequence<BigInteger>>

@Participant(value="SubsystemClass")
public class FibonacciSequence
extends AbstractSequence<BigInteger>
implements Sequence<BigInteger>

A Fibonacci sequence represents an integer sequence, where each number delivered by next() is the sum of the two preceding numbers, except one which is the initial value:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, ..

A Fibonacci sequence is unbounded unless a maximum number to consider has been supplied at construction time, not unique (1), but consistent.

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  BigInteger current
          The current Fibonacci number.
private  BigInteger maximum
          Maximum number to consider.
private  BigInteger previous
          The previous Fibonacci number.
 
Fields inherited from class dk.rode.thesis.meta.model.AbstractSequence
state
 
Constructor Summary
FibonacciSequence()
          No-arg constructor.
FibonacciSequence(BigInteger maximum)
          Constructor.
FibonacciSequence(FibonacciSequence sequence)
          Copy constructor.
 
Method Summary
 boolean bounded()
          Returns true if this sequence is bounded, i.e.
 boolean consistent()
          Returns true.
 FibonacciSequence copy()
          Returns a copy of this sequence that will start at the same sequence index as this sequence.
 BigInteger current()
          Returns the current element from this sequence.
 BigInteger next()
          Returns the next element from this sequence.
 void reset()
          Resets this sequence to start over if it is consistent.
 boolean unique()
          Returns false.
 
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

current

private BigInteger current
The current Fibonacci number.

The first number is one.


maximum

private BigInteger maximum
Maximum number to consider.

The number need not be a Fibonacci number.

A value of null means no maximum.


previous

private BigInteger previous
The previous Fibonacci number.

Constructor Detail

FibonacciSequence

public FibonacciSequence()
No-arg constructor.

No maximum number is enforced.


FibonacciSequence

public FibonacciSequence(BigInteger maximum)
Constructor. The maximum Fibonacci number to be delivered will the smallest Fibonacci number smaller or equal to maximum, if not null.

Parameters:
maximum - The maximum bound; can be null.
Throws:
IllegalArgumentException - If maximum is not null, but not positive.

FibonacciSequence

public FibonacciSequence(FibonacciSequence sequence)
Copy constructor.

Parameters:
sequence - The 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.

Specified by:
bounded in interface Sequence<BigInteger>
Returns:
True if bounded, false if not.
See Also:
Sequence.unique()

consistent

public boolean consistent()
Returns true.

Specified by:
consistent in interface Sequence<BigInteger>
Returns:
True.

copy

public FibonacciSequence copy()
Description copied from interface: Sequence
Returns a copy of this sequence that will start at the same sequence index as this sequence.

Specified by:
copy in interface Sequence<BigInteger>
Specified by:
copy in interface Copyable<Sequence<BigInteger>>
Returns:
A copy of this sequence; never null.

current

public BigInteger 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.

Specified by:
current in interface Sequence<BigInteger>
Returns:
The current element; never null.

next

public BigInteger next()
Description copied from interface: Sequence
Returns the next element from this sequence.

Specified by:
next in interface Sequence<BigInteger>
Returns:
The next element; never null.
See Also:
Sequence.current(), Sequence.state()

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<BigInteger>
Overrides:
reset in class AbstractSequence<BigInteger>

unique

public boolean unique()
Returns false.

Specified by:
unique in interface Sequence<BigInteger>
Returns:
False.
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.