Skip to content

Commit 8c386c0

Browse files
committed
added ChainLinks which allows us to access the resulting Chain structure
1 parent 2aa05f5 commit 8c386c0

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

src/main/java/com/github/hakenadu/javalangchains/chains/Chain.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.github.hakenadu.javalangchains.chains;
22

3-
import org.apache.logging.log4j.LogManager;
4-
53
/**
64
* Basic interface for all modular components in this repository. A
75
* {@link Chain} accepts an input of type *I* and provides an output of type
@@ -31,12 +29,7 @@ public interface Chain<I, O> {
3129
* @return a new {@link Chain} consisting of the original {@link Chain} and the
3230
* passed one
3331
*/
34-
default <B> Chain<I, B> chain(final Chain<O, B> next) {
35-
return input -> {
36-
final O prevOutput = run(input);
37-
LogManager.getLogger(next.getClass()).info("\n\n=================== INPUT ===================\n{}\n=============================================\n", prevOutput);
38-
39-
return next.run(prevOutput);
40-
};
32+
default <B> ChainLink<I, O, B> chain(final Chain<O, B> next) {
33+
return new ChainLink<>(this, next);
4134
}
4235
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.github.hakenadu.javalangchains.chains;
2+
3+
/**
4+
* A Link Between Worlds ;-)
5+
*
6+
* @param <I> type of the input chain's input
7+
* @param <M> type of the input chain's output and the output chain's input
8+
* @param <O> type of the output chain's output
9+
*/
10+
public final class ChainLink<I, M, O> implements Chain<I, O> {
11+
12+
private final Chain<I, M> inputChain;
13+
private final Chain<M, O> outputChain;
14+
15+
/**
16+
* @param inputChain {@link #inputChain}
17+
* @param outputChain {@link #outputChain}
18+
*/
19+
ChainLink(final Chain<I, M> inputChain, final Chain<M, O> outputChain) {
20+
this.inputChain = inputChain;
21+
this.outputChain = outputChain;
22+
}
23+
24+
@Override
25+
public O run(final I input) {
26+
final M intermediateOutput = inputChain.run(input);
27+
28+
final O output = outputChain.run(intermediateOutput);
29+
30+
return output;
31+
}
32+
33+
/**
34+
* @return true if this {@link ChainLink} is the first one of the whole chain
35+
*/
36+
public boolean isHead() {
37+
return !(inputChain instanceof ChainLink);
38+
}
39+
40+
/**
41+
* @return true if this {@link ChainLink} is the final one of the whole chain
42+
*/
43+
public boolean isTail() {
44+
return !(outputChain instanceof ChainLink);
45+
}
46+
47+
/**
48+
* @return {@link #inputChain}
49+
*/
50+
public Chain<I, M> getInputChain() {
51+
return inputChain;
52+
}
53+
54+
/**
55+
* @return {@link #outputChain}
56+
*/
57+
public Chain<M, O> getOutputChain() {
58+
return outputChain;
59+
}
60+
}

0 commit comments

Comments
 (0)