Lazy create and delete Setting subscriber list
* The subscriber list is initialized only when the first subscriber is
added and is deleted (set to null) when the last one is removed.
@@ -52,7 +52,7 @@ public Setting(SimpleUri id, T defaultValue, SettingValueValidator<T> validator)
this.defaultValue = defaultValue;
this.value = this.defaultValue;
- this.subscribers = Lists.newArrayList();
+ this.subscribers = null;
}
private void dispatchChangedEvent(PropertyChangeEvent event) {
@@ -66,11 +66,19 @@ private boolean validate(T 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() {
