public class FibonacciStackFrame extends Object
Constructor and Description |
---|
FibonacciStackFrame(int n,
FibonacciStackFrame caller)
Creates a stack frame to compute F(n).
|
Modifier and Type | Method and Description |
---|---|
void |
execute(Stack<FibonacciStackFrame> callStack)
Start or resume execution of this stack frame.
|
static Map<Integer,BigInteger> |
getCache()
Get the cache of already computed Fibonacci numbers.
|
FibonacciStackFrame |
getCaller()
Get the creator of this call stack.
|
BigInteger |
getfMinus1()
Get the current value of fMinus1.
|
BigInteger |
getfMinus2()
Get the current value of fMinus2.
|
int |
getN()
Get the value of n.
|
BigInteger |
getSum()
Get the current value of the sum F(n) = F(n - 1) + F(n - 2).
|
void |
receiveReturnValue(BigInteger retVal)
Receive a return value from another
FibonacciStackFrame
instance. |
void |
returnValueToCaller(BigInteger retVal,
Stack<FibonacciStackFrame> callStack)
Returns the value of F(n) to the creator (
this.caller )
of this stack frame if the creator was another FibonacciStackFrame
instance, and then pops this stack frame from the call stack. |
public FibonacciStackFrame(int n, FibonacciStackFrame caller)
caller
is a reference
to the FibonacciStackFrame
that created this stack frame. If
this stack frame is not being created by another
FibonacciStackFrame
instance, then caller
is
expected to be equal to null
.IllegalArgumentException
- if n < 0
n
- the Fibonccci number to computecaller
- the FibonacciStackFrame
that created this stack framepublic void receiveReturnValue(BigInteger retVal)
FibonacciStackFrame
instance. If this stack frame is computing F(n) it must create and push
another two stack frame instances onto the the call stack to compute F(n -
1) and F(n - 2); these stack frame instances are expected to invoke
receiveReturnValue
on this stack frame to return the values of
F(n - 1) and F(n - 2) to this stack frame. This stack frame stores the
returned values to ultimately compute the sum F(n) = F(n - 1) + F(n - 2)IllegalStateException
- if this stack frame has already received values for both F(n - 1)
and F(n - 2)retVal
- the value of F(n - 1) or F(n - 2) returned from another
FibonacciStackFrame
instancepublic void returnValueToCaller(BigInteger retVal, Stack<FibonacciStackFrame> callStack)
this.caller
)
of this stack frame if the creator was another FibonacciStackFrame
instance, and then pops this stack frame from the call stack.retVal
- the value of F(n) that is returned to another
FibonacciStackFrame
instancecallStack
- the call stackpublic BigInteger getSum()
public void execute(Stack<FibonacciStackFrame> callStack)
this
as the caller of the new stack frame
this
as the caller of the new stack frame
callStack
- public static Map<Integer,BigInteger> getCache()
public int getN()
public FibonacciStackFrame getCaller()
public BigInteger getfMinus1()
public BigInteger getfMinus2()