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

dk.rode.thesis.facade
Class AckermannSequence

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

@Participant(value="SubsystemClass")
 class AckermannSequence
extends AbstractSequence<Integer>
implements Sequence<Integer>

An Ackermann sequence calculates the next value of the Ackermann function, v = A(m, n), each time next() is invoked, using a fixed value for m and thus increasing n, either indefinitely, or until a maximum value has been reached.

The running time of next() is not consistent each time invoked!

An Ackermann sequence is bounded if a maximum value of n is supplied at construction time, otherwise unbounded, but always unique and consistent.

Implementation note:
This implementation is naive. The stack depth very quickly becomes an issue!

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  List<Integer> calculated
          The Ackermann values calculated so far for m and all n smaller or equal to n.
private  int m
          The constant value of m for all Ackermann values v = A(m,n) calculated by this sequence.
private  int maximumN
          The maximum n value to consider.
private  int n
          The value of n for the Ackermann value v = A(m,n) currently set for this sequence.
 
Fields inherited from class dk.rode.thesis.meta.model.AbstractSequence
state
 
Constructor Summary
AckermannSequence()
          Constructor, which uses zero as the value of m.
AckermannSequence(AckermannSequence sequence)
          Copy constructor.
AckermannSequence(int m)
          Constructor, which uses the fixed value of m supplied as m.
AckermannSequence(int m, int maximumN)
          Constructor, which uses the fixed value of m supplied as m.
 
Method Summary
 boolean bounded()
          Returns true if this sequence is bounded, i.e.
private  int calculate(int m, int n)
          Calculates A(m,n) and returns the value.
(package private)  int calculate(int m, int n, Map<Integer,Map<Integer,Integer>> ms)
          Calculates all A(m,n).
 boolean consistent()
          Returns true.
 AckermannSequence copy()
          Returns a copy of this sequence that will start at the same sequence index as this sequence.
 Integer current()
          Returns the current element from this sequence.
 boolean equals(Object object)
          Returns true if object is an instance of this sequence type using the same m as this sequence and currently at the same n, false if not.
 int hashCode()
          Returns the hash code of this sequence.
 int m()
          Returns the constant value of m for all Ackermann values v = A(m,n) calculated by this sequence.
 int n()
          The value of n for the Ackermann value v = A(m,n) currently set for this sequence.
 Integer next()
          Returns the next element from this sequence.
 void reset()
          Resets this sequence to start over if it is consistent.
 boolean unique()
          Returns true.
 
Methods inherited from class dk.rode.thesis.meta.model.AbstractSequence
getStringablePolicy, state, toString, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, 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

calculated

private List<Integer> calculated
The Ackermann values calculated so far for m and all n smaller or equal to n.

Never null.


m

private final int m
The constant value of m for all Ackermann values v = A(m,n) calculated by this sequence.


maximumN

private final int maximumN
The maximum n value to consider.

A value of Const.INFINITE means no maximum n value.


n

private int n
The value of n for the Ackermann value v = A(m,n) currently set for this sequence.

n will be increased with each call to next() until maximumN value has been reached, if any, which will cause this sequence to restart.

Constructor Detail

AckermannSequence

public AckermannSequence()
Constructor, which uses zero as the value of m.

There is no maximum value of n, i.e. from zero to infinity.


AckermannSequence

public AckermannSequence(AckermannSequence sequence)
Copy constructor.

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

AckermannSequence

public AckermannSequence(int m)
Constructor, which uses the fixed value of m supplied as m.

There is no maximum value of n, i.e. from zero to infinity.

Parameters:
m - The fixed m value used for all n in A(m,n) when calculating the next value by this sequence; cannot be negative.
Throws:
IllegalArgumentException - If m is negative.

AckermannSequence

public AckermannSequence(int m,
                         int maximumN)
Constructor, which uses the fixed value of m supplied as m.

n will run from zero to maximumN, if maximumN is not Const.INFINITE.

Parameters:
m - The fixed m value used for all n in A(m,n) when calculating the next value by this sequence; cannot be negative.
maximumN - The maximum value for n to consider. A value of Const.INFINITE means no maximum. Cannot be negative.
Throws:
IllegalArgumentException - If m or maximumN are negative.
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<Integer>
Returns:
True if bounded, false if not.
See Also:
Sequence.unique()

calculate

private final int calculate(int m,
                            int n)
Calculates A(m,n) and returns the value.

Parameters:
m - The m argument.
n - The n argument.
Returns:
The Ackermann value A(m,n).

calculate

final int calculate(int m,
                    int n,
                    Map<Integer,Map<Integer,Integer>> ms)
Calculates all A(m,n).

Parameters:
m - The m value.
n - The n value.
ms - A map storing already calculated values with m and n as keys.
Returns:
The Ackermann value A(m,n).

consistent

public boolean consistent()
Returns true.

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

copy

public AckermannSequence 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<Integer>
Specified by:
copy in interface Copyable<Sequence<Integer>>
Returns:
A copy of this sequence; never null.

current

public Integer 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<Integer>
Returns:
The current element; never null.

equals

public boolean equals(Object object)
Returns true if object is an instance of this sequence type using the same m as this sequence and currently at the same n, false if not.

Overrides:
equals in class Object
Parameters:
object - The object to test; can be null.
Returns:
True if equal, false if not.

hashCode

public int hashCode()
Returns the hash code of this sequence.

Overrides:
hashCode in class Object
Returns:
The hash code.

m

public int m()
Returns the constant value of m for all Ackermann values v = A(m,n) calculated by this sequence.

Returns:
The constant value of m.

n

public int n()
The value of n for the Ackermann value v = A(m,n) currently set for this sequence.

Returns:
The value of n.

next

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

Specified by:
next in interface Sequence<Integer>
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<Integer>
Overrides:
reset in class AbstractSequence<Integer>

unique

public boolean unique()
Returns true.

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