/** The packs this bot carries. */
private final Map<String, Pack> packs = Maps.newHashMap();
+ private final Map<String, Pack> packsByName = Maps.newHashMap();
/** The current name of the bot. */
private String name;
* The pack to add
*/
public void addPack(Pack pack) {
- packs.put(pack.id(), pack);
+ synchronized (this) {
+ if (packsByName.containsKey(pack.name())) {
+ Pack oldPack = packsByName.remove(pack.name());
+ packs.remove(oldPack.id());
+ }
+ packs.put(pack.id(), pack);
+ packsByName.put(pack.name(), pack);
+ }
}
//
--- /dev/null
+package net.pterodactylus.xdcc.data;
+
+import org.hamcrest.MatcherAssert;
+import org.hamcrest.Matchers;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+/**
+ * Unit test for {@link Bot}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class BotTest {
+
+ private final Network network = Mockito.mock(Network.class);
+ private final Bot bot = new Bot(network, "#test", "Test-Bot");
+ private final Pack oldPack = new Pack("7", "123", "new-file.dat");
+ private final Pack newPack = new Pack("15", "234", "new-file.dat");
+
+ @Test
+ public void addingOneFileWithTwoIdsRemovesTheOldId() {
+ bot.addPack(oldPack);
+ bot.addPack(newPack);
+ MatcherAssert.assertThat(bot.packs(), Matchers.contains(newPack));
+ }
+
+}