Thomas Vachuska
Committed by Gerrit Code Review

Simplifying removeListener to merely log when remove indicates no such listener.

Change-Id: I6d47fd70938b5e049e523f94d3e7242661fb154d
......@@ -20,7 +20,6 @@ import org.slf4j.Logger;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.slf4j.LoggerFactory.getLogger;
......@@ -33,8 +32,6 @@ public class ListenerRegistry<E extends Event, L extends EventListener<E>>
private final Logger log = getLogger(getClass());
private volatile boolean shutdown = false;
private long lastStart;
private L lastListener;
......@@ -61,8 +58,8 @@ public class ListenerRegistry<E extends Event, L extends EventListener<E>>
*/
public void removeListener(L listener) {
checkNotNull(listener, "Listener cannot be null");
if (checkForNonRegistrant()) {
checkArgument(listeners.remove(listener), "Listener not registered");
if (!listeners.remove(listener)) {
log.warn("Listener {} not registered", listener);
}
}
......@@ -91,41 +88,13 @@ public class ListenerRegistry<E extends Event, L extends EventListener<E>>
}
/**
* Predicate indicating whether we should throw an exception if the
* argument to {@link #removeListener} is not in the current set of
* listeners.
* <p>
* This default implementation returns <code>true</code>.
*
* @return true if non-listed listeners should cause exception on remove
*/
protected boolean checkForNonRegistrant() {
return true;
}
/**
* Reports a problem encountered while processing an event.
*
* @param event event being processed
* @param error error encountered while processing
*/
protected void reportProblem(E event, Throwable error) {
if (!shutdown) {
log.warn("Exception encountered while processing event " + event, error);
}
log.warn("Exception encountered while processing event " + event, error);
}
/**
* Prepares the registry for normal operation.
*/
public void activate() {
shutdown = false;
}
/**
* Prepares the registry for shutdown.
*/
public void deactivate() {
shutdown = true;
}
}
......
......@@ -26,13 +26,6 @@ import static org.junit.Assert.assertTrue;
*/
public class ListenerRegistryTest {
private static class RelaxedRegistry extends TestListenerRegistry {
@Override
protected boolean checkForNonRegistrant() {
return false;
}
}
private static final TestEvent FOO_EVENT =
new TestEvent(TestEvent.Type.FOO, "foo");
private static final TestEvent BAR_EVENT =
......@@ -78,16 +71,4 @@ public class ListenerRegistryTest {
assertTrue("BAR not processed", secondListener.events.contains(BAR_EVENT));
}
@Test(expected = IllegalArgumentException.class)
public void removeNonListenerCausesException() {
manager.removeListener(listener);
}
@Test
public void removeNonListenerIgnored() {
manager = new RelaxedRegistry();
manager.removeListener(listener);
assertTrue("what?", manager.listeners.isEmpty());
}
}
......
......@@ -59,8 +59,4 @@ class ModelListenerRegistry
}
}
@Override
protected boolean checkForNonRegistrant() {
return false;
}
}
......