X-Git-Url: https://git.pterodactylus.net/?p=xudocci.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fxdcc%2Fdata%2FBot.java;h=c9e2a9b822b80d1e5d0b6a186738d2cb7ff980e3;hp=96d91b654ef57b38a08d44cc4384f6f6da484856;hb=428a71042507e2fe0fce5b1b144ed60ec49d3183;hpb=db7e9612fa189cee9f4b9086e00d87c4d6c58bbf diff --git a/src/main/java/net/pterodactylus/xdcc/data/Bot.java b/src/main/java/net/pterodactylus/xdcc/data/Bot.java index 96d91b6..c9e2a9b 100644 --- a/src/main/java/net/pterodactylus/xdcc/data/Bot.java +++ b/src/main/java/net/pterodactylus/xdcc/data/Bot.java @@ -20,6 +20,7 @@ package net.pterodactylus.xdcc.data; import static com.google.common.base.Preconditions.checkNotNull; import java.util.Collection; +import java.util.Collections; import java.util.Iterator; import java.util.Map; @@ -35,23 +36,18 @@ public class Bot implements Iterable { /** The network this bot is on. */ private final Network network; + private final String channel; /** The packs this bot carries. */ private final Map packs = Maps.newHashMap(); + private final Map 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 +64,10 @@ public class Bot implements Iterable { return network; } + public String channel() { + return channel; + } + /** * Returns the current name of this bot. * @@ -83,7 +83,9 @@ public class Bot implements Iterable { * @return The packs this bot carries */ public Collection packs() { - return packs.values(); + synchronized (packs) { + return Collections.unmodifiableCollection(packs.values()); + } } // @@ -113,7 +115,14 @@ public class Bot implements Iterable { * 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); + } } // @@ -122,7 +131,7 @@ public class Bot implements Iterable { @Override public Iterator iterator() { - return packs.values().iterator(); + return packs().iterator(); } //