36 lines
748 B
Java
36 lines
748 B
Java
|
// functional/TransformFunction.java
|
|||
|
// <20>2016 MindView LLC: see Copyright.txt
|
|||
|
// We make no guarantees that this code is fit for any purpose.
|
|||
|
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
|||
|
import java.util.function.*;
|
|||
|
|
|||
|
class I {
|
|||
|
@Override
|
|||
|
public String toString() { return "I"; }
|
|||
|
}
|
|||
|
|
|||
|
class O {
|
|||
|
@Override
|
|||
|
public String toString() { return "O"; }
|
|||
|
}
|
|||
|
|
|||
|
public class TransformFunction {
|
|||
|
static Function<I,O> transform(Function<I,O> in) {
|
|||
|
return in.andThen(o -> {
|
|||
|
System.out.println(o);
|
|||
|
return o;
|
|||
|
});
|
|||
|
}
|
|||
|
public static void main(String[] args) {
|
|||
|
Function<I,O> f2 = transform(i -> {
|
|||
|
System.out.println(i);
|
|||
|
return new O();
|
|||
|
});
|
|||
|
O o = f2.apply(new I());
|
|||
|
}
|
|||
|
}
|
|||
|
/* Output:
|
|||
|
I
|
|||
|
O
|
|||
|
*/
|