fix checkstyle errors

This commit is contained in:
tigraboris 2018-01-20 21:42:14 +03:00
parent f7825f6c99
commit 4c4cbd41cf
3 changed files with 91 additions and 82 deletions

View File

@ -2,83 +2,84 @@ 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> { */
T get(); public interface Trampoline<T> {
T get();
/** /**
* @return next stage * @return next stage
*/ */
default Trampoline<T> jump() { default Trampoline<T> jump() {
return this; return this;
} }
default T result() { default T result() {
return get(); return get();
} }
/** /**
* @return true if complete * @return true if complete
* */
*/ default boolean complete() {
default boolean complete() { return true;
return true; }
}
/** /**
* Created a completed Trampoline * Created a completed Trampoline
* *
* @param result Completed result * @param result Completed result
* @return Completed Trampoline * @return Completed Trampoline
*/ */
static <T> Trampoline<T> done(final T result) { static <T> Trampoline<T> done(final T result) {
return () -> result; return () -> result;
} }
/** /**
* Create a Trampoline that has more work to do * Create a Trampoline that has more work to do
* *
* @param trampoline Next stage in Trampoline * @param trampoline Next stage in Trampoline
* @return Trampoline with more work * @return Trampoline with more work
*/ */
static <T> Trampoline<T> more(final Trampoline<Trampoline<T>> trampoline) { static <T> Trampoline<T> more(final Trampoline<Trampoline<T>> trampoline) {
return new Trampoline<T>() { return new Trampoline<T>() {
@Override @Override
public boolean complete() { public boolean complete() {
return false; return false;
} }
@Override @Override
public Trampoline<T> jump() { public Trampoline<T> jump() {
return trampoline.result(); return trampoline.result();
} }
@Override @Override
public T get() { public T get() {
return trampoline(this); return trampoline(this);
} }
T trampoline(final Trampoline<T> trampoline) { T trampoline(final Trampoline<T> trampoline) {
return Stream.iterate(trampoline, Trampoline::jump) return Stream.iterate(trampoline, Trampoline::jump)
.filter(Trampoline::complete) .filter(Trampoline::complete)
.findFirst() .findFirst()
.get() .get()
.result(); .result();
} }
}; };
} }
} }

View File

@ -31,24 +31,28 @@ 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 {
public static void main(String[] args) {
log.info("start pattern");
Integer result = loop(10, 1).result();
log.info("result {}", result);
} /**
* Main program for showing pattern. It does loop with factorial function.
* */
public static void main(String[] args) {
log.info("start pattern");
Integer result = loop(10, 1).result();
log.info("result {}", result);
/** }
* Manager for pattern. Define it with a factorial function.
* */ /**
public static Trampoline<Integer> loop(int times, int prod) { * Manager for pattern. Define it with a factorial function.
if (times == 0) */
return Trampoline.done(prod); public static Trampoline<Integer> loop(int times, int prod) {
else if (times == 0) {
return Trampoline.more(() -> loop(times - 1, prod * times)); return Trampoline.done(prod);
} else {
return Trampoline.more(() -> loop(times - 1, prod * times));
} }
}
} }

View File

@ -6,13 +6,17 @@ import java.io.IOException;
import static org.junit.Assert.*; import static org.junit.Assert.*;
/**
* Test for trampoline pattern.
* */
public class TrampolineAppTest { public class TrampolineAppTest {
@Test @Test
public void testTrampolineWithFactorialFunction()throws IOException{ public void testTrampolineWithFactorialFunction() throws IOException {
int result = TrampolineApp.loop(10, 1).result(); int result = TrampolineApp.loop(10, 1).result();
assertEquals("Be equal",3628800,result); assertEquals("Be equal", 3628800, result);
} }
} }