HostRegistry implements HostValidator<T> { public synchronized void update(T key, Collection<String> newHosts) { verifyHosts(key, newHosts); Collection<String> currentHosts = getHostsForKey(key); log.log(Level.FINE, () -> "Setting hosts for key '" + key + "', " + "newHosts: " + newHosts + ", " + "currentHosts: " + currentHosts); Collection<String> removedHosts = getRemovedHosts(newHosts, currentHosts); removeHosts(removedHosts); addHosts(key, newHosts); }  T getKeyForHost(String hostName); synchronized void update(T key, Collection<String> newHosts); @Override synchronized void verifyHosts(T key, Collection<String> newHosts); synchronized void removeHostsForKey(T key); synchronized Collection<String> getAllHosts();  }
@Test public void multiple_keys_are_handled() { HostRegistry<String> reg = new HostRegistry<>(); reg.update("fookey", List.of("foo.com", "bar.com")); reg.update("barkey", List.of("baz.com", "quux.com")); assertGetKey(reg, "foo.com", "fookey"); assertGetKey(reg, "bar.com", "fookey"); assertGetKey(reg, "baz.com", "barkey"); assertGetKey(reg, "quux.com", "barkey"); }
@Test(expected = IllegalArgumentException.class) public void keys_cannot_overlap() { HostRegistry<String> reg = new HostRegistry<>(); reg.update("fookey", List.of("foo.com", "bar.com")); reg.update("barkey", List.of("bar.com", "baz.com")); }