@@ -0,0 +1,3339 @@
+/*%0A * Copyright 2022, TeamDev. All rights reserved.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Redistribution and use in source and/or binary forms, with or without%0A * modification, must retain the above copyright notice and the following%0A * disclaimer.%0A *%0A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS%0A * %22AS IS%22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT%0A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR%0A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT%0A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,%0A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT%0A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,%0A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY%0A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT%0A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE%0A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.%0A */%0A%0Apackage io.spine.server;%0A%0Aimport com.google.common.collect.ImmutableMap;%0Aimport io.spine.type.TypeUrl;%0A%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.Optional;%0Aimport java.util.Set;%0Aimport java.util.function.Function;%0A%0Aimport static com.google.common.base.Preconditions.checkNotNull;%0A%0A/**%0A * Allows to find to which Bounded Context a given type belongs.%0A */%0Afinal class TypeDictionary %7B%0A%0A    private final ImmutableMap%3CTypeUrl, BoundedContext%3E map;%0A%0A    private TypeDictionary(Builder builder) %7B%0A        this.map = ImmutableMap.copyOf(builder.map);%0A    %7D%0A%0A    /**%0A     * Creates a new builder for a type dictionary.%0A     */%0A    static Builder newBuilder() %7B%0A        return new Builder();%0A    %7D%0A%0A    /**%0A     * Obtains a bounded context to which the given type belongs.%0A     *%0A     * @param type%0A     *         the type to search%0A     * @return the bounded context or empty %7B@code Optional%7D%0A     */%0A    Optional%3CBoundedContext%3E find(TypeUrl type) %7B%0A        var result = Optional.ofNullable(map.get(type));%0A        return result;%0A    %7D%0A%0A    /**%0A     * A builder for new type dictionary.%0A     */%0A    static final class Builder %7B%0A%0A        private final Map%3CTypeUrl, BoundedContext%3E map = new HashMap%3C%3E();%0A%0A        /**%0A         * Obtains the types from the given context and associates them with it.%0A         *%0A         * @param context%0A         *         the bounded context to query for the types%0A         * @param typeSupplier%0A         *         the function obtaining the types from the given instance of the context%0A         * @return this builder instance%0A         */%0A        Builder putAll(BoundedContext context,%0A                       Function%3CBoundedContext, Set%3CTypeUrl%3E%3E typeSupplier) %7B%0A            checkNotNull(context);%0A            checkNotNull(typeSupplier);%0A            var types = typeSupplier.apply(context);%0A            types.forEach(type -%3E map.put(type, context));%0A            return this;%0A        %7D%0A%0A        /**%0A         * Creates new type dictionary.%0A         */%0A        TypeDictionary build() %7B%0A            return new TypeDictionary(this);%0A        %7D%0A    %7D%0A%7D%0A

Introduce type dictionary