Remove null usage in SettingImpl.subscribers, Make field final
private String description;
private SettingConstraint<T> constraint;
- private Set<PropertyChangeListener> subscribers;
+ private final Set<PropertyChangeListener> subscribers = Sets.newHashSet();
/**
* Creates a new {@link SettingImpl} with the given id and default value but no constraint.
@@ -93,12 +93,10 @@ private String formatWarning(String s) {
}
private void dispatchChangedEvent(PropertyChangeEvent event) {
- if (subscribers != null) {
for (PropertyChangeListener subscriber : subscribers) {
subscriber.propertyChange(event);
}
}
- }
private boolean isConstraintUnsatisfiedBy(T theValue) {
if (constraint == null || constraint.isSatisfiedBy(theValue)) {
@@ -114,18 +112,13 @@ private boolean isConstraintUnsatisfiedBy(T theValue) {
*/
@Override
public boolean subscribe(PropertyChangeListener listener) {
- if (subscribers == null) {
- subscribers = Sets.newHashSet();
- }
-
if (listener == null) {
LOGGER.warn(formatWarning("A null subscriber cannot be added."));
-
return false;
}
+
if (subscribers.contains(listener)) {
LOGGER.warn(formatWarning("The listener has already been subscribed."));
-
return false;
}
@@ -146,10 +139,6 @@ public boolean unsubscribe(PropertyChangeListener listener) {
subscribers.remove(listener);
- if (subscribers.size() <= 0) {
- subscribers = null;
- }
-
return true;
}
@@ -158,7 +147,7 @@ public boolean unsubscribe(PropertyChangeListener listener) {
*/
@Override
public boolean hasSubscribers() {
- return subscribers != null;
+ return !subscribers.isEmpty();
}
/**
