/*
 * 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.event.model;

import com.google.common.collect.ImmutableSet;
import io.spine.server.event.AbstractEventSubscriber;
import io.spine.server.model.HandlerMethod;
import io.spine.server.model.MessageHandlerMap;
import io.spine.server.model.ModelClass;
import io.spine.server.type.EmptyClass;
import io.spine.server.type.EventClass;
import io.spine.type.MessageClass;

import java.util.Collection;
import java.util.Set;

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

/**
 * Provides type information on an {@link AbstractEventSubscriber} class.
 *
 * @param <S>
 *         the type of event subscribers
 */
public final class EventSubscriberClass<S extends AbstractEventSubscriber> extends ModelClass<S>
    implements EventReceiverClass, SubscribingClass {

    private static final long serialVersionUID = 0L;

    private final MessageHandlerMap<EventClass, EmptyClass, SubscriberMethod> eventSubscriptions;
    private final ImmutableSet<EventClass> domesticSubscriptions;
    private final ImmutableSet<EventClass> externalSubscriptions;

    private EventSubscriberClass(Class<? extends S> cls) {
        super(cls);
        this.eventSubscriptions = MessageHandlerMap.create(cls, new SubscriberSignature());
        this.domesticSubscriptions =
                    eventSubscriptions.messageClasses(HandlerMethod::isDomestic);
        this.externalSubscriptions =
                    eventSubscriptions.messageClasses(HandlerMethod::isExternal);
    }

    /**
     * Creates new instance for the passed raw class.
     */
    public static <S extends AbstractEventSubscriber>
    EventSubscriberClass<S> asEventSubscriberClass(Class<S> cls) {
        checkNotNull(cls);
        EventSubscriberClass<S> result = (EventSubscriberClass<S>)
                get(cls, EventSubscriberClass.class, () -> new EventSubscriberClass<>(cls));
        return result;
    }

    @Override
    public Set<EventClass> incomingEvents() {
        return domesticSubscriptions;
    }

    @Override
    public Set<EventClass> externalEvents() {
        return externalSubscriptions;
    }

    @Override
    public Collection<SubscriberMethod> subscribersOf(EventClass eventClass,
                                                      MessageClass originClass) {
        return eventSubscriptions.handlersOf(eventClass, originClass);
    }
}

@@ -1936,22 +1936,17 @@
 Method%3E 
-eventS
+s
 ubscript
@@ -1998,36 +1998,29 @@
 ss%3E domestic
-Subscription
+Event
 s;%0A    priva
@@ -2061,28 +2061,21 @@
 external
-Subscription
+Event
 s;%0A%0A    
@@ -2162,22 +2162,17 @@
    this.
-eventS
+s
 ubscript
@@ -2261,50 +2261,18 @@
 stic
-Subscriptions =%0A                    eventS
+Events = s
 ubsc
@@ -2348,50 +2348,18 @@
 rnal
-Subscriptions =%0A                    eventS
+Events = s
 ubsc
@@ -2908,28 +2908,21 @@
 domestic
-Subscription
+Event
 s;%0A    %7D
@@ -3006,28 +3006,21 @@
 external
-Subscription
+Event
 s;%0A    %7D
@@ -3045,16 +3045,20 @@
   public
+%0A   
  Collect
@@ -3119,62 +3119,8 @@
 ass,
-%0A                                                     
  Mes
@@ -3163,14 +3163,9 @@
 urn 
-eventS
+s
 ubsc

Simplify field names