| @@ -10,12 +10,25 public final class Closures { | |||||
| 10 | private Closures() { |
|
10 | private Closures() { | |
| 11 | } |
|
11 | } | |
| 12 |
|
12 | |||
|
|
13 | /** | |||
|
|
14 | * Wraps {@link Action} around the specified closure. The parameter | |||
|
|
15 | * of the action will be used as delegate in the specified closure. | |||
|
|
16 | * | |||
|
|
17 | * @param <T> The type of the action parameter | |||
|
|
18 | * @param closure The closure | |||
|
|
19 | * @return | |||
|
|
20 | */ | |||
| 13 | public static <T> Action<T> action(Closure<?> closure) { |
|
21 | public static <T> Action<T> action(Closure<?> closure) { | |
| 14 | return arg -> apply(closure, arg); |
|
22 | return arg -> apply(closure, arg); | |
| 15 | } |
|
23 | } | |
| 16 |
|
24 | |||
| 17 | public static void apply(Closure<?> action, Object target) { |
|
25 | public static void apply(Closure<?> action, Object target) { | |
| 18 |
action. |
|
26 | var prevDelegate = action.getDelegate(); | |
| 19 | action.call(target); |
|
27 | try { | |
|
|
28 | action.setDelegate(target); | |||
|
|
29 | action.call(target); | |||
|
|
30 | } finally { | |||
|
|
31 | action.setDelegate(prevDelegate); | |||
|
|
32 | } | |||
| 20 | } |
|
33 | } | |
| 21 | } |
|
34 | } | |
General Comments 0
You need to be logged in to leave comments.
Login now
