<commit_before>/*
 * Copyright 2018, TeamDev Ltd. 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.integration;

import com.google.protobuf.Any;
import com.google.protobuf.Message;
import com.google.protobuf.StringValue;
import io.spine.Identifier;
import io.spine.annotation.Internal;
import io.spine.core.BoundedContextName;
import io.spine.core.Command;
import io.spine.core.Event;
import io.spine.core.Rejection;
import io.spine.protobuf.AnyPacker;
import io.spine.server.delivery.ShardedMessage;
import io.spine.server.delivery.ShardedMessageId;

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

/**
 * Utilities for working with {@linkplain ExternalMessage external messages}.
 *
 * @author <NAME>
 */
@Internal
public final class ExternalMessages {

    /** Prevents instantiation on this utility class. */
    private ExternalMessages() {}

    /**
     * Wraps the instance of {@link Event} into an {@code ExternalMessage}.
     *
     * @param event              the event to wrap
     * @param boundedContextName the name of the bounded context in which the event was created
     * @return the external message wrapping the given event
     */
    public static ExternalMessage of(Event event, BoundedContextName boundedContextName) {
        checkNotNull(event);
        checkNotNull(boundedContextName);

        final ExternalMessage result = of(event.getId(), event, boundedContextName);
        return result;
    }

    public static ExternalMessage of(Command command, BoundedContextName boundedContextName) {
        checkNotNull(command);
        checkNotNull(boundedContextName);

        final ExternalMessage result = of(command.getId(), command, boundedContextName);
        return result;

    }

    /**
     * Wraps the instance of {@link Rejection} into an {@code ExternalMessage}.
     *
     * @param rejection          the rejection to wrap
     * @param boundedContextName the name of bounded context in which the rejection was created
     * @return the external message wrapping the given rejection
     */
    static ExternalMessage of(Rejection rejection, BoundedContextName boundedContextName) {
        checkNotNull(rejection);
        checkNotNull(boundedContextName);

        final ExternalMessage result = of(rejection.getId(), rejection, boundedContextName);
        return result;
    }

    /**
     * Wraps the instance of {@link RequestForExternalMessages} into an {@code ExternalMessage}.
     *
     * @param request            the request to wrap
     * @param boundedContextName the name of bounded context in which the request was created
     * @return the external message wrapping the given request
     */
    static ExternalMessage of(RequestForExternalMessages request,
                              BoundedContextName boundedContextName) {
        checkNotNull(request);
        checkNotNull(boundedContextName);

        final String idString = Identifier.newUuid();
        final ExternalMessage result = of(StringValue.newBuilder()
                                                     .setValue(idString)
                                                     .build(),
                                          request,
                                          boundedContextName);
        return result;
    }

    public static ExternalMessage of(ShardedMessage message,
                                     BoundedContextName boundedContextName) {
        checkNotNull(message);
        checkNotNull(boundedContextName);

        final ShardedMessageId id = message.getId();
        final ExternalMessage result = of(id, message, boundedContextName);
        return result;
    }

    public static ShardedMessage asShardedMessage(ExternalMessage value) {
        checkNotNull(value);
        final Any originalMessage = value.getOriginalMessage();
        final ShardedMessage result = AnyPacker.unpack(originalMessage);
        return result;
    }

