Skip to content

Commit 4c4cbd4

Browse files
author
tigraboris
committed
fix checkstyle errors
1 parent f7825f6 commit 4c4cbd4

File tree

3 files changed

+105
-96
lines changed

3 files changed

+105
-96
lines changed

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

Lines changed: 77 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,84 @@
22

33
import java.util.stream.Stream;
44

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>
67
* <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+
}
8283

8384

8485
}

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,28 @@
3131
* <p>it is possible to implement algorithms recursively in Java without blowing the stack
3232
* and to interleave the execution of functions without hard coding them together or even using threads.</p>
3333
*/
34-
3534
@Slf4j
3635
public class TrampolineApp {
37-
public static void main(String[] args) {
38-
log.info("start pattern");
39-
Integer result = loop(10, 1).result();
40-
log.info("result {}", result);
41-
42-
}
4336

44-
/**
45-
* Manager for pattern. Define it with a factorial function.
46-
* */
47-
public static Trampoline<Integer> loop(int times, int prod) {
48-
if (times == 0)
49-
return Trampoline.done(prod);
50-
else
51-
return Trampoline.more(() -> loop(times - 1, prod * times));
37+
/**
38+
* Main program for showing pattern. It does loop with factorial function.
39+
* */
40+
public static void main(String[] args) {
41+
log.info("start pattern");
42+
Integer result = loop(10, 1).result();
43+
log.info("result {}", result);
44+
45+
}
46+
47+
/**
48+
* Manager for pattern. Define it with a factorial function.
49+
*/
50+
public static Trampoline<Integer> loop(int times, int prod) {
51+
if (times == 0) {
52+
return Trampoline.done(prod);
53+
} else {
54+
return Trampoline.more(() -> loop(times - 1, prod * times));
5255
}
56+
}
5357

5458
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66

77
import static org.junit.Assert.*;
88

9+
10+
/**
11+
* Test for trampoline pattern.
12+
* */
913
public class TrampolineAppTest {
1014

1115

12-
@Test
13-
public void testTrampolineWithFactorialFunction()throws IOException{
14-
int result = TrampolineApp.loop(10, 1).result();
15-
assertEquals("Be equal",3628800,result);
16-
}
16+
@Test
17+
public void testTrampolineWithFactorialFunction() throws IOException {
18+
int result = TrampolineApp.loop(10, 1).result();
19+
assertEquals("Be equal", 3628800, result);
20+
}
1721

1822
}

0 commit comments

Comments
 (0)