<commit_before>/*
 * Copyright 2017 MovingBlocks
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.terasology.config.flexible;

import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.config.flexible.validators.SettingValueValidator;
import org.terasology.engine.SimpleUri;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.MessageFormat;
import java.util.Set;

/**
 * {@inheritDoc}
 *
 * @param <T> The type of the value this {@link SettingImpl} contains.
 */
public final class SettingImpl<T> implements Setting<T> {
    private static final Logger LOGGER = LoggerFactory.getLogger(SettingImpl.class);
    private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();

    private final SimpleUri id;
    private final String warningFormatString;

    private final T defaultValue;
    private final Class<T> valueClass;

    private T value;

    private String humanReadableName;

    private String description;
    private SettingValueValidator<T> validator;
    private Set<PropertyChangeListener> subscribers;

    /**
     * Creates a new {@link SettingImpl} with the given id and default value but no validator.
     *
     * @param id           the id of the setting.
     * @param defaultValue the default value of the setting.
     */
    public SettingImpl(Class<T> valueClass, SimpleUri id, T defaultValue) {
        this(valueClass, id, defaultValue, null);
    }

    /**
     * Creates a new {@link SettingImpl} with the given id, default value and validator.
     *
     * @param id           the id of the setting.
     * @param defaultValue the default value of the setting.
     * @param validator    the validator to be used to validate values.
     */
    public SettingImpl(Class<T> valueClass, SimpleUri id, T defaultValue, SettingValueValidator<T> validator) {
        this.id = id;
        this.warningFormatString = MessageFormat.format("Setting {0}: '{'0}'", this.id);

        this.validator = validator;

        if (!validate(defaultValue)) {
            throw new IllegalArgumentException("The default value must be a valid value. " +
                    "Check the logs for more information.");
        }

        this.defaultValue = defaultValue;
        this.value = this.defaultValue;
        this.valueClass = valueClass;
    }

    private void dispatchChangedEvent(PropertyChangeEvent event) {
        if (subscribers != null) {
            for (PropertyChangeListener subscriber : subscribers) {
                subscriber.propertyChange(event);
            }
        }
    }

    private boolean validate(T valueToValidate) {
        return validator == null || validator.validate(valueToValidate);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean subscribe(PropertyChangeListener listener) {
        if (subscribers == null) {
            subscribers = Sets.newHashSet();
        }

        if (listener == null) {
            LOGGER.warn(MessageFormat.format(this.warningFormatString,
                    "A null subscriber cannot be added"));

            return false;
        }
        if (subscribers.contains(listener)) {
            LOGGER.warn(MessageFormat.format(this.warningFormatString,
                    "The listener has already been subscribed"));

            return false;
        }

        subscribers.add(listener);

        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean unsubscribe(PropertyChangeListener listener) {
        if (!subscribers.contains(listener)) {
            LOGGER.warn(MessageFormat.format(this.warningFormatString,
                    "The listener does not exist in the subscriber list"), id);
            return false;
        }<commit_msg>Add warning logging methods to SettingImpl<commit_after>/*
 * Copyright 2017 MovingBlocks
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.terasology.config.flexible;

import com.google.common.base.Preconditions;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.config.flexible.validators.SettingValueValidator;
import org.terasology.engine.SimpleUri;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.MessageFormat;
import java.util.Set;

/**
 * {@inheritDoc}
 *
 * @param <T> The type of the value this {@link SettingImpl} contains.
 */
public final class SettingImpl<T> implements Setting<T> {
    private static final Logger LOGGER = LoggerFactory.getLogger(SettingImpl.class);
    private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();

    private final SimpleUri id;
    private final String warningFormatString;

    private final T defaultValue;
    private final Class<T> valueClass;

    private T value;

    private String humanReadableName;

    private String description;
    private SettingValueValidator<T> validator;
    private Set<PropertyChangeListener> subscribers;

    /**
     * Creates a new {@link SettingImpl} with the given id and default value but no validator.
     *
     * @param id           the id of the setting.
     * @param defaultValue the default value of the setting.
     */
    public SettingImpl(Class<T> valueClass, SimpleUri id, T defaultValue) {
        this(valueClass, id, defaultValue, null);
    }

    /**
     * Creates a new {@link SettingImpl} with the given id, default value and validator.
     *
     * @param id           the id of the setting.
     * @param defaultValue the default value of the setting.
     * @param validator    the validator to be used to validate values.
     */
    public SettingImpl(Class<T> valueClass, SimpleUri id, T defaultValue, SettingValueValidator<T> validator) {
        this.id = id;
        this.warningFormatString = MessageFormat.format("Setting {0}: '{'0}'", this.id);

        this.validator = validator;

        if (!validate(defaultValue)) {
            throw new IllegalArgumentException("The default value must be a valid value. " +
                    "Check the logs for more information.");
        }

        this.defaultValue = defaultValue;
        this.value = this.defaultValue;
        this.valueClass = valueClass;
    }

    private String formatWarning(String s) {
        return MessageFormat.format(warningFormatString, s);
    }

    private void logWarning(String warningMessage) {
        LOGGER.warn(formatWarning(warningMessage));
    }

    private void dispatchChangedEvent(PropertyChangeEvent event) {
        if (subscribers != null) {
            for (PropertyChangeListener subscriber : subscribers) {
                subscriber.propertyChange(event);
            }
        }
    }

    private boolean validate(T valueToValidate) {
        return validator == null || validator.validate(valueToValidate);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean subscribe(PropertyChangeListener listener) {
        if (subscribers == null) {
            subscribers = Sets.newHashSet();
        }

        if (listener == null) {
            logWarning("A null subscriber cannot be added.");

            return false;
        }
        if (subscribers.contains(listener)) {
            logWarning("The listener has already been subscribed.");

            return false;
        }

        subscribers.add(listener);

        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean unsubscribe(PropertyChangeListener listener) {
        if (!subscribers.contains(listener)) {
            LOGGER.warn("The listener does not exist in the subscriber list.");
            return false;
        }
{'fraction_non_alphanumeric': 0.0882744836775483, 'fraction_numerical': 0.002664890073284477, 'mean_word_length': 2.7796894670583296, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6203273', 'n_tokens_mistral': 2381, 'n_tokens_neox': 2315, 'n_words': 755}