    private static ExternalMessage of(Message messageId,
                                      Message message,
                                      BoundedContextName boundedContextName) {
        final Any packedId = Identifier.pack(messageId);
        final Any packedMessage = AnyPacker.pack(message);

        final ExternalMessage result = ExternalMessage.newBuilder()
                                                      .setId(packedId)<commit_msg>Add the missing documentation and rename some of the parameters.<commit_after>/*
 * Copyright 2018, TeamDev Ltd. 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.integration;

import com.google.protobuf.Any;
import com.google.protobuf.Message;
import com.google.protobuf.StringValue;
import io.spine.Identifier;
import io.spine.annotation.Internal;
import io.spine.core.BoundedContextName;
import io.spine.core.Command;
import io.spine.core.Event;
import io.spine.core.Rejection;
import io.spine.protobuf.AnyPacker;
import io.spine.server.delivery.ShardedMessage;
import io.spine.server.delivery.ShardedMessageId;

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

/**
 * Utilities for working with {@linkplain ExternalMessage external messages}.
 *
 * @author <NAME>
 */
@Internal
public final class ExternalMessages {

    /** Prevents instantiation on this utility class. */
    private ExternalMessages() {}

    /**
     * Wraps the instance of {@link Event} into an {@code ExternalMessage}.
     *
     * @param event  the event to wrap
     * @param origin the name of the bounded context in which the event was created
     * @return the external message wrapping the given event
     */
    public static ExternalMessage of(Event event, BoundedContextName origin) {
        checkNotNull(event);
        checkNotNull(origin);

        final ExternalMessage result = of(event.getId(), event, origin);
        return result;
    }

    /**
     * Wraps the instance of {@link Command} into an {@code ExternalMessage}.
     *
     * @param command the command to wrap
     * @param origin  the name of the bounded context in which the command was created
     * @return the external message wrapping the given command
     */
    public static ExternalMessage of(Command command, BoundedContextName origin) {
        checkNotNull(command);
        checkNotNull(origin);

        final ExternalMessage result = of(command.getId(), command, origin);
        return result;
    }

    /**
     * Wraps the instance of {@link Rejection} into an {@code ExternalMessage}.
     *
     * @param rejection the rejection to wrap
     * @param origin    the name of bounded context in which the rejection was created
     * @return the external message wrapping the given rejection
     */
    static ExternalMessage of(Rejection rejection, BoundedContextName origin) {
        checkNotNull(rejection);
        checkNotNull(origin);

        final ExternalMessage result = of(rejection.getId(), rejection, origin);
        return result;
    }

    /**
     * Wraps the instance of {@link RequestForExternalMessages} into an {@code ExternalMessage}.
     *
     * @param request the request to wrap
     * @param origin  the name of bounded context in which the request was created
     * @return the external message wrapping the given request
     */
    static ExternalMessage of(RequestForExternalMessages request, BoundedContextName origin) {
        checkNotNull(request);
        checkNotNull(origin);

        final String idString = Identifier.newUuid();
        final ExternalMessage result = of(StringValue.newBuilder()
                                                     .setValue(idString)
                                                     .build(),
                                          request,
                                          origin);
        return result;
    }

    /**
     * Wraps the instance of {@link ShardedMessage} into an {@code ExternalMessage}.
     *
     * @param message the message to wrap
     * @param origin  the name of bounded context in which the request message created
     * @return the external message wrapping the given message
     */
    public static ExternalMessage of(ShardedMessage message, BoundedContextName origin) {
        checkNotNull(message);
        checkNotNull(origin);

        final ShardedMessageId id = message.getId();
        final ExternalMessage result = of(id, message, origin);
        return result;
    }

    /**
     * Unpacks the given {@code ExternalMessage} into a {@link ShardedMessage}.
     *
     * <p>Callees of this method should ensure that the given external message indeed wraps
     * an instance of {@code ShardedMessage}, before calling this method.
     * A {@link ClassCastException} is thrown otherwise.
     *
     * @param value the value to unpack
     * @return the unpacked value
     */
    public static ShardedMessage asShardedMessage(ExternalMessage value) {
        checkNotNull(value);
        final Any originalMessage = value.getOriginalMessage();
        final ShardedMessage result = AnyPacker.unpack(originalMessage);
        return result;
    }

    private static ExternalMessage of(Message messageId,
                                      Message message,
                                      BoundedContextName boundedContextName) {
        final Any packedId = Identifier.pack(messageId);
        final Any packedMessage = AnyPacker.pack(message);

        final ExternalMessage result = ExternalMessage.newBuilder()
                                                      .setId(packedId)
{'fraction_non_alphanumeric': 0.06959867098015214, 'fraction_numerical': 0.0006994841304537903, 'mean_word_length': 2.7028164454516026, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 25, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '259615', 'n_tokens_mistral': 2838, 'n_tokens_neox': 2625, 'n_words': 1012}