2 * XdccDownloader - Bot.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.xdcc.data;
20 import static com.google.common.base.Preconditions.checkNotNull;
22 import java.util.Collection;
23 import java.util.Iterator;
26 import com.google.common.collect.Maps;
29 * A bot is a client in a {@link Network} that carries {@link Pack}s which are
30 * available for download.
32 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34 public class Bot implements Iterable<Pack> {
36 /** The network this bot is on. */
37 private final Network network;
38 private final String channel;
40 /** The packs this bot carries. */
41 private final Map<String, Pack> packs = Maps.newHashMap();
42 private final Map<String, Pack> packsByName = Maps.newHashMap();
44 /** The current name of the bot. */
47 public Bot(Network network, String channel, String name) {
48 this.network = checkNotNull(network, "network must not be null");
49 this.channel = checkNotNull(channel, "channel must not be null");
50 this.name = checkNotNull(name, "name must not be null");
58 * Returns the network the bot is on.
60 * @return The network the bot is on
62 public Network network() {
66 public String channel() {
71 * Returns the current name of this bot.
73 * @return The current name of this bot
75 public String name() {
80 * Returns the packs this bot carries.
82 * @return The packs this bot carries
84 public Collection<Pack> packs() {
85 synchronized (packs) {
86 return packs.values();
95 * Sets the current name of this bot.
98 * The name of this bot
101 public Bot name(String name) {
102 this.name = checkNotNull(name, "name must not be null");
111 * Adds the given pack to this bot.
116 public void addPack(Pack pack) {
117 synchronized (this) {
118 if (packsByName.containsKey(pack.name())) {
119 Pack oldPack = packsByName.remove(pack.name());
120 packs.remove(oldPack.id());
122 packs.put(pack.id(), pack);
123 packsByName.put(pack.name(), pack);
132 public Iterator<Pack> iterator() {
133 return packs().iterator();
141 public boolean equals(Object object) {
142 if (!(object instanceof Bot)) {
145 Bot bot = (Bot) object;
146 return network().equals(bot.network()) && name().equals(bot.name());
150 public int hashCode() {
151 return network().hashCode() ^ name().hashCode();
155 public String toString() {
156 return String.format("%s/%s", name(), network().name());