X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fxdcc%2Fdata%2FBot.java;h=0f096718cae033e74d432203f75373f11204812b;hb=ce7a48c4eda76cad0be79a58a31bff08b99a14d3;hp=46bca1896b5a7ec2f7bbc9d7083c1c4a56327266;hpb=afec2edc48b0151823ad0696965e533b7dec9184;p=xudocci.git diff --git a/src/main/java/net/pterodactylus/xdcc/data/Bot.java b/src/main/java/net/pterodactylus/xdcc/data/Bot.java index 46bca18..0f09671 100644 --- a/src/main/java/net/pterodactylus/xdcc/data/Bot.java +++ b/src/main/java/net/pterodactylus/xdcc/data/Bot.java @@ -17,11 +17,13 @@ package net.pterodactylus.xdcc.data; +import static com.google.common.base.Preconditions.checkNotNull; + import java.util.Collection; import java.util.Iterator; import java.util.Map; -import com.beust.jcommander.internal.Maps; +import com.google.common.collect.Maps; /** * A bot is a client in a {@link Network} that carries {@link Pack}s which are @@ -33,21 +35,19 @@ 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 - */ - public Bot(Network network) { - this.network = network; + 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"); } // @@ -63,6 +63,10 @@ public class Bot implements Iterable { return network; } + public String channel() { + return channel; + } + /** * Returns the current name of this bot. * @@ -78,7 +82,9 @@ public class Bot implements Iterable { * @return The packs this bot carries */ public Collection packs() { - return packs.values(); + synchronized (packs) { + return packs.values(); + } } // @@ -93,7 +99,7 @@ public class Bot implements Iterable { * @return This bot */ public Bot name(String name) { - this.name = name; + this.name = checkNotNull(name, "name must not be null"); return this; } @@ -108,7 +114,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); + } } // @@ -117,7 +130,7 @@ public class Bot implements Iterable { @Override public Iterator iterator() { - return packs.values().iterator(); + return packs().iterator(); } // @@ -125,6 +138,20 @@ public class Bot implements Iterable { // @Override + public boolean equals(Object object) { + if (!(object instanceof Bot)) { + return false; + } + Bot bot = (Bot) object; + return network().equals(bot.network()) && name().equals(bot.name()); + } + + @Override + public int hashCode() { + return network().hashCode() ^ name().hashCode(); + } + + @Override public String toString() { return String.format("%s/%s", name(), network().name()); }