/*
 * Copyright 2020, 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.delivery;

import io.spine.type.TypeUrl;

import java.util.Map;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Maps.newConcurrentMap;

/**
 * @author Alex Tymchenko
 */
final class InboxDeliveries {

    private final Map<String, ShardedMessageDelivery<InboxMessage>> contents = newConcurrentMap();

    ShardedMessageDelivery<InboxMessage> get(String typeUrl) {
        ShardedMessageDelivery<InboxMessage> result = contents.get(typeUrl);
        checkNotNull(result,
                     "The delivery is not registered for the Inbox of type `%s`.", typeUrl);
        return result;
    }

    ShardedMessageDelivery<InboxMessage> get(InboxMessage message) {
        String typeUrl = message.getInboxId()
                                .getTypeUrl();
        return get(typeUrl);
    }

    void register(Inbox<?> inbox) {
        TypeUrl entityType = inbox.entityStateType();
        contents.put(entityType.value(), inbox.delivery());
    }

    void unregister(Inbox<?> inbox) {
        TypeUrl entityType = inbox.entityStateType();
        contents.remove(entityType.value());
    }
}

@@ -1151,23 +1151,21 @@
 ns.check
-NotNull
+State
 ;%0Aimport
@@ -1233,30 +1233,122 @@
 %0A * 
-@author Alex Tymchenko
+The container for the %7B@linkplain ShardedMessageDelivery message deliveries%7D for each%0A * %7B@link Inbox%7D registered.
 %0A */
@@ -1479,16 +1479,200 @@
 Map();%0A%0A
+    /**%0A     * Obtains the proper delivery for the given type URL of a message target.%0A     *%0A     * @throws IllegalStateException%0A     *         if there is no delivery found%0A     */%0A
     Shar
@@ -1820,22 +1820,28 @@
 heck
-NotNull(result
+State(result != null
 ,%0A  
@@ -1861,64 +1861,59 @@
     
-  %22The delivery is not registered for the Inbox of
+%22Cannot find the registered Inbox for the
  type 
+URL 
 %60%25s%60
@@ -1952,24 +1952,228 @@
 ult;%0A    %7D%0A%0A
+    /**%0A     * Obtains the proper delivery for the message passed according to the type URL of its target.%0A     *%0A     * @throws IllegalStateException%0A     *         if there is no delivery found%0A     */%0A
     ShardedM
@@ -2350,32 +2350,90 @@
 ypeUrl);%0A    %7D%0A%0A
+    /**%0A     * Registers the given %7B@code Inbox%7D.%0A     */%0A
     void registe
@@ -2569,24 +2569,162 @@
 ());%0A    %7D%0A%0A
+    /**%0A     * Unregisters the given %7B@code Inbox%7D.%0A     *%0A     * %3Cp%3EIf there was no such %7B@code Inbox%7D registered, does nothing.%0A     */%0A
     void unr

Document the delivery container.