Synchronize all access on packs
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / data / Bot.java
index 211dde7..0f09671 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;
@@ -81,7 +82,9 @@ public class Bot implements Iterable<Pack> {
         * @return The packs this bot carries
         */
        public Collection<Pack> packs() {
-               return packs.values();
+               synchronized (packs) {
+                       return packs.values();
+               }
        }
 
        //
@@ -111,7 +114,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);
+               }
        }
 
        //
@@ -120,7 +130,7 @@ public class Bot implements Iterable<Pack> {
 
        @Override
        public Iterator<Pack> iterator() {
-               return packs.values().iterator();
+               return packs().iterator();
        }
 
        //