aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/CompletionHandlers.java
blob: 1063c70fe0f380564f0f954d932141800c9c6a53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.server.jetty;

import com.yahoo.jdisc.handler.CompletionHandler;

import java.util.Arrays;

/**
 * @author Simon Thoresen Hult
 */
public class CompletionHandlers {

    public static void tryComplete(CompletionHandler handler) {
        if (handler == null) {
            return;
        }
        try {
            handler.completed();
        } catch (Exception e) {
            // ignore
        }
    }

    public static void tryFail(CompletionHandler handler, Throwable t) {
        if (handler == null) {
            return;
        }
        try {
            handler.failed(t);
        } catch (Exception e) {
            // ignore
        }
    }

    public static CompletionHandler wrap(CompletionHandler... handlers) {
        return wrap(Arrays.asList(handlers));
    }

    public static CompletionHandler wrap(final Iterable<CompletionHandler> handlers) {
        return new CompletionHandler() {

            @Override
            public void completed() {
                for (CompletionHandler handler : handlers) {
                    tryComplete(handler);
                }
            }

            @Override
            public void failed(Throwable t) {
                for (CompletionHandler handler : handlers) {
                    tryFail(handler, t);
                }
            }
        };
    }
}