<commit_before>/*
 * Copyright 2016 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.Lists;
import org.terasology.config.flexible.validators.SettingValueValidator;
import org.terasology.engine.SimpleUri;
import org.terasology.utilities.subscribables.GeneralSubscribable;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

public class Setting<T> implements GeneralSubscribable {
    private final T defaultValue;

    private SimpleUri id;

    private T value;

    private String name;

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

    public Setting(SimpleUri id, T defaultValue) {
        this(id, defaultValue, null);
    }

    public Setting(SimpleUri id, T defaultValue, SettingValueValidator<T> validator) {
        this.id = id;

        this.validator = validator;

        if (!validate(defaultValue))
            throw new IllegalArgumentException("The default value must be a valid value.");

        this.defaultValue = defaultValue;
        this.value = this.defaultValue;

        this.subscribers = Lists.newArrayList();
    }

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

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

    public void subscribe(PropertyChangeListener listener) {
        subscribers.add(listener);
    }

    public void unsubscribe(PropertyChangeListener listener) {
        subscribers.remove(listener);
    }

    public boolean hasSubscribers() {
        return !subscribers.isEmpty();
    }

    public SimpleUri getId() {
        return id;
    }

    public SettingValueValidator<T> getValidator() {
        return validator;
    }

    public T getDefaultValue() {<commit_msg>Lazy create and delete Setting subscriber list<commit_after>/*
 * Copyright 2016 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.Lists;
import org.terasology.config.flexible.validators.SettingValueValidator;
import org.terasology.engine.SimpleUri;
import org.terasology.utilities.subscribables.GeneralSubscribable;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

public class Setting<T> implements GeneralSubscribable {
    private final T defaultValue;

    private SimpleUri id;

    private T value;

    private String name;

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

    public Setting(SimpleUri id, T defaultValue) {
        this(id, defaultValue, null);
    }

    public Setting(SimpleUri id, T defaultValue, SettingValueValidator<T> validator) {
        this.id = id;

        this.validator = validator;

        if (!validate(defaultValue))
            throw new IllegalArgumentException("The default value must be a valid value.");

        this.defaultValue = defaultValue;
        this.value = this.defaultValue;

        this.subscribers = null;
    }

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

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

    public void subscribe(PropertyChangeListener listener) {
        if (subscribers == null) {
            subscribers = Lists.newArrayList();
        }

        subscribers.add(listener);
    }

    public void unsubscribe(PropertyChangeListener listener) {
        subscribers.remove(listener);

        if (subscribers.size() <= 0) {
            subscribers = null;
        }
    }

    public boolean hasSubscribers() {
        return !subscribers.isEmpty();
    }

    public SimpleUri getId() {
        return id;
    }

    public SettingValueValidator<T> getValidator() {
        return validator;
    }

    public T getDefaultValue() {
{'fraction_non_alphanumeric': 0.0798280052346233, 'fraction_numerical': 0.0031781641428304357, 'mean_word_length': 3.232594936708861, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 13, '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': '4669501', 'n_tokens_mistral': 1419, 'n_tokens_neox': 1354, 'n_words': 430}