CommModel extends AbstractModel<CommUser> implements TickListener { @Override public boolean unregister(CommUser commUser) { checkArgument(contains(commUser), "CommModel does not contain %s.", commUser); final CommDevice unregDevice = usersDevices.remove(commUser); unregDevice.unregister(); unregisteredUsersDevices.put(commUser, unregDevice); usersHasChanged = true; if (eventDispatcher.hasListenerFor(EventTypes.REMOVE_COMM_USER)) { eventDispatcher.dispatchEvent(new CommModelEvent( EventTypes.REMOVE_COMM_USER, this, unregDevice, commUser)); } return true; } CommModel(RandomGenerator rng, Builder b); EventAPI getEventAPI(); @Override boolean register(CommUser commUser); @Override boolean unregister(CommUser commUser); boolean contains(CommUser commUser); double getDefaultReliability(); Optional<Double> getDefaultMaxRange(); @Override @Nonnull U get(Class<U> clazz); @Override void tick(TimeLapse timeLapse); @Override void afterTick(TimeLapse timeLapse); ImmutableBiMap<CommUser, CommDevice> getUsersAndDevices(); static Builder builder();  }
@Test public void testUnregister() { agent1.device().send(Contents.YO, agent2); agent2.device().send(Contents.HELLO_WORLD, agent1); assertThat(agent1.device().getOutbox()).hasSize(1); assertThat(agent2.device().getOutbox()).hasSize(1); assertThat(model.contains(agent2)).isTrue(); model.unregister(agent2); assertThat(model.contains(agent2)).isFalse(); boolean fail = false; try { model.unregister(agent2); } catch (final IllegalArgumentException e) { assertThat(e.getMessage()).contains("CommModel does not contain"); fail = true; } assertThat(fail).isTrue(); model.afterTick(TimeLapseFactory.create(0, 100)); assertThat(agent1.device().getReceivedCount()).isEqualTo(0); assertThat(agent2.device().getReceivedCount()).isEqualTo(0); assertThat(agent1.device().getOutbox()).hasSize(0); assertThat(agent2.device().getOutbox()).hasSize(1); fail = false; try { agent2.device().send(Contents.YO, agent1); } catch (final IllegalStateException e) { fail = true; assertThat(e.getMessage()).contains("unregistered"); } assertThat(fail).isTrue(); fail = false; try { agent2.device().broadcast(Contents.YO); } catch (final IllegalStateException e) { fail = true; assertThat(e.getMessage()).contains("unregistered"); } assertThat(fail).isTrue(); fail = false; try { agent2.device().clearOutbox(); } catch (final IllegalStateException e) { fail = true; assertThat(e.getMessage()).contains("unregistered"); } assertThat(fail).isTrue(); }