/*
 * Copyright 2018, 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.system.server;

import io.spine.core.EventClass;
import io.spine.core.EventEnvelope;
import io.spine.server.bus.DispatcherRegistry;
import io.spine.server.event.EventBus;
import io.spine.server.event.EventDispatcher;

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

public final class SystemBus
        implements DispatcherRegistry<EventClass, EventEnvelope, EventDispatcher<?>> {

    private final EventBus systemEventBus;

    private SystemBus(EventBus systemEventBus) {
        this.systemEventBus = checkNotNull(systemEventBus);
    }

    public static SystemBus newInstance(SystemContext context) {
        checkNotNull(context);
        EventBus delegate = context.getEventBus();
        return new SystemBus(delegate);
    }

    @Override
    public void register(EventDispatcher<?> dispatcher) {
        checkNotNull(dispatcher);
        systemEventBus.register(dispatcher);
    }

    @Override
    public void unregister(EventDispatcher<?> dispatcher) {
        checkNotNull(dispatcher);
        systemEventBus.unregister(dispatcher);
    }
}

@@ -1035,24 +1035,56 @@
 em.server;%0A%0A
+import io.spine.annotation.SPI;%0A
 import io.sp
@@ -1343,284 +1343,1225 @@
 l;%0A%0A
-public final class SystemBus%0A        implements DispatcherRegistry%3CEventClass, EventEnvelope, EventDispatcher%3C?%3E%3E %7B%0A%0A    private final EventBus systemEventBus;%0A%0A    private SystemBus(EventBus systemEventBus) %7B%0A        this.systemEventBus = checkNotNull(systemEventBus);%0A    %7D%0A
+/**%0A * A message bus for system events.%0A *%0A * %3Cp%3EA domain bounded context may register dispatchers of system events in the %7B@code SystemBus%7D.%0A * All the events of a system context are broadcast by this bus.%0A *%0A * %3Cp%3EOnly the system events are allowed in the %7B@code SystemBus%7D. This class does not extend%0A * the %7B@link io.spine.server.bus.Bus Bus%7D base class in order to restrict users from posting events%0A * into the system bus.%0A *%0A * @implNote%0A * A system bus is a delegate for the system event bus. When registering a dispatcher in%0A * the system bus, the dispatcher gets registered in the system event bus. This way, all%0A * the messages posted into the system event bus can be accessed via the system bus.%0A */%0A@SPI%0Apublic final class SystemBus%0A        implements DispatcherRegistry%3CEventClass, EventEnvelope, EventDispatcher%3C?%3E%3E %7B%0A%0A    private final EventBus systemEventBus;%0A%0A    private SystemBus(EventBus systemEventBus) %7B%0A        this.systemEventBus = systemEventBus;%0A    %7D%0A%0A    /**%0A     * Creates a new instance of %7B@code SystemBus%7D for the given system context.%0A     *%0A     * @param context%0A     *         the system context to broadcast the events of%0A     * @return a new instance of %7B@code SystemBus%7D%0A     */
 %0A   

Document `SystemBus`.