Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4c4cbd4

Browse files
author
tigraboris
committed
fix checkstyle errors
1 parentf7825f6 commit4c4cbd4

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
importjava.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-
publicinterfaceTrampoline<T> {
14-
Tget();
15-
16-
17-
/**
18-
* @return next stage
19-
*/
20-
defaultTrampoline<T>jump() {
21-
returnthis;
22-
}
23-
24-
25-
defaultTresult() {
26-
returnget();
27-
}
28-
29-
/**
30-
* @return true if complete
31-
*
32-
*/
33-
defaultbooleancomplete() {
34-
returntrue;
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(finalTresult) {
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(finalTrampoline<Trampoline<T>>trampoline) {
55-
returnnewTrampoline<T>() {
56-
@Override
57-
publicbooleancomplete() {
58-
returnfalse;
59-
}
60-
61-
@Override
62-
publicTrampoline<T>jump() {
63-
returntrampoline.result();
64-
}
65-
66-
@Override
67-
publicTget() {
68-
returntrampoline(this);
69-
}
70-
71-
Ttrampoline(finalTrampoline<T>trampoline) {
72-
73-
returnStream.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+
publicinterfaceTrampoline<T> {
16+
Tget();
17+
18+
19+
/**
20+
* @return next stage
21+
*/
22+
defaultTrampoline<T>jump() {
23+
returnthis;
24+
}
25+
26+
27+
defaultTresult() {
28+
returnget();
29+
}
30+
31+
/**
32+
* @return true if complete
33+
*/
34+
defaultbooleancomplete() {
35+
returntrue;
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(finalTresult) {
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(finalTrampoline<Trampoline<T>>trampoline) {
56+
returnnewTrampoline<T>() {
57+
@Override
58+
publicbooleancomplete() {
59+
returnfalse;
60+
}
61+
62+
@Override
63+
publicTrampoline<T>jump() {
64+
returntrampoline.result();
65+
}
66+
67+
@Override
68+
publicTget() {
69+
returntrampoline(this);
70+
}
71+
72+
Ttrampoline(finalTrampoline<T>trampoline) {
73+
74+
returnStream.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
publicclassTrampolineApp {
37-
publicstaticvoidmain(String[]args) {
38-
log.info("start pattern");
39-
Integerresult =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-
publicstaticTrampoline<Integer>loop(inttimes,intprod) {
48-
if (times ==0)
49-
returnTrampoline.done(prod);
50-
else
51-
returnTrampoline.more(() ->loop(times -1,prod *times));
37+
/**
38+
* Main program for showing pattern. It does loop with factorial function.
39+
* */
40+
publicstaticvoidmain(String[]args) {
41+
log.info("start pattern");
42+
Integerresult =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+
publicstaticTrampoline<Integer>loop(inttimes,intprod) {
51+
if (times ==0) {
52+
returnTrampoline.done(prod);
53+
}else {
54+
returnTrampoline.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
importstaticorg.junit.Assert.*;
88

9+
10+
/**
11+
* Test for trampoline pattern.
12+
* */
913
publicclassTrampolineAppTest {
1014

1115

12-
@Test
13-
publicvoidtestTrampolineWithFactorialFunction()throwsIOException{
14-
intresult =TrampolineApp.loop(10,1).result();
15-
assertEquals("Be equal",3628800,result);
16-
}
16+
@Test
17+
publicvoidtestTrampolineWithFactorialFunction()throwsIOException{
18+
intresult =TrampolineApp.loop(10,1).result();
19+
assertEquals("Be equal",3628800,result);
20+
}
1721

1822
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp