/*
 * 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> valueValidator;
    private List<PropertyChangeListener> subscribers;

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

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

        this.valueValidator = valueValidator;

        this.subscribers = Lists.newArrayList();
    }

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

    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> getValueValidator() {
        return valueValidator;
    }

    public T getDefaultValue() {
        return defaultValue;
    }

    public T getValue() {
        return value;
    }

    public boolean setValue(T value) {
        if (!valueValidator.validate(value))
            return false;

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

        this.value = value;

        dispatchChangedEvent(event);

        return true;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }
}

@@ -1191,29 +1191,24 @@
 dator%3CT%3E val
-ueVal
 idator;%0A    
@@ -1258,16 +1258,112 @@
 ibers;%0A%0A
+    public Setting(SimpleUri id, T defaultValue) %7B%0A        this(id, defaultValue, null);%0A    %7D%0A%0A
     publ
@@ -1427,29 +1427,24 @@
 dator%3CT%3E val
-ueVal
 idator) %7B%0A  
@@ -1460,24 +1460,191 @@
 s.id = id;%0A%0A
+        this.validator = validator;%0A%0A        if (!validate(defaultValue))%0A            throw new IllegalArgumentException(%22The default value must be a valid value.%22);%0A%0A
         this
@@ -1718,55 +1718,8 @@
 e;%0A%0A
-        this.valueValidator = valueValidator;%0A%0A
     
@@ -1956,32 +1956,142 @@
        %7D%0A    %7D%0A%0A
+    private boolean validate(T value) %7B%0A        return validator == null %7C%7C validator.validate(value);%0A    %7D%0A%0A
     public void 
@@ -2468,21 +2468,16 @@
 %3E getVal
-ueVal
 idator()
@@ -2497,21 +2497,16 @@
 turn val
-ueVal
 idator;%0A
@@ -2691,21 +2691,16 @@
 if (!val
-ueVal
 idator.v

Add ctor to Setting, rename to validator