211dde7e78130da750d645ba6cea55369474f0e5
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / data / Bot.java
1 /*
2  * XdccDownloader - Bot.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.xdcc.data;
19
20 import static com.google.common.base.Preconditions.checkNotNull;
21
22 import java.util.Collection;
23 import java.util.Iterator;
24 import java.util.Map;
25
26 import com.google.common.collect.Maps;
27
28 /**
29  * A bot is a client in a {@link Network} that carries {@link Pack}s which are
30  * available for download.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class Bot implements Iterable<Pack> {
35
36         /** The network this bot is on. */
37         private final Network network;
38         private final String channel;
39
40         /** The packs this bot carries. */
41         private final Map<String, Pack> packs = Maps.newHashMap();
42
43         /** The current name of the bot. */
44         private String name;
45
46         public Bot(Network network, String channel, String name) {
47                 this.network = checkNotNull(network, "network must not be null");
48                 this.channel = checkNotNull(channel, "channel must not be null");
49                 this.name = checkNotNull(name, "name must not be null");
50         }
51
52         //
53         // ACCESSORS
54         //
55
56         /**
57          * Returns the network the bot is on.
58          *
59          * @return The network the bot is on
60          */
61         public Network network() {
62                 return network;
63         }
64
65         public String channel() {
66                 return channel;
67         }
68
69         /**
70          * Returns the current name of this bot.
71          *
72          * @return The current name of this bot
73          */
74         public String name() {
75                 return name;
76         }
77
78         /**
79          * Returns the packs this bot carries.
80          *
81          * @return The packs this bot carries
82          */
83         public Collection<Pack> packs() {
84                 return packs.values();
85         }
86
87         //
88         // MUTATORS
89         //
90
91         /**
92          * Sets the current name of this bot.
93          *
94          * @param name
95          *              The name of this bot
96          * @return This bot
97          */
98         public Bot name(String name) {
99                 this.name = checkNotNull(name, "name must not be null");
100                 return this;
101         }
102
103         //
104         // ACTIONS
105         //
106
107         /**
108          * Adds the given pack to this bot.
109          *
110          * @param pack
111          *              The pack to add
112          */
113         public void addPack(Pack pack) {
114                 packs.put(pack.id(), pack);
115         }
116
117         //
118         // ITERABLE METHODS
119         //
120
121         @Override
122         public Iterator<Pack> iterator() {
123                 return packs.values().iterator();
124         }
125
126         //
127         // OBJECT METHODS
128         //
129
130         @Override
131         public boolean equals(Object object) {
132                 if (!(object instanceof Bot)) {
133                         return false;
134                 }
135                 Bot bot = (Bot) object;
136                 return network().equals(bot.network()) && name().equals(bot.name());
137         }
138
139         @Override
140         public int hashCode() {
141                 return network().hashCode() ^ name().hashCode();
142         }
143
144         @Override
145         public String toString() {
146                 return String.format("%s/%s", name(), network().name());
147         }
148
149 }