1 | package net.spy.memcached.ops; |
2 | |
3 | /** |
4 | * An operation callback that will capture receivedStatus and complete |
5 | * invocations and dispatch to a single callback. |
6 | * |
7 | * <p> |
8 | * This is useful for the cases where a single request gets split into |
9 | * multiple requests and the callback needs to not know the difference. |
10 | * </p> |
11 | */ |
12 | public abstract class MultiOperationCallback implements OperationCallback { |
13 | |
14 | private OperationStatus mostRecentStatus = null; |
15 | private int remaining=0; |
16 | protected final OperationCallback originalCallback; |
17 | |
18 | /** |
19 | * Get a MultiOperationCallback over the given callback for the specified |
20 | * number of replicates. |
21 | * |
22 | * @param original the original callback |
23 | * @param todo how many complete() calls we expect before dispatching. |
24 | */ |
25 | public MultiOperationCallback(OperationCallback original, int todo) { |
26 | originalCallback = original; |
27 | remaining = todo; |
28 | } |
29 | |
30 | public void complete() { |
31 | if(--remaining == 0) { |
32 | originalCallback.receivedStatus(mostRecentStatus); |
33 | originalCallback.complete(); |
34 | } |
35 | } |
36 | |
37 | public void receivedStatus(OperationStatus status) { |
38 | mostRecentStatus = status; |
39 | } |
40 | |
41 | } |