fix checkstyle errors
This commit is contained in:
parent
f7825f6c99
commit
4c4cbd41cf
@ -2,14 +2,16 @@ package com.iluwatar.trampoline;
|
|||||||
|
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
/**<p>Trampoline pattern allows to define recursive algorithms by iterative loop </p>
|
/**
|
||||||
|
* <p>Trampoline pattern allows to define recursive algorithms by iterative loop </p>
|
||||||
* <p>When get is called on the returned Trampoline, internally it will iterate calling ‘jump’
|
* <p>When get is called on the returned Trampoline, internally it will iterate calling ‘jump’
|
||||||
on the returned Trampoline as long as the concrete instance returned is {@link #more(Trampoline)},
|
* on the returned Trampoline as long as the concrete instance returned is {@link #more(Trampoline)},
|
||||||
stopping once the returned instance is {@link #done(Object)}.</p>
|
* stopping once the returned instance is {@link #done(Object)}.</p>
|
||||||
<p>Essential we convert looping via recursion into iteration,
|
* <p>Essential we convert looping via recursion into iteration,
|
||||||
the key enabling mechanism is the fact that {@link #more(Trampoline)} is a lazy operation.</p>
|
* the key enabling mechanism is the fact that {@link #more(Trampoline)} is a lazy operation.</p>
|
||||||
|
*
|
||||||
|
* @param <T> is type for returning result.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public interface Trampoline<T> {
|
public interface Trampoline<T> {
|
||||||
T get();
|
T get();
|
||||||
|
|
||||||
@ -28,7 +30,6 @@ public interface Trampoline<T> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @return true if complete
|
* @return true if complete
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
default boolean complete() {
|
default boolean complete() {
|
||||||
return true;
|
return true;
|
||||||
|
@ -31,9 +31,12 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
* <p>it is possible to implement algorithms recursively in Java without blowing the stack
|
* <p>it is possible to implement algorithms recursively in Java without blowing the stack
|
||||||
* and to interleave the execution of functions without hard coding them together or even using threads.</p>
|
* and to interleave the execution of functions without hard coding them together or even using threads.</p>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class TrampolineApp {
|
public class TrampolineApp {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main program for showing pattern. It does loop with factorial function.
|
||||||
|
* */
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
log.info("start pattern");
|
log.info("start pattern");
|
||||||
Integer result = loop(10, 1).result();
|
Integer result = loop(10, 1).result();
|
||||||
@ -43,12 +46,13 @@ public class TrampolineApp {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Manager for pattern. Define it with a factorial function.
|
* Manager for pattern. Define it with a factorial function.
|
||||||
* */
|
*/
|
||||||
public static Trampoline<Integer> loop(int times, int prod) {
|
public static Trampoline<Integer> loop(int times, int prod) {
|
||||||
if (times == 0)
|
if (times == 0) {
|
||||||
return Trampoline.done(prod);
|
return Trampoline.done(prod);
|
||||||
else
|
} else {
|
||||||
return Trampoline.more(() -> loop(times - 1, prod * times));
|
return Trampoline.more(() -> loop(times - 1, prod * times));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,10 @@ import java.io.IOException;
|
|||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for trampoline pattern.
|
||||||
|
* */
|
||||||
public class TrampolineAppTest {
|
public class TrampolineAppTest {
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user