add new package
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
### Relevant Articles:
|
||||
- [Facade Design Pattern in Java](https://www.baeldung.com/java-facade-pattern)
|
||||
- [Proxy, Decorator, Adapter and Bridge Patterns](https://www.baeldung.com/java-structural-design-patterns)
|
||||
- [Composite Design Pattern in Java](https://www.baeldung.com/java-composite-pattern)
|
||||
- [The Decorator Pattern in Java](https://www.baeldung.com/java-decorator-pattern)
|
||||
- [The Adapter Pattern in Java](https://www.baeldung.com/java-adapter-pattern)
|
||||
- [The Proxy Pattern in Java](https://www.baeldung.com/java-proxy-pattern)
|
||||
- [The Bridge Pattern in Java](https://www.baeldung.com/java-bridge-pattern)
|
||||
- [Pipeline Design Pattern in Java](https://www.baeldung.com/java-pipeline-design-pattern)
|
||||
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>design-patterns-structural</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>design-patterns-structural</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>patterns-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.adapter;
|
||||
|
||||
import java.util.Enumeration;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class IteratorAdapter<E> implements Iterator<E> {
|
||||
|
||||
private Enumeration<E> enumeration;
|
||||
|
||||
public IteratorAdapter(Enumeration<E> enumeration) {
|
||||
this.enumeration = enumeration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return enumeration.hasMoreElements();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
return enumeration.nextElement();
|
||||
}
|
||||
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.adapter;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
public class StringTokenizerIteratorAdapter extends StringTokenizer implements Iterator<String> {
|
||||
|
||||
public StringTokenizerIteratorAdapter(final String str, final String delim, final boolean returnDelims) {
|
||||
super(str, delim, returnDelims);
|
||||
}
|
||||
|
||||
public StringTokenizerIteratorAdapter(final String str, final String delim) {
|
||||
super(str, delim);
|
||||
}
|
||||
|
||||
public StringTokenizerIteratorAdapter(final String str) {
|
||||
super(str);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return hasMoreTokens();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String next() {
|
||||
return nextToken();
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.pipeline.immutable;
|
||||
|
||||
public interface Pipe<IN, OUT> {
|
||||
OUT process(IN input);
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.pipeline.immutable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
public class Pipeline<IN, OUT> {
|
||||
|
||||
private Collection<Pipe<?, ?>> pipes;
|
||||
|
||||
private Pipeline(Pipe<IN, OUT> pipe) {
|
||||
pipes = Collections.singletonList(pipe);
|
||||
}
|
||||
|
||||
private Pipeline(Collection<Pipe<?, ?>> pipes) {
|
||||
this.pipes = new ArrayList<>(pipes);
|
||||
}
|
||||
|
||||
public static <IN, OUT> Pipeline<IN, OUT> of(Pipe<IN, OUT> pipe) {
|
||||
return new Pipeline<>(pipe);
|
||||
}
|
||||
|
||||
|
||||
public <NEW_OUT> Pipeline<IN, NEW_OUT> withNextPipe(Pipe<OUT, NEW_OUT> pipe) {
|
||||
final ArrayList<Pipe<?, ?>> newPipes = new ArrayList<>(pipes);
|
||||
newPipes.add(pipe);
|
||||
return new Pipeline<>(newPipes);
|
||||
}
|
||||
|
||||
public OUT process(IN input) {
|
||||
Object output = input;
|
||||
for (final Pipe pipe : pipes) {
|
||||
output = pipe.process(output);
|
||||
}
|
||||
return (OUT) output;
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.pipeline.pipes;
|
||||
|
||||
public interface Pipe<IN, OUT> {
|
||||
OUT process(IN input);
|
||||
|
||||
default <NEW_OUT> Pipe<IN, NEW_OUT> add(Pipe <OUT, NEW_OUT> pipe) {
|
||||
return input -> pipe.process(process(input));
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
|
||||
# Root logger
|
||||
log4j.rootLogger=INFO, file, stdout
|
||||
|
||||
# Write to console
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.Target=System.out
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.adapter;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.StringTokenizer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class IteratorAdapterUnitTest {
|
||||
|
||||
public static final String STRING_DATA = "Welcome to baeldung.com";
|
||||
private final StringTokenizer tokenizer = new StringTokenizer(STRING_DATA);
|
||||
private final Iterator<String> tokenizerAdapter = new StringTokenizerIteratorAdapter(STRING_DATA);
|
||||
private final List<String> expectedTokensForString = Arrays.asList("Welcome", "to", "baeldung.com");
|
||||
|
||||
@Test
|
||||
void givenTokenizer_thenAdapterProducesCorrectResult() {
|
||||
List<String> actualTokens = new ArrayList<>();
|
||||
new IteratorAdapter(tokenizer).forEachRemaining(s -> actualTokens.add((String) s));
|
||||
assertEquals(expectedTokensForString, actualTokens);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTokenizerAdapter_thenIteratorProducesCorrectResult() {
|
||||
List<String> actualTokens = new ArrayList<>();
|
||||
tokenizerAdapter.forEachRemaining(actualTokens::add);
|
||||
assertEquals(expectedTokensForString, actualTokens);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.pipeline;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class BiFunctionPipelineUnitTest {
|
||||
|
||||
@Test
|
||||
void whenCombiningFunctionAndBiFunctions_andInitializingPipeline_thenResultIsCorrect() {
|
||||
BiFunction<Integer, Integer, Integer> add = Integer::sum;
|
||||
BiFunction<Integer, Integer, Integer> mul = (a, b) -> a * b;
|
||||
Function<Integer, String> toString = Object::toString;
|
||||
BiFunction<Integer, Integer, String> pipeline = add.andThen(a -> mul.apply(a, 2))
|
||||
.andThen(toString);
|
||||
String result = pipeline.apply(1, 2);
|
||||
String expected = "6";
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.pipeline;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.function.Function;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class FunctionPipelineUnitTest {
|
||||
|
||||
@Test
|
||||
void whenCombiningThreeFunctions_andInitializingPipeline_thenResultIsCorrect() {
|
||||
Function<Integer, Integer> square = s -> s * s;
|
||||
Function<Integer, Integer> half = s -> s / 2;
|
||||
Function<Integer, String> toString = Object::toString;
|
||||
Function<Integer, String> pipeline = square.andThen(half)
|
||||
.andThen(toString);
|
||||
String result = pipeline.apply(5);
|
||||
String expected = "12";
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.pipeline;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import com.baeldung.pipeline.pipes.Pipe;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class PipeUnitTest {
|
||||
|
||||
@Test
|
||||
void whenCombiningThreePipes_andInitializingPipeline_thenResultIsCorrect() {
|
||||
Pipe<Integer, Integer> square = s -> s * s;
|
||||
Pipe<Integer, Integer> half = s -> s / 2;
|
||||
Pipe<Integer, String> toString = Object::toString;
|
||||
Pipe<Integer, String> pipeline = square.add(half).add(toString);
|
||||
String result = pipeline.process(5);
|
||||
String expected = "12";
|
||||
assertThat(result).isEqualTo(expected);
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.pipeline;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import com.baeldung.pipeline.immutable.Pipe;
|
||||
import com.baeldung.pipeline.immutable.Pipeline;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class PipelineUnitTest {
|
||||
|
||||
@Test
|
||||
void whenCombiningThreePipes_andInitializingPipeline_thenResultIsCorrect() {
|
||||
Pipe<Integer, Integer> square = s -> s * s;
|
||||
Pipe<Integer, Integer> half = s -> s / 2;
|
||||
Pipe<Integer, String> toString = Object::toString;
|
||||
Pipeline<Integer, Integer> squarePipeline = Pipeline.of(square);
|
||||
Pipeline<Integer, Integer> squareAndHalfPipeline = squarePipeline.withNextPipe(half);
|
||||
Pipeline<Integer, String> squareHalfAndStringPipeline = squareAndHalfPipeline.withNextPipe(toString);
|
||||
|
||||
String result = squareHalfAndStringPipeline.process(5);
|
||||
String expected = "12";
|
||||
assertThat(result).isEqualTo(expected);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user