With Lombok
01 import lombok.ExtensionMethod;
02 import lombok.Action;
03 import lombok.Actions.Action1;
04
05 @ExtensionMethod(Operations.class)
06 public class Main {
07
08 public static void main(final String[] args) {
09 "Hello World".andThen(println());
10 }
11
12 @Action
13 private static void println(final Object o) {
14 System.out.println(o);
15 }
16 }
17
18 public class Operations {
19
20 public static <T> void andThen(final T value, final Action1<T> andThen) {
21 if (value != null) andThen.apply(value);
22 }
23 }
Vanilla Java
01 import lombok.Actions.Action1;
02
03 public class Main {
04
05 public static void main(String[] args) {
06 Operations.andThen("Hello World", println());
07 }
08
09 private static Action1<Object> println() {
10 return new Action1<Object>() {
11 public void apply(final Object o) {
12 System.out.println(o);
13 }
14 };
15 }
16 }
17
18 public class Operations {
19
20 public static <T> void andThen(final T value, final Action1<T> andThen) {
21 if (value != null) andThen.apply(value);
22 }
23 }