Skip to content

Commit f7825f6

Browse files
author
tigraboris
committed
changes fixing mistakes
1 parent 94fdefe commit f7825f6

File tree

6 files changed

+34
-44
lines changed

6 files changed

+34
-44
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@
147147
<module>event-sourcing</module>
148148
<module>data-transfer-object</module>
149149
<module>throttling</module>
150-
151150
<module>unit-of-work</module>
152151
<module>partial-response</module>
153152
<module>eip-wire-tap</module>
154153
<module>eip-splitter</module>
155154
<module>eip-aggregator</module>
156155
<module>retry</module>
156+
<module>trampoline</module>
157157
</modules>
158158

159159
<repositories>

trampoline/README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ tags:
1212
---
1313

1414
## Intent
15-
By representing a computation in one of 2 states
15+
Trampoline pattern is used for implementing algorithms recursively in Java without blowing the stack
16+
and to interleave the execution of functions without hard coding them together
17+
It is possible by representing a computation in one of 2 states : done | more
1618
(completed with result, or a reference to the reminder of the computation,
17-
something like the way a java.util.Supplier does)
18-
it is possible to implement algorithms recursively in Java without blowing the stack
19-
and to interleave the execution of functions without hard coding them together or even using threads.
19+
something like the way a java.util.Supplier does).
2020

2121

22+
## Explanation
23+
Trampoline pattern allows to define recursive algorithms by iterative loop.
24+
2225

2326
## Applicability
2427
Use the Trampoline pattern when
@@ -32,9 +35,11 @@ The time overhead of a reflection call is traded for the space overhead of an in
3235
Trampolines in Java usually involve the creation of a GenericListener to pass events to an outer class.
3336

3437

35-
## Credits
36-
38+
## Tutorials
3739
* [Trampolining: a practical guide for awesome Java Developers](https://medium.com/@johnmcclean/trampolining-a-practical-guide-for-awesome-java-developers-4b657d9c3076)
3840
* [Trampoline in java ](http://mindprod.com/jgloss/trampoline.html)
39-
* [cyclops-react](https://github.com/aol/cyclops-react)
41+
42+
## Credits
43+
* [library 'cyclops-react' uses the pattern](https://github.com/aol/cyclops-react)
44+
4045

trampoline/pom.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,16 @@
3434
</parent>
3535
<artifactId>trampoline</artifactId>
3636
<dependencies>
37-
<!-- https://mvnrepository.com/artifact/junit/junit -->
3837
<dependency>
3938
<groupId>junit</groupId>
4039
<artifactId>junit</artifactId>
4140
<version>4.12</version>
4241
<scope>test</scope>
4342
</dependency>
44-
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
4543
<dependency>
4644
<groupId>org.projectlombok</groupId>
4745
<artifactId>lombok</artifactId>
4846
<version>1.16.18</version>
49-
<scope>provided</scope>
5047
</dependency>
5148

5249

trampoline/src/main/java/com/iluwatar/trampoline/Trampoline.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import java.util.stream.Stream;
44

5-
/**When get is called on the returned Trampoline, internally it will iterate calling ‘jump’
6-
on the returned Trampoline as long as the concrete instance returned is More,
7-
stopping once the returned instance is Done. Essential we convert looping via recursion into iteration,
8-
the key enabling mechanism is the fact that Trampoline.more is a lazy operation.
9-
Trampoline in cyclops-react extends java.util.Supplier. Calling Trampoline.more we are basically creating
10-
a Supplier that defers the actual recursive call, and having defered the call we can move it outside of the recursive loop.
11-
This means we can define algorithms recursively in Java but execute them iteratively.*/
5+
/**<p>Trampoline pattern allows to define recursive algorithms by iterative loop </p>
6+
* <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+
*/
1212

1313
public interface Trampoline<T> {
1414
T get();
Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/**
22
* The MIT License
33
* Copyright (c) 2014-2016 Ilkka Seppälä
4-
*
4+
* <p>
55
* Permission is hereby granted, free of charge, to any person obtaining a copy
66
* of this software and associated documentation files (the "Software"), to deal
77
* in the Software without restriction, including without limitation the rights
88
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
99
* copies of the Software, and to permit persons to whom the Software is
1010
* furnished to do so, subject to the following conditions:
11-
*
11+
* <p>
1212
* The above copyright notice and this permission notice shall be included in
1313
* all copies or substantial portions of the Software.
14-
*
14+
* <p>
1515
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1616
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1717
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -27,41 +27,28 @@
2727
import lombok.extern.slf4j.Slf4j;
2828

2929
/**
30-
* <div>
31-
* By representing a computation in one of 2 states
32-
(completed with result, or a reference to the reminder of the computation,
33-
something like the way a java.util.Supplier does)
34-
it is possible to implement algorithms recursively in Java without blowing the stack
35-
and to interleave the execution of functions without hard coding them together or even using threads.
36-
</div>
37-
<div>
38-
Trampoline has 2 state : [done], [ more]
39-
</div>
40-
When get is called on the returned Trampoline, internally it will iterate calling ‘jump’
41-
on the returned Trampoline as long as the concrete instance returned is More,
42-
stopping once the returned instance is Done. Essential we convert looping via recursion into iteration,
43-
the key enabling mechanism is the fact that Trampoline.more is a lazy operation.
44-
Trampoline in cyclops-react extends java.util.Supplier. Calling Trampoline.more we are basically creating
45-
a Supplier that defers the actual recursive call, and having defered the call we can move it outside of the recursive loop.
46-
This means we can define algorithms recursively in Java but execute them iteratively.
30+
* <p>Trampoline pattern allows to define recursive algorithms by iterative loop </p>
31+
* <p>it is possible to implement algorithms recursively in Java without blowing the stack
32+
* and to interleave the execution of functions without hard coding them together or even using threads.</p>
4733
*/
4834

4935
@Slf4j
5036
public class TrampolineApp {
5137
public static void main(String[] args) {
5238
log.info("start pattern");
5339
Integer result = loop(10, 1).result();
54-
log.info("result {}" ,result);
40+
log.info("result {}", result);
5541

5642
}
43+
5744
/**
58-
* Manager for pattern.
45+
* Manager for pattern. Define it with a factorial function.
5946
* */
60-
public static Trampoline<Integer> loop(int times,int prod){
61-
if(times==0)
47+
public static Trampoline<Integer> loop(int times, int prod) {
48+
if (times == 0)
6249
return Trampoline.done(prod);
6350
else
64-
return Trampoline.more(()->loop(times-1,prod*times));
51+
return Trampoline.more(() -> loop(times - 1, prod * times));
6552
}
6653

6754
}

trampoline/src/test/java/com/iluwatar/trampoline/TrampolineAppTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
public class TrampolineAppTest {
1010

11+
1112
@Test
12-
public void test()throws IOException{
13+
public void testTrampolineWithFactorialFunction()throws IOException{
1314
int result = TrampolineApp.loop(10, 1).result();
1415
assertEquals("Be equal",3628800,result);
1516
}

0 commit comments

Comments
 (0)