96d91b654ef57b38a08d44cc4384f6f6da484856
[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
39         /** The packs this bot carries. */
40         private final Map<String, Pack> packs = Maps.newHashMap();
41
42         /** The current name of the bot. */
43         private String name;
44
45         /**
46          * Creates a new bot.
47          *
48          * @param network
49          *              The network the bot is on
50          * @param name
51          *              The name of the bot
52          */
53         public Bot(Network network, String name) {
54                 this.network = checkNotNull(network, "network must not be null");
55                 this.name = checkNotNull(name, "name must not be null");
56         }
57
58         //
59         // ACCESSORS
60         //
61
62         /**
63          * Returns the network the bot is on.
64          *
65          * @return The network the bot is on
66          */
67         public Network network() {
68                 return network;
69         }
70
71         /**
72          * Returns the current name of this bot.
73          *
74          * @return The current name of this bot
75          */
76         public String name() {
77                 return name;
78         }
79
80         /**
81          * Returns the packs this bot carries.
82          *
83          * @return The packs this bot carries
84          */
85         public Collection<Pack> packs() {
86                 return packs.values();
87         }
88
89         //
90         // MUTATORS
91         //
92
93         /**
94          * Sets the current name of this bot.
95          *
96          * @param name
97          *              The name of this bot
98          * @return This bot
99          */
100         public Bot name(String name) {
101                 this.name = checkNotNull(name, "name must not be null");
102                 return this;
103         }
104
105         //
106         // ACTIONS
107         //
108
109         /**
110          * Adds the given pack to this bot.
111          *
112          * @param pack
113          *              The pack to add
114          */
115         public void addPack(Pack pack) {
116                 packs.put(pack.id(), pack);
117         }
118
119         //
120         // ITERABLE METHODS
121         //
122
123         @Override
124         public Iterator<Pack> iterator() {
125                 return packs.values().iterator();
126         }
127
128         //
129         // OBJECT METHODS
130         //
131
132         @Override
133         public boolean equals(Object object) {
134                 if (!(object instanceof Bot)) {
135                         return false;
136                 }
137                 Bot bot = (Bot) object;
138                 return network().equals(bot.network()) && name().equals(bot.name());
139         }
140
141         @Override
142         public int hashCode() {
143                 return network().hashCode() ^ name().hashCode();
144         }
145
146         @Override
147         public String toString() {
148                 return String.format("%s/%s", name(), network().name());
149         }
150
151 }