Remove old pack if pack with newer ID is added
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / data / Bot.java
index 211dde7..3ca34f3 100644 (file)
@@ -39,6 +39,7 @@ public class Bot implements Iterable<Pack> {
 
        /** 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;
@@ -111,7 +112,14 @@ public class Bot implements Iterable<Pack> {
         *              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);
+               }
        }
 
        //