package lab.util; import java.math.BigInteger; import java.util.HashMap; import java.util.Map; public class FibonacciStackFrame { private static Map cache = new HashMap(); private int n; private FibonacciStackFrame caller; private BigInteger fMinus1; private BigInteger fMinus2; private BigInteger sum; /** * Creates a stack frame to compute F(n). 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. * * * @param n * the Fibonccci number to compute * @param caller * the FibonacciStackFrame that created this stack frame * @throws IllegalArgumentException * if n < 0 */ public FibonacciStackFrame(int n, FibonacciStackFrame caller) { } /** * Receive a return value from another 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) * * @param retVal * the value of F(n - 1) or F(n - 2) returned from another * FibonacciStackFrame instance * @throws IllegalStateException * if this stack frame has already received values for both F(n - 1) * and F(n - 2) */ public void receiveReturnValue(BigInteger retVal) { } /** * 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. * * @param retVal * the value of F(n) that is returned to another * FibonacciStackFrame instance * @param callStack * the call stack */ public void returnValueToCaller(BigInteger retVal, Stack callStack) { } /** * Get the current value of the sum F(n) = F(n - 1) + F(n - 2). Note that the * final correct value of F(n) will not be returned by this method until this * stack frame has actually completed computing the sum F(n - 1) + F(n - 2) * * @return the current value of the sum F(n) = F(n - 1) + F(n - 2); the * correct value of F(n) will not be returned by this method until * this stack frame is actually finished computing the sum F(n - 1) + * F(n - 2) */ public BigInteger getSum() { } /** * Start or resume execution of this stack frame. This method is expected to * be invoked when this stack frame is on top of the call stack. When this * method is invoked, this stack frame can be in one of six possible states: * *
    *
  1. this stack frame is computing F(0), in which case the value 0 can be * returned to the caller *
  2. this stack frame is computing F(1), in which case the value 1 can be * returned to the caller *
  3. this stack frame is computing F(n) and F(n) is in the cache, in which * case the value F(n) can be retrieved from the cache and returned to the * caller *
  4. this stack frame is computing F(n - 1), in which case this stack frame * should push a new stack frame onto the call stack to compute F(n - 1) passing * this as the caller of the new stack frame *
  5. this stack frame is computing F(n - 2), in which case this stack frame * should push a new stack frame onto the call stack to compute F(n - 2) passing * this as the caller of the new stack frame *
  6. this stack frame is computing the sum F(n - 1) + F(n - 2), in which * case this stack frame should compute the sum, put the sum in the cache, and * return the value of the sum to the caller * * @param callStack */ public void execute(Stack callStack) { } /** * Get the cache of already computed Fibonacci numbers. * This method is here for * debugging and testing purposes. * * @return the cache of already computed Fibonacci numbers */ public static Map getCache() { return cache; } /** * Get the value of n. This method is here for * debugging and testing purposes. * * @return the value of n */ public int getN() { return n; } /** * Get the creator of this call stack. This method is here for * debugging and testing purposes. * * @return the creator of this call stack */ public FibonacciStackFrame getCaller() { return caller; } /** * Get the current value of fMinus1. This method is here for * debugging and testing purposes. * * @return the current value of fMinus1 */ public BigInteger getfMinus1() { return fMinus1; } /** * Get the current value of fMinus2. This method is here for * debugging and testing purposes. * * @return the current value of fMinus2 */ public BigInteger getfMinus2() { return fMinus2; } }