<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.delivery;

import com.google.common.base.MoreObjects;
import io.spine.core.BoundedContextName;
import io.spine.core.CommandEnvelope;
import io.spine.core.EventEnvelope;
import io.spine.core.MessageEnvelope;
import io.spine.core.RejectionEnvelope;
import io.spine.server.entity.EntityClass;

import java.util.Objects;

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

/**
 * @author <NAME>
 */
public final class DeliveryTag<E extends MessageEnvelope<?, ?, ?>> {

    private final BoundedContextName boundedContextName;
    private final EntityClass<?> entityClass;
    private final Class<E> envelopeType;

    private DeliveryTag(BoundedContextName boundedContextName,
                        EntityClass<?> entityClass,
                        Class<E> envelopeType) {
        this.boundedContextName = boundedContextName;
        this.entityClass = entityClass;
        this.envelopeType = envelopeType;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        final DeliveryTag<?> that = (DeliveryTag<?>) o;
        return Objects.equals(boundedContextName, that.boundedContextName) &&
                Objects.equals(entityClass, that.entityClass) &&
                Objects.equals(envelopeType, that.envelopeType);
    }

    @Override
    public int hashCode() {
        return Objects.hash(boundedContextName, entityClass, envelopeType);
    }

    public static DeliveryTag<CommandEnvelope> forCommandsOf(Shardable shardable) {
        checkNotNull(shardable);
        return forEnvelope(shardable.getBoundedContextName(),
                           shardable.getShardedModelClass(),
                           CommandEnvelope.class);
    }

    public static DeliveryTag<EventEnvelope> forEventsOf(Shardable shardable) {
        checkNotNull(shardable);
        return forEnvelope(shardable.getBoundedContextName(),
                           shardable.getShardedModelClass(),
                           EventEnvelope.class);
    }

    public static DeliveryTag<RejectionEnvelope> forRejectionsOf(Shardable shardable) {
        checkNotNull(shardable);
        return forEnvelope(shardable.getBoundedContextName(),
                           shardable.getShardedModelClass(),
                           RejectionEnvelope.class);
    }

    private static <E extends MessageEnvelope<?, ?, ?>> DeliveryTag<E>
    forEnvelope(BoundedContextName bcName, EntityClass<?> entityClass, Class<E> envelopeClass) {
        checkNotNull(bcName);
        checkNotNull(entityClass);
        checkNotNull(envelopeClass);

        final DeliveryTag<E> id = new DeliveryTag<>(bcName, entityClass, envelopeClass);
        return id;
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                          .add("BoundedContext name", boundedContextName)<commit_msg>Document the class.<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.delivery;

import com.google.common.base.MoreObjects;
import io.spine.core.BoundedContextName;
import io.spine.core.CommandEnvelope;
import io.spine.core.EventEnvelope;
import io.spine.core.MessageEnvelope;
import io.spine.core.RejectionEnvelope;
import io.spine.server.entity.EntityClass;

import java.util.Objects;

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

/**
 * A value, that defines the message destination in the {@linkplain Delivery delivery process}.
 *
 * <p>In the message delivery process, envelopes are travelling from some {@linkplain Sender sender}
 * through the {@linkplain ShardedStream sharded message stream} to a number of {@linkplain Consumer
 * consumers}. To identify which consumers, should receive the message, an instance of
 * {@code DeliveryTag} is used.
 *
 * <p>The value of the tag identifies the entity, which exists in scope of some bounded context and
 * declares the need to consume message envelopes of a specific type (such as
 * {@link CommandEnvelope}).
 *
 * @author <NAME>
 */
public final class DeliveryTag<E extends MessageEnvelope<?, ?, ?>> {

    private final BoundedContextName boundedContextName;
    private final EntityClass<?> entityClass;
    private final Class<E> envelopeType;

    private DeliveryTag(BoundedContextName boundedContextName,
                        EntityClass<?> entityClass,
                        Class<E> envelopeType) {
        this.boundedContextName = boundedContextName;
        this.entityClass = entityClass;
        this.envelopeType = envelopeType;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        final DeliveryTag<?> that = (DeliveryTag<?>) o;
        return Objects.equals(boundedContextName, that.boundedContextName) &&
                Objects.equals(entityClass, that.entityClass) &&
                Objects.equals(envelopeType, that.envelopeType);
    }

    @Override
    public int hashCode() {
        return Objects.hash(boundedContextName, entityClass, envelopeType);
    }

    /**
     * Creates an instance of {@code DeliveryTag} that identifies the need to deliver command
     * envelopes to entities that are sharded within the given {@code Shardable}.
     *
     * @param shardable the shardable, which entities declare the need in command envelopes
     * @return the new instance of {@code DeliveryTag}
     */
    public static DeliveryTag<CommandEnvelope> forCommandsOf(Shardable shardable) {
        checkNotNull(shardable);
        return forEnvelope(shardable.getBoundedContextName(),
                           shardable.getShardedModelClass(),
                           CommandEnvelope.class);
    }

    /**
     * Creates an instance of {@code DeliveryTag} that identifies the need to deliver event
     * envelopes to entities that are sharded within the given {@code Shardable}.
     *
     * @param shardable the shardable, which entities declare the need in event envelopes
     * @return the new instance of {@code DeliveryTag}
     */
    public static DeliveryTag<EventEnvelope> forEventsOf(Shardable shardable) {
        checkNotNull(shardable);
        return forEnvelope(shardable.getBoundedContextName(),
                           shardable.getShardedModelClass(),
                           EventEnvelope.class);
    }

    /**
     * Creates an instance of {@code DeliveryTag} that identifies the need to deliver rejection
     * envelopes to entities that are sharded within the given {@code Shardable}.
     *
     * @param shardable the shardable, which entities declare the need in command rejection
     * @return the new instance of {@code DeliveryTag}
     */
    public static DeliveryTag<RejectionEnvelope> forRejectionsOf(Shardable shardable) {
        checkNotNull(shardable);
        return forEnvelope(shardable.getBoundedContextName(),
                           shardable.getShardedModelClass(),
                           RejectionEnvelope.class);
    }

    private static <E extends MessageEnvelope<?, ?, ?>> DeliveryTag<E>
    forEnvelope(BoundedContextName bcName, EntityClass<?> entityClass, Class<E> envelopeClass) {
        checkNotNull(bcName);
        checkNotNull(entityClass);
        checkNotNull(envelopeClass);

        final DeliveryTag<E> id = new DeliveryTag<>(bcName, entityClass, envelopeClass);
        return id;
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                          .add("BoundedContext name", boundedContextName)
{'fraction_non_alphanumeric': 0.08009833043121992, 'fraction_numerical': 0.0008194202601659326, 'mean_word_length': 3.071726438698916, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, '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': '6742635', 'n_tokens_mistral': 2644, 'n_tokens_neox': 2462, 'n_words': 817}