|
2 | 2 |
|
3 | 3 | import java.util.stream.Stream;
|
4 | 4 |
|
5 |
| -/**<p>Trampoline pattern allows to define recursive algorithms by iterative loop </p> |
| 5 | +/** |
| 6 | + * <p>Trampoline pattern allows to define recursive algorithms by iterative loop </p> |
6 | 7 | * <p>When get is called on the returned Trampoline, internally it will iterate calling ‘jump’
|
7 |
| - on the returned Trampoline as long as the concrete instance returned is {@link #more(Trampoline)}, |
8 |
| - stopping once the returned instance is {@link #done(Object)}.</p> |
9 |
| - <p>Essential we convert looping via recursion into iteration, |
10 |
| - the key enabling mechanism is the fact that {@link #more(Trampoline)} is a lazy operation.</p> |
11 |
| -*/ |
12 |
| - |
13 |
| -public interface Trampoline<T> { |
14 |
| - T get(); |
15 |
| - |
16 |
| - |
17 |
| - /** |
18 |
| - * @return next stage |
19 |
| - */ |
20 |
| - default Trampoline<T> jump() { |
21 |
| - return this; |
22 |
| - } |
23 |
| - |
24 |
| - |
25 |
| - default T result() { |
26 |
| - return get(); |
27 |
| - } |
28 |
| - |
29 |
| - /** |
30 |
| - * @return true if complete |
31 |
| - * |
32 |
| - */ |
33 |
| - default boolean complete() { |
34 |
| - return true; |
35 |
| - } |
36 |
| - |
37 |
| - /** |
38 |
| - * Created a completed Trampoline |
39 |
| - * |
40 |
| - * @param result Completed result |
41 |
| - * @return Completed Trampoline |
42 |
| - */ |
43 |
| - static <T> Trampoline<T> done(final T result) { |
44 |
| - return () -> result; |
45 |
| - } |
46 |
| - |
47 |
| - |
48 |
| - /** |
49 |
| - * Create a Trampoline that has more work to do |
50 |
| - * |
51 |
| - * @param trampoline Next stage in Trampoline |
52 |
| - * @return Trampoline with more work |
53 |
| - */ |
54 |
| - static <T> Trampoline<T> more(final Trampoline<Trampoline<T>> trampoline) { |
55 |
| - return new Trampoline<T>() { |
56 |
| - @Override |
57 |
| - public boolean complete() { |
58 |
| - return false; |
59 |
| - } |
60 |
| - |
61 |
| - @Override |
62 |
| - public Trampoline<T> jump() { |
63 |
| - return trampoline.result(); |
64 |
| - } |
65 |
| - |
66 |
| - @Override |
67 |
| - public T get() { |
68 |
| - return trampoline(this); |
69 |
| - } |
70 |
| - |
71 |
| - T trampoline(final Trampoline<T> trampoline) { |
72 |
| - |
73 |
| - return Stream.iterate(trampoline, Trampoline::jump) |
74 |
| - .filter(Trampoline::complete) |
75 |
| - .findFirst() |
76 |
| - .get() |
77 |
| - .result(); |
78 |
| - |
79 |
| - } |
80 |
| - }; |
81 |
| - } |
| 8 | + * on the returned Trampoline as long as the concrete instance returned is {@link #more(Trampoline)}, |
| 9 | + * stopping once the returned instance is {@link #done(Object)}.</p> |
| 10 | + * <p>Essential we convert looping via recursion into iteration, |
| 11 | + * the key enabling mechanism is the fact that {@link #more(Trampoline)} is a lazy operation.</p> |
| 12 | + * |
| 13 | + * @param <T> is type for returning result. |
| 14 | + */ |
| 15 | +public interface Trampoline<T> { |
| 16 | + T get(); |
| 17 | + |
| 18 | + |
| 19 | + /** |
| 20 | + * @return next stage |
| 21 | + */ |
| 22 | + default Trampoline<T> jump() { |
| 23 | + return this; |
| 24 | + } |
| 25 | + |
| 26 | + |
| 27 | + default T result() { |
| 28 | + return get(); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @return true if complete |
| 33 | + */ |
| 34 | + default boolean complete() { |
| 35 | + return true; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Created a completed Trampoline |
| 40 | + * |
| 41 | + * @param result Completed result |
| 42 | + * @return Completed Trampoline |
| 43 | + */ |
| 44 | + static <T> Trampoline<T> done(final T result) { |
| 45 | + return () -> result; |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + /** |
| 50 | + * Create a Trampoline that has more work to do |
| 51 | + * |
| 52 | + * @param trampoline Next stage in Trampoline |
| 53 | + * @return Trampoline with more work |
| 54 | + */ |
| 55 | + static <T> Trampoline<T> more(final Trampoline<Trampoline<T>> trampoline) { |
| 56 | + return new Trampoline<T>() { |
| 57 | + @Override |
| 58 | + public boolean complete() { |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public Trampoline<T> jump() { |
| 64 | + return trampoline.result(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public T get() { |
| 69 | + return trampoline(this); |
| 70 | + } |
| 71 | + |
| 72 | + T trampoline(final Trampoline<T> trampoline) { |
| 73 | + |
| 74 | + return Stream.iterate(trampoline, Trampoline::jump) |
| 75 | + .filter(Trampoline::complete) |
| 76 | + .findFirst() |
| 77 | + .get() |
| 78 | + .result(); |
| 79 | + |
| 80 | + } |
| 81 | + }; |
| 82 | + } |
82 | 83 |
|
83 | 84 |
|
84 | 85 | }
|
0 commit comments