<commit_before>/*
 * 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.code.proto.enrichment;

import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.Immutable;
import com.google.protobuf.Descriptors.FieldDescriptor;
import io.spine.code.proto.MessageType;

import java.util.Objects;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableBiMap.toImmutableBiMap;

/**
 * Matches fields an enrichment source to the fields of the enrichment.
 */
@Immutable
final class FieldMatch {

    /**
     * The source type of the enrichment.
     */
    private final MessageType sourceType;

    /**
     * The target type of the enrichment.
     */
    private final MessageType targetType;

    /**
     * Maps the descriptor of the enrichment source field, to the descriptor of
     * the enrichment field.
     */
    private final ImmutableBiMap<FieldDescriptor, FieldDescriptor> sourceToTarget;

    FieldMatch(MessageType sourceType, MessageType targetType, ImmutableList<FieldDef> fields) {
        checkNotNull(sourceType);
        checkNotNull(fields);
        checkArgument(
                !fields.isEmpty(),
                "Enrichment type (`%s`) must have at least one field.", targetType
        );
        this.sourceType = sourceType;
        this.targetType = targetType;
        this.sourceToTarget =
                fields.stream()
                      .collect(toImmutableBiMap(
                              f -> f.find(sourceType.descriptor()),
                              FieldDef::descriptor
                      ));
    }

    FieldDescriptor sourceOf(FieldDescriptor target) {
        FieldDescriptor source = sourceToTarget.inverse()
                                               .get(target);
        checkNotNull(source,
                     "Unable to find source field for the target field `%s`.",
                     target.getFullName());
        return source;
    }

    @Override
    public int hashCode() {
        return Objects.hash(sourceType, targetType, sourceToTarget);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof FieldMatch)) {
            return false;
        }
        final FieldMatch other = (FieldMatch) obj;
        return Objects.equals(sourceType, other.sourceType)
                && Objects.equals(targetType, other.targetType)
                && Objects.equals(sourceToTarget, other.sourceToTarget);
    }
}<commit_msg>Inverse field matching map<commit_after>/*
 * 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.code.proto.enrichment;

import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.Immutable;
import com.google.protobuf.Descriptors.FieldDescriptor;
import io.spine.code.proto.MessageType;

import java.util.Objects;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableBiMap.toImmutableBiMap;

/**
 * Matches fields an enrichment source to the fields of the enrichment.
 */
@Immutable
final class FieldMatch {

    /**
     * The source type of the enrichment.
     */
    private final MessageType sourceType;

    /**
     * The target type of the enrichment.
     */
    private final MessageType targetType;

    /**
     * Maps the descriptor of the enrichment field, to instruction of how to obtain the value
     * of the enrichment field.
     */
    private final ImmutableBiMap<FieldDescriptor, FieldDescriptor> targetToSource;

    FieldMatch(MessageType sourceType, MessageType targetType, ImmutableList<FieldDef> fields) {
        checkNotNull(sourceType);
        checkNotNull(fields);
        checkArgument(
                !fields.isEmpty(),
                "Enrichment type (`%s`) must have at least one field.", targetType
        );
        this.sourceType = sourceType;
        this.targetType = targetType;
        this.targetToSource =
                fields.stream()
                      .collect(toImmutableBiMap(
                              FieldDef::descriptor,
                              f -> f.find(sourceType.descriptor())
                      ));
    }

    FieldDescriptor sourceOf(FieldDescriptor target) {
        FieldDescriptor source = targetToSource.get(target);
        checkNotNull(source,
                     "Unable to find source field for the target field `%s`.",
                     target.getFullName());
        return source;
    }

    @Override
    public int hashCode() {
        return Objects.hash(sourceType, targetType, targetToSource);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof FieldMatch)) {
            return false;
        }
        final FieldMatch other = (FieldMatch) obj;
        return Objects.equals(sourceType, other.sourceType)
                && Objects.equals(targetType, other.targetType)
                && Objects.equals(targetToSource, other.targetToSource);
    }
}
{'fraction_non_alphanumeric': 0.07849315068493151, 'fraction_numerical': 0.001095890410958904, 'mean_word_length': 2.685512367491166, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6888971', 'n_tokens_mistral': 1945, 'n_tokens_neox': 1774, 'n_words': 626}