From 4c4cbd41cf8a1310304bd16a2b40acb18ee7a05d Mon Sep 17 00:00:00 2001 From: tigraboris Date: Sat, 20 Jan 2018 21:42:14 +0300 Subject: [PATCH] fix checkstyle errors --- .../com/iluwatar/trampoline/Trampoline.java | 127 +++++++++--------- .../iluwatar/trampoline/TrampolineApp.java | 32 +++-- .../trampoline/TrampolineAppTest.java | 14 +- 3 files changed, 91 insertions(+), 82 deletions(-) diff --git a/trampoline/src/main/java/com/iluwatar/trampoline/Trampoline.java b/trampoline/src/main/java/com/iluwatar/trampoline/Trampoline.java index 8a02e246b..277ae9ffb 100644 --- a/trampoline/src/main/java/com/iluwatar/trampoline/Trampoline.java +++ b/trampoline/src/main/java/com/iluwatar/trampoline/Trampoline.java @@ -2,83 +2,84 @@ package com.iluwatar.trampoline; import java.util.stream.Stream; -/**

Trampoline pattern allows to define recursive algorithms by iterative loop

+/** + *

Trampoline pattern allows to define recursive algorithms by iterative loop

*

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)}, - stopping once the returned instance is {@link #done(Object)}.

-

Essential we convert looping via recursion into iteration, - the key enabling mechanism is the fact that {@link #more(Trampoline)} is a lazy operation.

-*/ - -public interface Trampoline { - T get(); + * on the returned Trampoline as long as the concrete instance returned is {@link #more(Trampoline)}, + * stopping once the returned instance is {@link #done(Object)}.

+ *

Essential we convert looping via recursion into iteration, + * the key enabling mechanism is the fact that {@link #more(Trampoline)} is a lazy operation.

+ * + * @param is type for returning result. + */ +public interface Trampoline { + T get(); - /** - * @return next stage - */ - default Trampoline jump() { - return this; - } + /** + * @return next stage + */ + default Trampoline jump() { + return this; + } - default T result() { - return get(); - } + default T result() { + return get(); + } - /** - * @return true if complete - * - */ - default boolean complete() { - return true; - } + /** + * @return true if complete + */ + default boolean complete() { + return true; + } - /** - * Created a completed Trampoline - * - * @param result Completed result - * @return Completed Trampoline - */ - static Trampoline done(final T result) { - return () -> result; - } + /** + * Created a completed Trampoline + * + * @param result Completed result + * @return Completed Trampoline + */ + static Trampoline done(final T result) { + return () -> result; + } - /** - * Create a Trampoline that has more work to do - * - * @param trampoline Next stage in Trampoline - * @return Trampoline with more work - */ - static Trampoline more(final Trampoline> trampoline) { - return new Trampoline() { - @Override - public boolean complete() { - return false; - } + /** + * Create a Trampoline that has more work to do + * + * @param trampoline Next stage in Trampoline + * @return Trampoline with more work + */ + static Trampoline more(final Trampoline> trampoline) { + return new Trampoline() { + @Override + public boolean complete() { + return false; + } - @Override - public Trampoline jump() { - return trampoline.result(); - } + @Override + public Trampoline jump() { + return trampoline.result(); + } - @Override - public T get() { - return trampoline(this); - } + @Override + public T get() { + return trampoline(this); + } - T trampoline(final Trampoline trampoline) { + T trampoline(final Trampoline trampoline) { - return Stream.iterate(trampoline, Trampoline::jump) - .filter(Trampoline::complete) - .findFirst() - .get() - .result(); + return Stream.iterate(trampoline, Trampoline::jump) + .filter(Trampoline::complete) + .findFirst() + .get() + .result(); - } - }; - } + } + }; + } } diff --git a/trampoline/src/main/java/com/iluwatar/trampoline/TrampolineApp.java b/trampoline/src/main/java/com/iluwatar/trampoline/TrampolineApp.java index 73222ddf5..76e3ed3da 100644 --- a/trampoline/src/main/java/com/iluwatar/trampoline/TrampolineApp.java +++ b/trampoline/src/main/java/com/iluwatar/trampoline/TrampolineApp.java @@ -31,24 +31,28 @@ import lombok.extern.slf4j.Slf4j; *

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.

*/ - @Slf4j 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 loop(int times, int prod) { - if (times == 0) - return Trampoline.done(prod); - else - return Trampoline.more(() -> loop(times - 1, prod * times)); + } + + /** + * Manager for pattern. Define it with a factorial function. + */ + public static Trampoline loop(int times, int prod) { + if (times == 0) { + return Trampoline.done(prod); + } else { + return Trampoline.more(() -> loop(times - 1, prod * times)); } + } } diff --git a/trampoline/src/test/java/com/iluwatar/trampoline/TrampolineAppTest.java b/trampoline/src/test/java/com/iluwatar/trampoline/TrampolineAppTest.java index 9765131d5..553e583e1 100644 --- a/trampoline/src/test/java/com/iluwatar/trampoline/TrampolineAppTest.java +++ b/trampoline/src/test/java/com/iluwatar/trampoline/TrampolineAppTest.java @@ -6,13 +6,17 @@ import java.io.IOException; import static org.junit.Assert.*; + +/** + * Test for trampoline pattern. + * */ public class TrampolineAppTest { - @Test - public void testTrampolineWithFactorialFunction()throws IOException{ - int result = TrampolineApp.loop(10, 1).result(); - assertEquals("Be equal",3628800,result); - } + @Test + public void testTrampolineWithFactorialFunction() throws IOException { + int result = TrampolineApp.loop(10, 1).result(); + assertEquals("Be equal", 3628800, result); + } } \ No newline at end of file