summaryrefslogtreecommitdiffstats
blob: de1bd6eca5c1927b3d49ef7a2d26dda859bc8f6f (plain)
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
58
59
60
61
62
/*
 * Copyright (c) 2015, Isode Limited, London, England.
 * All rights reserved.
 */
package com.isode.stroke.signals;

import java.util.HashMap;
import java.util.Map;

/* package */ abstract class BaseSignal implements SignalConnection.DisconnectListener {
    
    // Optimized for the case of 0 or 1 bind
    private SignalConnection connection_;
    private BaseSlot bind_;
    private Map<SignalConnection, BaseSlot> binds_;
    
    protected final synchronized SignalConnection addBind(final BaseSlot bind) {
        final SignalConnection connection = new SignalConnection(this);
        if (binds_ != null) {
            binds_.put(connection, bind);
        } else if (connection_ != null) {
            binds_ = new HashMap<SignalConnection, BaseSlot>();
            binds_.put(connection_, bind_);
            connection_ = null;
            bind_ = null;
            binds_.put(connection, bind);
        } else {
            connection_ = connection;
            bind_ = bind;
        }
        return connection;
    }
    
    protected final synchronized BaseSlot[] getBinds() {
        if (binds_ != null) {
            return binds_.values().toArray(new BaseSlot[binds_.size()]);
        } else if (connection_ != null) {
            return new BaseSlot[]{bind_};
        } else {
            return null; // return null rather than allocate an empty array
        }
    }
    
    @Override
    public final synchronized void onSignalConnectionDisconnect(final SignalConnection connection) {
        if (binds_ != null) {
            binds_.remove(connection);
        } else if (connection_ == connection) {
            connection_ = null;
            bind_ = null;
        }
    }

    public final synchronized void disconnectAll() {
        if (binds_ != null) {
            binds_.clear();
        } else {
            connection_ = null;
            bind_ = null;
        }
    }
}