joshuataylor

Trampoline Example for http://stackoverflow.com/a/39350385/1

Sep 8th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 KB | None | 0 0
  1. /* See http://stackoverflow.com/a/39350385/1281433 */
  2.  
  3. public class Trampoline {
  4.     /**
  5.      * State of a computation for a trampoline.
  6.      *
  7.      * @param <T> the type of value
  8.      */
  9.     public interface TrampolineState<T> {
  10.         /**
  11.          * Returns whether the state is a finished state.
  12.          *
  13.          * @return whether the state is a finshed state
  14.          */
  15.         boolean isFinished();
  16.  
  17.         /**
  18.          * Returns the value, if this state is finished.
  19.          *
  20.          * @return the value
  21.          * @throws IllegalStateException if the state is not finished
  22.          */
  23.         T getValue() throws IllegalStateException;
  24.  
  25.         /**
  26.          * Returns the next state, if this state is not finished.
  27.          *
  28.          * @return the next state
  29.          * @throws IllegalStateException if the state is finished
  30.          */
  31.         TrampolineState<T> getNext() throws IllegalStateException;
  32.     }
  33.  
  34.     /**
  35.      * Executes a trampolined state and its successors until a finished state is
  36.      * reached, and then returns the value of the finished state.
  37.      *
  38.      * @param state the state
  39.      * @return the value
  40.      */
  41.     public <T> T trampoline(TrampolineState<T> state) {
  42.         while (!state.isFinished()) {
  43.             state = state.getNext();
  44.         }
  45.         return state.getValue();
  46.     }
  47.  
  48.     /**
  49.      * Returns the state for for sum computation.
  50.      *
  51.      * @param x the augend
  52.      * @param y the addend
  53.      * @return the state
  54.      */
  55.     private TrampolineState<Integer> getSumTrampolineState(int x, int y) {
  56.         return new TrampolineState<Integer>() {
  57.             @Override
  58.             public boolean isFinished() {
  59.                 return x == 0;
  60.             }
  61.  
  62.             @Override
  63.             public Integer getValue() {
  64.                 if (!isFinished()) {
  65.                     throw new IllegalStateException();
  66.                 }
  67.                 return y;
  68.             }
  69.  
  70.             @Override
  71.             public TrampolineState<Integer> getNext() {
  72.                 if (isFinished()) {
  73.                     throw new IllegalStateException();
  74.                 }
  75.                 return getSumTrampolineState(x - 1, y + 1);
  76.             }
  77.         };
  78.     }
  79.  
  80.     /**
  81.      * Returns a sum, computed by a trampolined computation.
  82.      *
  83.      * @param x the augend
  84.      * @param y the addend
  85.      * @return the sum
  86.      */
  87.     public int sum(int x, int y) {
  88.         return trampoline(getSumTrampolineState(x, y));
  89.     }
  90. }
Add Comment
Please, Sign In to add comment