/*
 * Copyright 2019, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * 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 THE COPYRIGHT
 * OWNER OR CONTRIBUTORS 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 io.spine.client;

import com.google.common.annotations.VisibleForTesting;

import java.util.ArrayList;
import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Maintains the list of subscriptions created by the {@link Client} which are
 * not cancelled yet. Cancels remaining subscriptions upon request.
 */
final class ActiveSubscriptions {

    private final List<Subscription> subscriptions = new ArrayList<>();

    /** Remembers the passed subscription for future use. */
    synchronized void remember(Subscription s) {
        subscriptions.add(checkNotNull(s));
    }

    /** Forgets the passed subscription. */
    synchronized void forget(Subscription s) {
        subscriptions.remove(checkNotNull(s));
    }

    /** Cancels all the remembered subscription. */
    synchronized void cancelAll(Client client) {
        // Use the loop approach to avoid concurrent modification because the `Client` modifies
        // active subscriptions when canceling.
        while (!subscriptions.isEmpty()) {
            Subscription subscription = subscriptions.get(0);
            client.cancel(subscription);
        }
    }

    @VisibleForTesting
    synchronized boolean contains(Subscription s) {
        return subscriptions.contains(s);
    }

    @VisibleForTesting
    synchronized boolean isEmpty() {
        return subscriptions.isEmpty();
    }
}

@@ -1087,16 +1087,52 @@
 Testing;
+%0Aimport io.grpc.stub.StreamObserver;
 %0A%0Aimport
@@ -1351,53 +1351,327 @@
 yet.
- Cancels remaining subscriptions upon
+%0A *%0A * %3Cp%3EThe %7B@link Client%7D updates this collection when a new subscription is%0A * %7B@linkplain Client#subscribeTo(Topic, StreamObserver) created%7D and%0A * %7B@linkplain Client#cancel(Subscription) canceled%7D.%0A *%0A * %3Cp%3ERemaining subscriptions are cancelled by the %7B@code Client%7D upon%0A * %7B@linkplain #cancelAll(Client)
  request
 .%0A *
@@ -1666,16 +1666,17 @@
  request
+%7D
 .%0A */%0Afi
@@ -1842,37 +1842,24 @@
 use. */%0A    
-synchronized 
 void remembe
@@ -1973,37 +1973,24 @@
 ion. */%0A    
-synchronized 
 void forget(
@@ -2117,29 +2117,16 @@
  */%0A    
-synchronized 
 void can
@@ -2479,37 +2479,24 @@
 Testing%0A    
-synchronized 
 boolean cont
@@ -2594,25 +2594,12 @@
 ing%0A
+
     
-synchronized 
 bool

Remove premature synchronization