Add ctor to Setting, rename to validator
* Constructor validates the default value and throws an exception if
it is an invalid value.
private String name;
private String description;
- private SettingValueValidator<T> valueValidator;
+ private SettingValueValidator<T> validator;
private List<PropertyChangeListener> subscribers;
- public Setting(SimpleUri id, T defaultValue, SettingValueValidator<T> valueValidator) {
+ 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.valueValidator = valueValidator;
-
this.subscribers = Lists.newArrayList();
}
@@ -54,6 +61,10 @@ private void dispatchChangedEvent(PropertyChangeEvent event) {
}
}
+ private boolean validate(T value) {
+ return validator == null || validator.validate(value);
+ }
+
public void subscribe(PropertyChangeListener listener) {
subscribers.add(listener);
}
@@ -70,8 +81,8 @@ public SimpleUri getId() {
return id;
}
- public SettingValueValidator<T> getValueValidator() {
- return valueValidator;
+ public SettingValueValidator<T> getValidator() {
+ return validator;
}
public T getDefaultValue() {
@@ -83,7 +94,7 @@ public T getValue() {
}
public boolean setValue(T value) {
- if (!valueValidator.validate(value))
+ if (!validator.validate(value))
return false;
PropertyChangeEvent event = new PropertyChangeEvent(this, id.toString(), this.value, value);
