mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-08-23 16:24:05 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -29,19 +29,18 @@ import java.util.stream.Stream;
|
||||
/**
|
||||
* Trampoline pattern allows to define recursive algorithms by iterative loop.
|
||||
*
|
||||
* <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)}, stopping once the returned instance is {@link #done(Object)}.
|
||||
* <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)},
|
||||
* stopping once the returned instance is {@link #done(Object)}.
|
||||
*
|
||||
* <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>Essential we convert looping via recursion into iteration, the key enabling mechanism is the
|
||||
* fact that {@link #more(Trampoline)} is a lazy operation.
|
||||
*
|
||||
* @param <T> is type for returning result.
|
||||
* @param <T> is type for returning result.
|
||||
*/
|
||||
public interface Trampoline<T> {
|
||||
T get();
|
||||
|
||||
|
||||
/**
|
||||
* Jump to next stage.
|
||||
*
|
||||
@@ -51,7 +50,6 @@ public interface Trampoline<T> {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
default T result() {
|
||||
return get();
|
||||
}
|
||||
@@ -75,7 +73,6 @@ public interface Trampoline<T> {
|
||||
return () -> result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a Trampoline that has more work to do.
|
||||
*
|
||||
|
||||
@@ -29,26 +29,20 @@ import lombok.extern.slf4j.Slf4j;
|
||||
/**
|
||||
* Trampoline pattern allows to define recursive algorithms by iterative loop.
|
||||
*
|
||||
* <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>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 {
|
||||
|
||||
/**
|
||||
* Main program for showing pattern. It does loop with factorial function.
|
||||
*/
|
||||
/** Main program for showing pattern. It does loop with factorial function. */
|
||||
public static void main(String[] args) {
|
||||
LOGGER.info("Start calculating war casualties");
|
||||
var result = loop(10, 1).result();
|
||||
LOGGER.info("The number of orcs perished in the war: {}", result);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
if (times == 0) {
|
||||
return Trampoline.done(prod);
|
||||
|
||||
@@ -28,9 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Test for trampoline pattern.
|
||||
*/
|
||||
/** Test for trampoline pattern. */
|
||||
class TrampolineAppTest {
|
||||
|
||||
@Test
|
||||
@@ -38,5 +36,4 @@ class TrampolineAppTest {
|
||||
long result = TrampolineApp.loop(10, 1).result();
|
||||
assertEquals(3_628_800, result);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user