2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util;
21 import java.util.concurrent.atomic.AtomicInteger;
24 * <p>A callback wrapper that succeeds the wrapped callback when the count is
25 * reached, or on first failure.</p>
26 * <p>This callback is particularly useful when an async operation is split
27 * into multiple parts, for example when an original byte buffer that needs
28 * to be written, along with a callback, is split into multiple byte buffers,
29 * since it allows the original callback to be wrapped and notified only when
30 * the last part has been processed.</p>
33 * public void process(EndPoint endPoint, ByteBuffer buffer, Callback callback)
35 * ByteBuffer[] buffers = split(buffer);
36 * CountCallback countCallback = new CountCallback(callback, buffers.length);
37 * endPoint.write(countCallback, buffers);
41 public class CountingCallback implements Callback
43 private final Callback callback;
44 private final AtomicInteger count;
46 public CountingCallback(Callback callback, int count)
48 this.callback = callback;
49 this.count = new AtomicInteger(count);
53 public void succeeded()
55 // Forward success on the last success.
58 int current = count.get();
60 // Already completed ?
64 if (count.compareAndSet(current, current - 1))
74 public void failed(Throwable failure)
76 // Forward failure on the first failure.
79 int current = count.get();
81 // Already completed ?
85 if (count.compareAndSet(current, 0))
87 callback.failed(failure);
94 public String toString()
96 return String.format("%s@%x", getClass().getSimpleName(), hashCode());