mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-09-03 04:18:19 +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:
@@ -45,28 +45,29 @@ public class App {
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
/*
|
||||
Suppose we wanted to pass through a String to a series of filtering stages and convert it
|
||||
as a char array on the last stage.
|
||||
Suppose we wanted to pass through a String to a series of filtering stages and convert it
|
||||
as a char array on the last stage.
|
||||
|
||||
- Stage handler 1 (pipe): Removing the alphabets, accepts a String input and returns the
|
||||
processed String output. This will be used by the next handler as its input.
|
||||
- Stage handler 1 (pipe): Removing the alphabets, accepts a String input and returns the
|
||||
processed String output. This will be used by the next handler as its input.
|
||||
|
||||
- Stage handler 2 (pipe): Removing the digits, accepts a String input and returns the
|
||||
processed String output. This shall also be used by the last handler we have.
|
||||
- Stage handler 2 (pipe): Removing the digits, accepts a String input and returns the
|
||||
processed String output. This shall also be used by the last handler we have.
|
||||
|
||||
- Stage handler 3 (pipe): Converting the String input to a char array handler. We would
|
||||
be returning a different type in here since that is what's specified by the requirement.
|
||||
This means that at any stages along the pipeline, the handler can return any type of data
|
||||
as long as it fulfills the requirements for the next handler's input.
|
||||
- Stage handler 3 (pipe): Converting the String input to a char array handler. We would
|
||||
be returning a different type in here since that is what's specified by the requirement.
|
||||
This means that at any stages along the pipeline, the handler can return any type of data
|
||||
as long as it fulfills the requirements for the next handler's input.
|
||||
|
||||
Suppose we wanted to add another handler after ConvertToCharArrayHandler. That handler
|
||||
then is expected to receive an input of char[] array since that is the type being returned
|
||||
by the previous handler, ConvertToCharArrayHandler.
|
||||
*/
|
||||
Suppose we wanted to add another handler after ConvertToCharArrayHandler. That handler
|
||||
then is expected to receive an input of char[] array since that is the type being returned
|
||||
by the previous handler, ConvertToCharArrayHandler.
|
||||
*/
|
||||
LOGGER.info("Creating pipeline");
|
||||
var filters = new Pipeline<>(new RemoveAlphabetsHandler())
|
||||
.addHandler(new RemoveDigitsHandler())
|
||||
.addHandler(new ConvertToCharArrayHandler());
|
||||
var filters =
|
||||
new Pipeline<>(new RemoveAlphabetsHandler())
|
||||
.addHandler(new RemoveDigitsHandler())
|
||||
.addHandler(new ConvertToCharArrayHandler());
|
||||
var input = "GoYankees123!";
|
||||
LOGGER.info("Executing pipeline with input: {}", input);
|
||||
var output = filters.execute(input);
|
||||
|
||||
@@ -28,9 +28,7 @@ import java.util.Arrays;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Stage handler that converts an input String to its char[] array counterpart.
|
||||
*/
|
||||
/** Stage handler that converts an input String to its char[] array counterpart. */
|
||||
class ConvertToCharArrayHandler implements Handler<String, char[]> {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ConvertToCharArrayHandler.class);
|
||||
@@ -40,9 +38,9 @@ class ConvertToCharArrayHandler implements Handler<String, char[]> {
|
||||
var characters = input.toCharArray();
|
||||
var string = Arrays.toString(characters);
|
||||
LOGGER.info(
|
||||
String.format("Current handler: %s, input is %s of type %s, output is %s, of type %s",
|
||||
ConvertToCharArrayHandler.class, input, String.class, string, Character[].class)
|
||||
);
|
||||
String.format(
|
||||
"Current handler: %s, input is %s of type %s, output is %s, of type %s",
|
||||
ConvertToCharArrayHandler.class, input, String.class, string, Character[].class));
|
||||
|
||||
return characters;
|
||||
}
|
||||
|
||||
@@ -33,4 +33,4 @@ package com.iluwatar.pipeline;
|
||||
*/
|
||||
interface Handler<I, O> {
|
||||
O process(I input);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,4 +46,4 @@ class Pipeline<I, O> {
|
||||
O execute(I input) {
|
||||
return currentHandler.process(input);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,8 @@ class RemoveAlphabetsHandler implements Handler<String, String> {
|
||||
public String process(String input) {
|
||||
var inputWithoutAlphabets = new StringBuilder();
|
||||
var isAlphabetic = (IntPredicate) Character::isAlphabetic;
|
||||
input.chars()
|
||||
input
|
||||
.chars()
|
||||
.filter(isAlphabetic.negate())
|
||||
.mapToObj(x -> (char) x)
|
||||
.forEachOrdered(inputWithoutAlphabets::append);
|
||||
@@ -49,11 +50,12 @@ class RemoveAlphabetsHandler implements Handler<String, String> {
|
||||
LOGGER.info(
|
||||
String.format(
|
||||
"Current handler: %s, input is %s of type %s, output is %s, of type %s",
|
||||
RemoveAlphabetsHandler.class, input,
|
||||
String.class, inputWithoutAlphabetsStr, String.class
|
||||
)
|
||||
);
|
||||
RemoveAlphabetsHandler.class,
|
||||
input,
|
||||
String.class,
|
||||
inputWithoutAlphabetsStr,
|
||||
String.class));
|
||||
|
||||
return inputWithoutAlphabetsStr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,8 @@ class RemoveDigitsHandler implements Handler<String, String> {
|
||||
public String process(String input) {
|
||||
var inputWithoutDigits = new StringBuilder();
|
||||
var isDigit = (IntPredicate) Character::isDigit;
|
||||
input.chars()
|
||||
input
|
||||
.chars()
|
||||
.filter(isDigit.negate())
|
||||
.mapToObj(x -> (char) x)
|
||||
.forEachOrdered(inputWithoutDigits::append);
|
||||
@@ -49,10 +50,8 @@ class RemoveDigitsHandler implements Handler<String, String> {
|
||||
LOGGER.info(
|
||||
String.format(
|
||||
"Current handler: %s, input is %s of type %s, output is %s, of type %s",
|
||||
RemoveDigitsHandler.class, input, String.class, inputWithoutDigitsStr, String.class
|
||||
)
|
||||
);
|
||||
RemoveDigitsHandler.class, input, String.class, inputWithoutDigitsStr, String.class));
|
||||
|
||||
return inputWithoutDigitsStr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,17 +24,15 @@
|
||||
*/
|
||||
package com.iluwatar.pipeline;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application Test
|
||||
*/
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Application Test */
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,20 +28,17 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Test for {@link Pipeline}
|
||||
*/
|
||||
/** Test for {@link Pipeline} */
|
||||
class PipelineTest {
|
||||
|
||||
@Test
|
||||
void testAddHandlersToPipeline() {
|
||||
var filters = new Pipeline<>(new RemoveAlphabetsHandler())
|
||||
.addHandler(new RemoveDigitsHandler())
|
||||
.addHandler(new ConvertToCharArrayHandler());
|
||||
var filters =
|
||||
new Pipeline<>(new RemoveAlphabetsHandler())
|
||||
.addHandler(new RemoveDigitsHandler())
|
||||
.addHandler(new ConvertToCharArrayHandler());
|
||||
|
||||
assertArrayEquals(
|
||||
new char[]{'#', '!', '(', '&', '%', '#', '!'},
|
||||
filters.execute("#H!E(L&L0O%THE3R#34E!")
|
||||
);
|
||||
new char[] {'#', '!', '(', '&', '%', '#', '!'}, filters.execute("#H!E(L&L0O%THE3R#34E!"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user