<commit_before>/*
 * 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 <NAME>
 */
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());
    }
}<commit_msg>Document the delivery container.<commit_after>/*
 * 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.checkState;
import static com.google.common.collect.Maps.newConcurrentMap;

/**
 * The container for the {@linkplain ShardedMessageDelivery message deliveries} for each
 * {@link Inbox} registered.
 */
final class InboxDeliveries {

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

    /**
     * Obtains the proper delivery for the given type URL of a message target.
     *
     * @throws IllegalStateException
     *         if there is no delivery found
     */
    ShardedMessageDelivery<InboxMessage> get(String typeUrl) {
        ShardedMessageDelivery<InboxMessage> result = contents.get(typeUrl);
        checkState(result != null,
                   "Cannot find the registered Inbox for the type URL `%s`.", typeUrl);
        return result;
    }

    /**
     * Obtains the proper delivery for the message passed according to the type URL of its target.
     *
     * @throws IllegalStateException
     *         if there is no delivery found
     */
    ShardedMessageDelivery<InboxMessage> get(InboxMessage message) {
        String typeUrl = message.getInboxId()
                                .getTypeUrl();
        return get(typeUrl);
    }

    /**
     * Registers the given {@code Inbox}.
     */
    void register(Inbox<?> inbox) {
        TypeUrl entityType = inbox.entityStateType();
        contents.put(entityType.value(), inbox.delivery());
    }

    /**
     * Unregisters the given {@code Inbox}.
     *
     * <p>If there was no such {@code Inbox} registered, does nothing.
     */
    void unregister(Inbox<?> inbox) {
        TypeUrl entityType = inbox.entityStateType();
        contents.remove(entityType.value());
    }
}
{'fraction_non_alphanumeric': 0.08349628470864294, 'fraction_numerical': 0.001564333202972233, 'mean_word_length': 3.3201013513513513, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3872511', 'n_tokens_mistral': 1445, 'n_tokens_neox': 1322, 'n_words': 506}