LocalListenerRegistry { LocalRegistration addListener(RegistrationKey registrationKey, MailboxListener.ReactiveMailboxListener listener) { AtomicBoolean firstListener = new AtomicBoolean(false); listenersByKey.compute(registrationKey, (key, listeners) -> Optional.ofNullable(listeners) .map(set -> ImmutableSet.<MailboxListener.ReactiveMailboxListener>builder().addAll(set).add(listener).build()) .orElseGet(() -> { firstListener.set(true); return ImmutableSet.of(listener); }) ); return new LocalRegistration(firstListener.get(), () -> removeListener(registrationKey, listener)); } LocalListenerRegistry();   }
@Test void addListenerShouldReturnFirstListenerWhenNoPreviouslyRegisteredListeners() { MailboxListener listener = event -> { }; assertThat(testee.addListener(KEY_1, listener).isFirstListener()).isTrue(); }
@Test void addListenerShouldNotReturnFirstListenerWhenPreviouslyRegisteredListeners() { MailboxListener listener = event -> { }; MailboxListener listener2 = event -> { }; testee.addListener(KEY_1, listener); assertThat(testee.addListener(KEY_1, listener2).isFirstListener()).isFalse(); }
@Test void removeListenerShouldNotReturnLastListenerRemovedWhenSeveralListener() { MailboxListener listener = event -> { }; MailboxListener listener2 = event -> { }; LocalListenerRegistry.LocalRegistration registration = testee.addListener(KEY_1, listener); testee.addListener(KEY_1, listener2); assertThat(registration.unregister().lastListenerRemoved()).isFalse(); }
@Test void removeListenerShouldReturnLastListenerRemovedWhenOneListener() { MailboxListener listener = event -> { }; LocalListenerRegistry.LocalRegistration registration = testee.addListener(KEY_1, listener); assertThat(registration.unregister().lastListenerRemoved()).isTrue(); }