/*
 * 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.");
        }

        Preconditions.checkNotNull(defaultValue, formatWarning("The default value cannot be null."));

        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;
        }

        subscribers.remove(listener);

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

        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean hasSubscribers() {
        return subscribers != null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public SimpleUri getId() {
        return id;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public SettingValueValidator<T> getValidator() {
        return validator;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public T getDefaultValue() {
        return defaultValue;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public T getValue() {
        return value;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean setValue(T newValue) {
        Preconditions.checkNotNull(newValue, formatWarning("The value of a setting cannot be null."));

        if (!validate(newValue)) {
            return false;
        }

        PropertyChangeEvent event = new PropertyChangeEvent(this, id.toString(), this.value, newValue);
        this.value = newValue;
        dispatchChangedEvent(event);

        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getHumanReadableName() {
        return humanReadableName;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getDescription() {
        return description;
    }

    @Override
    public void setValueFromJson(String json) {
        value = GSON.fromJson(json, valueClass);
    }

    @Override
    public JsonElement getValueAsJson() {
        return GSON.toJsonTree(value);
    }
}

@@ -3142,16 +3142,63 @@
 ueClass;
+%0A%0A        this.subscribers = Sets.newHashSet();
 %0A    %7D%0A%0A
@@ -3493,47 +3493,8 @@
 ) %7B%0A
-        if (subscribers != null) %7B%0A    
     
@@ -3557,36 +3557,32 @@
 ) %7B%0A            
-    
 subscriber.prope
@@ -3603,22 +3603,8 @@
 t);%0A
-            %7D%0A
     
@@ -3865,99 +3865,8 @@
 ) %7B%0A
-        if (subscribers == null) %7B%0A            subscribers = Sets.newHashSet();%0A        %7D%0A%0A
     
@@ -4533,90 +4533,8 @@
 );%0A%0A
-        if (subscribers.size() %3C= 0) %7B%0A            subscribers = null;%0A        %7D%0A%0A
     
@@ -4676,16 +4676,19 @@
 bers
- != null
+.size() %3E 0
 ;%0A  

Remove null usage for subscriber absence in SettingImpl