mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 10:58:42 +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:
@@ -35,13 +35,16 @@
|
||||
<artifactId>function-composition</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -27,9 +27,7 @@ package com.iluwatar.function.composition;
|
||||
import java.util.function.Function;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Main application class to demonstrate the use of function composition.
|
||||
*/
|
||||
/** Main application class to demonstrate the use of function composition. */
|
||||
public class App {
|
||||
|
||||
/**
|
||||
@@ -42,7 +40,8 @@ public class App {
|
||||
Function<Integer, Integer> timesTwo = x -> x * 2;
|
||||
Function<Integer, Integer> square = x -> x * x;
|
||||
|
||||
Function<Integer, Integer> composedFunction = FunctionComposer.composeFunctions(timesTwo, square);
|
||||
Function<Integer, Integer> composedFunction =
|
||||
FunctionComposer.composeFunctions(timesTwo, square);
|
||||
|
||||
int result = composedFunction.apply(3);
|
||||
logger.info("Result of composing 'timesTwo' and 'square' functions applied to 3 is: " + result);
|
||||
|
||||
+6
-5
@@ -27,20 +27,21 @@ package com.iluwatar.function.composition;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Class for composing functions using the Function Composition pattern.
|
||||
* Provides a static method to compose two functions using the 'andThen' method.
|
||||
* Class for composing functions using the Function Composition pattern. Provides a static method to
|
||||
* compose two functions using the 'andThen' method.
|
||||
*/
|
||||
public class FunctionComposer {
|
||||
|
||||
/**
|
||||
* Composes two functions where the output of the first function becomes
|
||||
* the input of the second function.
|
||||
* Composes two functions where the output of the first function becomes the input of the second
|
||||
* function.
|
||||
*
|
||||
* @param f1 the first function to apply
|
||||
* @param f2 the second function to apply after the first
|
||||
* @return a composed function that applies f1 and then f2
|
||||
*/
|
||||
public static Function<Integer, Integer> composeFunctions(Function<Integer, Integer> f1, Function<Integer, Integer> f2) {
|
||||
public static Function<Integer, Integer> composeFunctions(
|
||||
Function<Integer, Integer> f1, Function<Integer, Integer> f2) {
|
||||
return f1.andThen(f2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,17 +24,15 @@
|
||||
*/
|
||||
package com.iluwatar.function.composition;
|
||||
|
||||
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
-34
@@ -24,79 +24,73 @@
|
||||
*/
|
||||
package com.iluwatar.function.composition;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Test;
|
||||
import java.util.function.Function;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* Test class for FunctionComposer.
|
||||
*/
|
||||
import java.util.function.Function;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Test class for FunctionComposer. */
|
||||
public class FunctionComposerTest {
|
||||
|
||||
/**
|
||||
* Tests the composition of two functions.
|
||||
*/
|
||||
/** Tests the composition of two functions. */
|
||||
@Test
|
||||
public void testComposeFunctions() {
|
||||
void testComposeFunctions() {
|
||||
Function<Integer, Integer> timesTwo = x -> x * 2;
|
||||
Function<Integer, Integer> square = x -> x * x;
|
||||
|
||||
Function<Integer, Integer> composed = FunctionComposer.composeFunctions(timesTwo, square);
|
||||
|
||||
assertEquals("Expected output of composed functions is 36", 36, (int) composed.apply(3));
|
||||
assertEquals(36, composed.apply(3), "Expected output of composed functions is 36");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests function composition with identity function.
|
||||
*/
|
||||
/** Tests function composition with identity function. */
|
||||
@Test
|
||||
public void testComposeWithIdentity() {
|
||||
void testComposeWithIdentity() {
|
||||
Function<Integer, Integer> identity = Function.identity();
|
||||
Function<Integer, Integer> timesThree = x -> x * 3;
|
||||
|
||||
Function<Integer, Integer> composedLeft = FunctionComposer.composeFunctions(identity, timesThree);
|
||||
Function<Integer, Integer> composedRight = FunctionComposer.composeFunctions(timesThree, identity);
|
||||
Function<Integer, Integer> composedLeft =
|
||||
FunctionComposer.composeFunctions(identity, timesThree);
|
||||
Function<Integer, Integer> composedRight =
|
||||
FunctionComposer.composeFunctions(timesThree, identity);
|
||||
|
||||
assertEquals("Composition with identity on the left should be the same", 9, (int) composedLeft.apply(3));
|
||||
assertEquals("Composition with identity on the right should be the same", 9, (int) composedRight.apply(3));
|
||||
assertEquals(
|
||||
9, composedLeft.apply(3), "Composition with identity on the left should be the same");
|
||||
assertEquals(
|
||||
9, composedRight.apply(3), "Composition with identity on the right should be the same");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests function composition resulting in zero.
|
||||
*/
|
||||
/** Tests function composition resulting in zero. */
|
||||
@Test
|
||||
public void testComposeToZero() {
|
||||
void testComposeToZero() {
|
||||
Function<Integer, Integer> multiply = x -> x * 10;
|
||||
Function<Integer, Integer> toZero = x -> 0;
|
||||
|
||||
Function<Integer, Integer> composed = FunctionComposer.composeFunctions(multiply, toZero);
|
||||
|
||||
assertEquals("Expected output of function composition leading to zero is 0", 0, (int) composed.apply(5));
|
||||
assertEquals(
|
||||
0, composed.apply(5), "Expected output of function composition leading to zero is 0");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the composition with a negative function.
|
||||
*/
|
||||
/** Tests the composition with a negative function. */
|
||||
@Test
|
||||
public void testComposeNegative() {
|
||||
void testComposeNegative() {
|
||||
Function<Integer, Integer> negate = x -> -x;
|
||||
Function<Integer, Integer> square = x -> x * x;
|
||||
|
||||
Function<Integer, Integer> composed = FunctionComposer.composeFunctions(negate, square);
|
||||
|
||||
assertEquals("Expected square of negative number to be positive", 9, (int) composed.apply(3));
|
||||
assertEquals(9, composed.apply(3), "Expected square of negative number to be positive");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the composition of functions that cancel each other out.
|
||||
*/
|
||||
/** Tests the composition of functions that cancel each other out. */
|
||||
@Test
|
||||
public void testComposeInverseFunctions() {
|
||||
void testComposeInverseFunctions() {
|
||||
Function<Integer, Integer> timesTwo = x -> x * 2;
|
||||
Function<Integer, Integer> half = x -> x / 2;
|
||||
|
||||
Function<Integer, Integer> composed = FunctionComposer.composeFunctions(timesTwo, half);
|
||||
|
||||
assertEquals("Expect the functions to cancel each other out", 5, (int) composed.apply(5));
|
||||
assertEquals(5, composed.apply(5), "Expect the functions to cancel each other out");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user