/*
 * Copyright 2014-2017 Real Logic Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.aeron;

import org.agrona.collections.Int2ObjectHashMap;

import java.util.ArrayList;
import java.util.function.Consumer;

class ActiveSubscriptions
{
    private final Int2ObjectHashMap<ArrayList<Subscription>> subscriptionsByStreamIdMap = new Int2ObjectHashMap<>();

    public void forEach(final int streamId, final Consumer<Subscription> consumer)
    {
        final ArrayList<Subscription> subscriptions = subscriptionsByStreamIdMap.get(streamId);
        if (null != subscriptions)
        {
            subscriptions.forEach(consumer);
        }
    }

    public void add(final Subscription subscription)
    {
        subscriptionsByStreamIdMap
            .computeIfAbsent(subscription.streamId(), (ignore) -> new ArrayList<>())
            .add(subscription);
    }

    public void remove(final Subscription subscription)
    {
        final int streamId = subscription.streamId();
        final ArrayList<Subscription> subscriptions = subscriptionsByStreamIdMap.get(streamId);

        if (null != subscriptions && subscriptions.remove(subscription) && subscriptions.isEmpty())
        {
            subscriptionsByStreamIdMap.remove(streamId);
        }
    }

    public void close()
    {
        for (final ArrayList<Subscription> subscriptions : subscriptionsByStreamIdMap.values())
        {
            for (int i = 0, size = subscriptions.size(); i < size; i++)
            {
                subscriptions.get(i).forceClose();
            }
        }

        subscriptionsByStreamIdMap.clear();
    }
}

@@ -612,16 +612,61 @@
 aeron;%0A%0A
+import org.agrona.collections.ArrayListUtil;%0A
 import o
@@ -706,16 +706,16 @@
 ashMap;%0A
-
 %0Aimport 
@@ -1281,132 +1281,300 @@
     
-subscriptionsByStreamIdMap%0A            .computeIfAbsent(subscription.streamId(), (ignore) -%3E new ArrayList%3C%3E())%0A
+ArrayList%3CSubscription%3E subscriptions = subscriptionsByStreamIdMap.get(subscription.streamId());%0A        if (null == subscriptions)%0A        %7B%0A            subscriptions = new ArrayList%3C%3E();%0A            subscriptionsByStreamIdMap.put(subscription.streamId(), subscriptions);%0A    
     
+%7D%0A%0A
         
 .add
@@ -1569,16 +1569,29 @@
         
+subscriptions
 .add(sub
@@ -1855,24 +1855,31 @@
 riptions && 
+remove(
 subscription
@@ -1879,24 +1879,18 @@
 riptions
-.remove(
+, 
 subscrip
@@ -2297,32 +2297,32 @@
    %7D%0A        %7D%0A%0A
-
         subscrip
@@ -2354,11 +2354,437 @@
 );%0A    %7D
+%0A%0A    private static boolean remove(final ArrayList%3CSubscription%3E subscriptions, final Subscription subscription)%0A    %7B%0A        for (int i = 0, size = subscriptions.size(); i %3C size; i++)%0A        %7B%0A            if (subscription == subscriptions.get(i))%0A            %7B%0A                ArrayListUtil.fastUnorderedRemove(subscriptions, i, size - 1);%0A                return true;%0A            %7D%0A        %7D%0A%0A        return false;%0A    %7D
 %0A%7D%0A

Improve the efficiency of ActiveSubscriptions add and remove.