/*
 * Copyright 2014 - 2016 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.Collection;
import java.util.List;
import java.util.function.Consumer;

import static java.util.stream.Collectors.toList;

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

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

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

        subscriptions.add(subscription);
    }

    public void remove(final Subscription subscription)
    {
        final int streamId = subscription.streamId();
        final List<Subscription> subscriptions = subscriptionsByStreamIdMap.get(streamId);
        if (subscriptions.remove(subscription) && subscriptions.isEmpty())
        {
            subscriptionsByStreamIdMap.remove(streamId);
            if (subscriptionsByStreamIdMap.isEmpty())
            {
                subscriptionsByStreamIdMap.remove(streamId);
            }
        }
    }

    public void close()
    {
        subscriptionsByStreamIdMap
            .values()
            .stream()
            .flatMap(Collection::stream)
            .collect(toList())
            .forEach(Subscription::close);
    }
}

@@ -1046,21 +1046,22 @@
 iption%3E 
-handl
+consum
 er)%0A    
@@ -1236,13 +1236,14 @@
 ach(
-handl
+consum
 er);
@@ -1330,49 +1330,8 @@
     
- final List%3CSubscription%3E subscriptions =
  sub
@@ -1353,16 +1353,29 @@
 eamIdMap
+%0A            
 .compute
@@ -1383,29 +1383,16 @@
 fAbsent(
-%0A            
 subscrip
@@ -1438,18 +1438,16 @@
 ist%3C%3E())
-;%0A
 %0A       
@@ -1447,29 +1447,20 @@
         
-subscriptions
+    
 .add(sub
@@ -1677,32 +1677,33 @@
 .get(streamId);%0A
+%0A
         if (subs
@@ -1702,29 +1702,16 @@
 if (
-subscriptions.remove(
+null != 
 subs
@@ -1718,17 +1718,17 @@
 cription
-)
+s
  && subs
@@ -1741,128 +1741,45 @@
 ons.
-isEmpty())%0A        %7B%0A            subscriptionsByStreamIdMap.remove(streamId);%0A            if (subscriptionsByStreamIdMap
+remove(subscription) && subscriptions
 .isE
@@ -1782,28 +1782,24 @@
 .isEmpty())%0A
-    
         %7B%0A  
@@ -1800,36 +1800,32 @@
   %7B%0A            
-    
 subscriptionsByS
@@ -1857,22 +1857,8 @@
 d);%0A
-            %7D%0A
     

Deal with case of removing a Subscription that has been previously removed.