<commit_before>/*
 * Copyright 2017, 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.storage.jdbc;

import com.google.common.collect.ImmutableMap;

import java.util.EnumMap;
import java.util.Map;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Strings.isNullOrEmpty;
import static io.spine.server.storage.jdbc.Type.ID;

/**
 * A {@link Type}-to-name mapping.
 *
 * <p>A data type may have different names in different databases.
 * E.g. for binary data {@code BLOB} is used in MySQL, but in PostgreSQL it's {@code BYTEA}.
 *
 * <p>This class provides a flexible way to point out
 * database specific names of {@linkplain Type types}.
 *
 * <p>A mapping should provide names for all types except the {@link Type#ID ID}.
 *
 * <p>There are predefined {@linkplain TypeMappings mappings}.
 *
 * @author <NAME>
 */
public class TypeMapping {

    private final Map<Type, String> mappedTypes;

    private TypeMapping(Builder builder) {
        final ImmutableMap<Type, String> typesFromBuilder = builder.types.build();
        final int typesCountWithoutId = Type.values().length - 1;
        checkState(typesFromBuilder.size() == typesCountWithoutId,
                   "A mapping should contain names for all types except Type.ID (%s), " +
                   "but only (%s) types were mapped.", typesCountWithoutId, typesFromBuilder.size());
        mappedTypes = new EnumMap<>(typesFromBuilder);
    }

    /**
     * Obtains the name of the specified {@link Type}.
     *
     * @param type the type to get the name
     * @return the type name
     * @throws IllegalStateException if the name for the specified type is not defined
     */
    public String getTypeName(Type type) {
        checkState(mappedTypes.containsKey(type),
                   "The type mapping doesn't define name for %s type.");
        final String name = mappedTypes.get(type);
        return name;
    }

    /**<commit_msg>Add a missing argument to the error message<commit_after>/*
 * Copyright 2017, 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.storage.jdbc;

import com.google.common.collect.ImmutableMap;

import java.util.EnumMap;
import java.util.Map;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Strings.isNullOrEmpty;
import static io.spine.server.storage.jdbc.Type.ID;

/**
 * A {@link Type}-to-name mapping.
 *
 * <p>A data type may have different names in different databases.
 * E.g. for binary data {@code BLOB} is used in MySQL, but in PostgreSQL it's {@code BYTEA}.
 *
 * <p>This class provides a flexible way to point out
 * database specific names of {@linkplain Type types}.
 *
 * <p>A mapping should provide names for all types except the {@link Type#ID ID}.
 *
 * <p>There are predefined {@linkplain TypeMappings mappings}.
 *
 * @author <NAME>
 */
public class TypeMapping {

    private final Map<Type, String> mappedTypes;

    private TypeMapping(Builder builder) {
        final ImmutableMap<Type, String> typesFromBuilder = builder.types.build();
        final int typesCountWithoutId = Type.values().length - 1;
        checkState(typesFromBuilder.size() == typesCountWithoutId,
                   "A mapping should contain names for all types except Type.ID (%s), " +
                   "but only (%s) types were mapped.", typesCountWithoutId, typesFromBuilder.size());
        mappedTypes = new EnumMap<>(typesFromBuilder);
    }

    /**
     * Obtains the name of the specified {@link Type}.
     *
     * @param type the type to get the name
     * @return the type name
     * @throws IllegalStateException if the name for the specified type is not defined
     */
    public String getTypeName(Type type) {
        checkState(mappedTypes.containsKey(type),
                   "The type mapping doesn't define name for %s type.", type);
        final String name = mappedTypes.get(type);
        return name;
    }

    /**
{'fraction_non_alphanumeric': 0.08618399202922616, 'fraction_numerical': 0.0016605778811026237, 'mean_word_length': 3.5048616305160807, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '3315570', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1561, 'n_words': 668}