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

import com.google.protobuf.Any;
import io.spine.base.EnrichmentMessage;
import io.spine.core.Enrichment.Container;
import io.spine.core.Enrichment.ModeCase;
import io.spine.type.TypeName;

import java.util.Map;
import java.util.Optional;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.spine.protobuf.AnyPacker.unpack;

/**
 * Utility class for working with event enrichments.
 */
public final class Enrichments {

    /** Prevents instantiation of this utility class. */
    private Enrichments() {
    }

    /**
     * Returns all enrichments from the context.
     *
     * @param context a context to get enrichments from
     * @return an optional of enrichments
     * @deprecated please use {@link EventContext#find(Class)}
     */
    @Deprecated
    public static Optional<Container> getEnrichments(EventContext context) {
        checkNotNull(context);
        Enrichment enrichment = context.getEnrichment();
        return container(enrichment);
    }

    /**
     * Obtains the container of enrichments from the passed enclosing instance,
     * if it its {@link ModeCase} allows for having enrichments.
     *
     * <p>Otherwise, empty {@code Optional} is returned.
     */
    static Optional<Container> container(Enrichment enrichment) {
        if (enrichment.getModeCase() == ModeCase.CONTAINER) {
            return Optional.of(enrichment.getContainer());
        }
        return Optional.empty();
    }

    /**
     * Return a specific enrichment from the context.
     *
     * @param  enrichmentClass a class of the event enrichment
     * @param  context         a context to get an enrichment from
     * @param  <E>             a type of the event enrichment
     * @return an optional of the enrichment
     * @deprecated please use {@link EventContext#find(Class)}
     */
    @Deprecated
    public static <E extends EnrichmentMessage>
    Optional<E> getEnrichment(Class<E> enrichmentClass, EventContext context) {
        checkNotNull(enrichmentClass);
        checkNotNull(context);
        Optional<E> result = context.find(enrichmentClass);
        return result;
    }

    /**
     * Obtains enrichment from the passed container.
     */
    static <E extends EnrichmentMessage>
    Optional<E> find(Class<E> enrichmentClass, Container enrichments) {
        String typeName = TypeName.of(enrichmentClass)
                                  .value();
        Any any = enrichments.getItemsMap()
                             .get(typeName);
        Optional<E> result = Optional.ofNullable(any)
                                     .map(packed -> unpack(packed, enrichmentClass));
        return result;
    }

    /**
     * Creates a new {@link Enrichment} instance from the passed map.
     */
    static Enrichment createEnrichment(Map<String, Any> enrichments) {
        Enrichment.Builder enrichment =
                Enrichment.newBuilder()
                          .setContainer(Container.newBuilder()
                                                 .putAllItems(enrichments));
        return enrichment.build();
    }
}

@@ -1273,73 +1273,8 @@
 l;%0A%0A
-import static com.google.common.base.Preconditions.checkNotNull;%0A
 impo
@@ -1381,23 +1381,16 @@
 ts.%0A */%0A
-public 
 final cl
@@ -1519,1586 +1519,446 @@
   * 
-Returns all enrichments from the context.%0A     *%0A     * @param context a context to get enrichments from%0A     * @return an optional of enrichments%0A     * @deprecated please use %7B@link EventContext#find(Class)%7D%0A     */%0A    @Deprecated%0A    public static Optional%3CContainer%3E getEnrichments(EventContext context) %7B%0A        checkNotNull(context);%0A        Enrichment enrichment = context.getEnrichment();%0A        return container(enrichment);%0A    %7D%0A%0A    /**%0A     * Obtains the container of enrichments from the passed enclosing instance,%0A     * if it its %7B@link ModeCase%7D allows for having enrichments.%0A     *%0A     * %3Cp%3EOtherwise, empty %7B@code Optional%7D is returned.%0A     */%0A    static Optional%3CContainer%3E container(Enrichment enrichment) %7B%0A        if (enrichment.getModeCase() == ModeCase.CONTAINER) %7B%0A            return Optional.of(enrichment.getContainer());%0A        %7D%0A        return Optional.empty();%0A    %7D%0A%0A    /**%0A     * Return a specific enrichment from the context.%0A     *%0A     * @param  enrichmentClass a class of the event enrichment%0A     * @param  context         a context to get an enrichment from%0A     * @param  %3CE%3E             a type of the event enrichment%0A     * @return an optional of the enrichment%0A     * @deprecated please use %7B@link EventContext#find(Class)%7D%0A     */%0A    @Deprecated%0A    public static %3CE extends EnrichmentMessage%3E%0A    Optional%3CE%3E getEnrichment(Class%3CE%3E enrichmentClass, EventContext context) %7B%0A        checkNotNull(enrichmentClass);%0A        checkNotNull(context);%0A        Optional%3CE%3E result = context.find(enrichmentClass);%0A        return result
+Obtains the container of enrichments from the passed enclosing instance,%0A     * if it its %7B@link ModeCase%7D allows for having enrichments.%0A     *%0A     * %3Cp%3EOtherwise, empty %7B@code Optional%7D is returned.%0A     */%0A    static Optional%3CContainer%3E container(Enrichment enrichment) %7B%0A        if (enrichment.getModeCase() == ModeCase.CONTAINER) %7B%0A            return Optional.of(enrichment.getContainer());%0A        %7D%0A        return Optional.empty()
 ;%0A  

Remove deprecated methods