Remove old pack if pack with newer ID is added
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / data / Bot.java
index 96d91b6..3ca34f3 100644 (file)
@@ -35,23 +35,18 @@ public class Bot implements Iterable<Pack> {
 
        /** The network this bot is on. */
        private final Network network;
+       private final String channel;
 
        /** 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;
 
-       /**
-        * Creates a new bot.
-        *
-        * @param network
-        *              The network the bot is on
-        * @param name
-        *              The name of the bot
-        */
-       public Bot(Network network, String name) {
+       public Bot(Network network, String channel, String name) {
                this.network = checkNotNull(network, "network must not be null");
+               this.channel = checkNotNull(channel, "channel must not be null");
                this.name = checkNotNull(name, "name must not be null");
        }
 
@@ -68,6 +63,10 @@ public class Bot implements Iterable<Pack> {
                return network;
        }
 
+       public String channel() {
+               return channel;
+       }
+
        /**
         * Returns the current name of this bot.
         *
@@ -113,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);
+               }
        }
 
        //