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.Collections;
24 import java.util.Iterator;
27 import com.google.common.collect.Maps;
30 * A bot is a client in a {@link Network} that carries {@link Pack}s which are
31 * available for download.
33 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35 public class Bot implements Iterable<Pack> {
37 /** The network this bot is on. */
38 private final Network network;
39 private final String channel;
41 /** The packs this bot carries. */
42 private final Map<String, Pack> packs = Maps.newHashMap();
43 private final Map<String, Pack> packsByName = Maps.newHashMap();
45 /** The current name of the bot. */
48 public Bot(Network network, String channel, String name) {
49 this.network = checkNotNull(network, "network must not be null");
50 this.channel = checkNotNull(channel, "channel must not be null");
51 this.name = checkNotNull(name, "name must not be null");
59 * Returns the network the bot is on.
61 * @return The network the bot is on
63 public Network network() {
67 public String channel() {
72 * Returns the current name of this bot.
74 * @return The current name of this bot
76 public String name() {
81 * Returns the packs this bot carries.
83 * @return The packs this bot carries
85 public Collection<Pack> packs() {
86 synchronized (packs) {
87 return Collections.unmodifiableCollection(packs.values());
96 * Sets the current name of this bot.
99 * The name of this bot
102 public Bot name(String name) {
103 this.name = checkNotNull(name, "name must not be null");
112 * Adds the given pack to this bot.
117 public void addPack(Pack pack) {
118 synchronized (this) {
119 if (packsByName.containsKey(pack.name())) {
120 Pack oldPack = packsByName.remove(pack.name());
121 packs.remove(oldPack.id());
123 packs.put(pack.id(), pack);
124 packsByName.put(pack.name(), pack);
133 public Iterator<Pack> iterator() {
134 return packs().iterator();
142 public boolean equals(Object object) {
143 if (!(object instanceof Bot)) {
146 Bot bot = (Bot) object;
147 return network().equals(bot.network()) && name().equals(bot.name());
151 public int hashCode() {
152 return network().hashCode() ^ name().hashCode();
156 public String toString() {
157 return String.format("%s/%s", name(), network().name());