<commit_before>/*
 * Copyright 2020, 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.protobuf;

import com.google.protobuf.EnumValue;
import com.google.protobuf.ProtocolMessageEnum;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.spine.util.Exceptions.newIllegalArgumentException;

/**
 * Handles conversion of Java {@link Enum} objects to respective {@link EnumValue} Protobuf
 * counterpart.
 *
 * <p>Enums are converted by their {@linkplain EnumValue#getName() name} first or by the
 * {@linkplain EnumValue#getNumber() number} if the name is not present.
 */
final class EnumConverter extends ProtoConverter<EnumValue, Enum<? extends ProtocolMessageEnum>> {

    private final Class<? extends Enum<? extends ProtocolMessageEnum>> type;

    /**
     * Creates a new converter for the specified {@code type}.
     */
    EnumConverter(Class<? extends Enum<? extends ProtocolMessageEnum>> type) {
        super();
        this.type = checkNotNull(type);
    }

    @Override
    protected Enum<? extends ProtocolMessageEnum> toObject(EnumValue input) {
        String name = input.getName();
        if (name.isEmpty()) {
            int number = input.getNumber();
            return findByNumber(number);
        } else {
            return findByName(name);
        }
    }

    /**
     * Retrieves the enum constant with the specified {@code number}.
     *
     * @throws IllegalArgumentException
     *         if enum constant with such a number is not present
     */
    private Enum<? extends ProtocolMessageEnum> findByNumber(int number) {
        Enum<? extends ProtocolMessageEnum>[] constants = type.getEnumConstants();
        for (Enum<? extends ProtocolMessageEnum> constant : constants) {
            ProtocolMessageEnum asProtoEnum = (ProtocolMessageEnum) constant;
            int valueNumber = asProtoEnum.getNumber();
            if (number == valueNumber) {
                return constant;
            }
        }<commit_msg>Skip `UNRECOGNIZED` enums while iterating over values for an enum `number`.<commit_after>/*
 * Copyright 2020, 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.protobuf;

import com.google.protobuf.EnumValue;
import com.google.protobuf.ProtocolMessageEnum;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.spine.util.Exceptions.newIllegalArgumentException;

/**
 * Handles conversion of Java {@link Enum} objects to respective {@link EnumValue} Protobuf
 * counterpart.
 *
 * <p>Enums are converted by their {@linkplain EnumValue#getName() name} first or by the
 * {@linkplain EnumValue#getNumber() number} if the name is not present.
 */
final class EnumConverter extends ProtoConverter<EnumValue, Enum<? extends ProtocolMessageEnum>> {

    /**
     * A special name of a Protobuf enum entry that denotes an unrecognized value.
     */
    private static final String UNRECOGNIZED_PROTO_ENUM = "UNRECOGNIZED";

    private final Class<? extends Enum<? extends ProtocolMessageEnum>> type;

    /**
     * Creates a new converter for the specified {@code type}.
     */
    EnumConverter(Class<? extends Enum<? extends ProtocolMessageEnum>> type) {
        super();
        this.type = checkNotNull(type);
    }

    @Override
    protected Enum<? extends ProtocolMessageEnum> toObject(EnumValue input) {
        String name = input.getName();
        if (name.isEmpty()) {
            int number = input.getNumber();
            return findByNumber(number);
        } else {
            return findByName(name);
        }
    }

    /**
     * Retrieves the enum constant with the specified {@code number}.
     *
     * @throws IllegalArgumentException
     *         if enum constant with such a number is not present
     */
    private Enum<? extends ProtocolMessageEnum> findByNumber(int number) {
        Enum<? extends ProtocolMessageEnum>[] constants = type.getEnumConstants();
        for (Enum<? extends ProtocolMessageEnum> constant : constants) {
            if (UNRECOGNIZED_PROTO_ENUM.equalsIgnoreCase(constant.name())){
                continue;
            }
            ProtocolMessageEnum asProtoEnum = (ProtocolMessageEnum) constant;
            int valueNumber = asProtoEnum.getNumber();
            if (number == valueNumber) {
                return constant;
            }
        }
{'fraction_non_alphanumeric': 0.08117195004803074, 'fraction_numerical': 0.001280819724623759, 'mean_word_length': 3.3053066850447967, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '6812251', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1517, 'n_words': 622}