/* Copyright © 2014 Matthew Champion
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
 * Neither the name of mattunderscore.com nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL MATTHEW CHAMPION BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

package com.mattunderscore.tcproxy.proxy.connection;

import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * Manager for connections.
 * @author Matt Champion on 22/02/14.
 */
public final class ConnectionManager {
    private final Set<Connection> connections = new CopyOnWriteArraySet<>();
    private final Set<Listener> listeners = new CopyOnWriteArraySet<>();

    /**
     * Register a new connection.
     * @param connection The new connection
     */
    public void register(Connection connection) {
        if (connections.add(connection)) {
            for (final Listener listener : listeners) {
                listener.newConnection(connection);
            }
        }
    }

    /**
     * Unregister an existing connection.
     * @param connection The existing connection
     */
    public void unregister(Connection connection) {
        if (connections.remove(connection)) {
            for (final Listener listener : listeners) {
                listener.closedConnection(connection);
            }
        }
    }

    /**
     * @return The current connections
     */
    public Set<Connection> getConnections() {
        return connections;
    }

    /**
     * Add a listener for connection events.
     * @param listener The listener
     */
    public void addListener(final Listener listener) {
        listeners.add(listener);
    }

    /**
     * Connection event listener.
     */
    public interface Listener {
        /**
         * Notified of new connections.
         * @param connection The new connection
         */
        void newConnection(Connection connection);

        /**
         * Notified of closed connections.
         * @param connection The close connections
         */
        void closedConnection(Connection connection);
    }
}

@@ -1543,16 +1543,42 @@
 ction;%0A%0A
+import java.util.HashSet;%0A
 import j
@@ -1587,24 +1587,24 @@
 a.util.Set;%0A
-
 import java.
@@ -1641,16 +1641,96 @@
 aySet;%0A%0A
+import net.jcip.annotations.GuardedBy;%0Aimport net.jcip.annotations.ThreadSafe;%0A%0A
 /**%0A * M
@@ -1795,16 +1795,28 @@
 14.%0A */%0A
+@ThreadSafe%0A
 public f
@@ -1842,24 +1842,54 @@
 onManager %7B%0A
+    @GuardedBy(%22connections%22)%0A
     private 
@@ -1928,32 +1928,20 @@
 s = new 
-CopyOnWriteArray
+Hash
 Set%3C%3E();
@@ -2171,39 +2171,136 @@
     
-if (connections.add(connection)
+final boolean add;%0A        synchronized (connections) %7B%0A            add = connections.add(connection);%0A        %7D%0A        if (add
 ) %7B%0A
@@ -2609,42 +2609,148 @@
     
-if (connections.remove(connection)
+final boolean remove;%0A        synchronized (connections) %7B%0A            remove = connections.remove(connection);%0A        %7D%0A        if (remove
 ) %7B%0A

Use an intrinsic lock to protect the connection set to save copying it every time a connection is added.