/*
 * 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.server.integration;

import com.google.common.collect.ImmutableSet;
import io.spine.core.BoundedContextName;
import io.spine.server.transport.Publisher;

import java.util.Set;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.spine.base.Identifier.newUuid;
import static io.spine.base.Identifier.pack;

/**
 * A client of the {@code RequestForExternalMessages} {@link Publisher}.
 *
 * <p>Posts the updates on the requested messages.
 */
final class ConfigurationBroadcast {

    private final BoundedContextName contextName;
    private final Publisher needsPublisher;

    private ImmutableSet<ExternalMessageType> knownNeeds = ImmutableSet.of();

    ConfigurationBroadcast(BoundedContextName contextName, Publisher publisher) {
        this.contextName = checkNotNull(contextName);
        this.needsPublisher = checkNotNull(publisher);
    }

    /**
     * Notifies other Bounded contexts about a change in the requested messages.
     *
     * <p>If the given {@code needs} are the same as previous ones, i.e. the needs did not change,
     * the request is not sent.
     *
     * @param needs
     *         the new needs of current context
     */
    synchronized void onNeedsUpdated(Set<ExternalMessageType> needs) {
        checkNotNull(needs);
        ImmutableSet<ExternalMessageType> newNeeds = ImmutableSet.copyOf(needs);
        if (!knownNeeds.equals(newNeeds)) {
            knownNeeds = newNeeds;
            send();
        }
    }

    /**
     * Notifies other Bounded contexts about current requested messages.
     */
    synchronized void send() {
        RequestForExternalMessages request = RequestForExternalMessages
                .newBuilder()
                .addAllRequestedMessageType(knownNeeds)
                .buildPartial();
        ExternalMessage externalMessage = ExternalMessages.of(request, contextName);
        needsPublisher.publish(pack(newUuid()), externalMessage);
    }
}

@@ -1181,31 +1181,8 @@
 r;%0A%0A
-import java.util.Set;%0A%0A
 impo
@@ -2228,16 +2228,25 @@
 Updated(
+Immutable
 Set%3CExte
@@ -2304,89 +2304,8 @@
 s);%0A
-        ImmutableSet%3CExternalMessageType%3E newNeeds = ImmutableSet.copyOf(needs);%0A
     
@@ -2333,19 +2333,16 @@
 quals(ne
-wNe
 eds)) %7B%0A
@@ -2372,11 +2372,8 @@
 = ne
-wNe
 eds;

Remove redundant copying