Use map and set creators from Guava.
[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 java.util.Collection;
21 import java.util.Iterator;
22 import java.util.Map;
23
24 import com.google.common.collect.Maps;
25
26 /**
27  * A bot is a client in a {@link Network} that carries {@link Pack}s which are
28  * available for download.
29  *
30  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31  */
32 public class Bot implements Iterable<Pack> {
33
34         /** The network this bot is on. */
35         private final Network network;
36
37         /** The packs this bot carries. */
38         private final Map<String, Pack> packs = Maps.newHashMap();
39
40         /** The current name of the bot. */
41         private String name;
42
43         /**
44          * Creates a new bot.
45          *
46          * @param network
47          *              The network the bot is on
48          */
49         public Bot(Network network) {
50                 this.network = network;
51         }
52
53         //
54         // ACCESSORS
55         //
56
57         /**
58          * Returns the network the bot is on.
59          *
60          * @return The network the bot is on
61          */
62         public Network network() {
63                 return network;
64         }
65
66         /**
67          * Returns the current name of this bot.
68          *
69          * @return The current name of this bot
70          */
71         public String name() {
72                 return name;
73         }
74
75         /**
76          * Returns the packs this bot carries.
77          *
78          * @return The packs this bot carries
79          */
80         public Collection<Pack> packs() {
81                 return packs.values();
82         }
83
84         //
85         // MUTATORS
86         //
87
88         /**
89          * Sets the current name of this bot.
90          *
91          * @param name
92          *              The name of this bot
93          * @return This bot
94          */
95         public Bot name(String name) {
96                 this.name = name;
97                 return this;
98         }
99
100         //
101         // ACTIONS
102         //
103
104         /**
105          * Adds the given pack to this bot.
106          *
107          * @param pack
108          *              The pack to add
109          */
110         public void addPack(Pack pack) {
111                 packs.put(pack.id(), pack);
112         }
113
114         //
115         // ITERABLE METHODS
116         //
117
118         @Override
119         public Iterator<Pack> iterator() {
120                 return packs.values().iterator();
121         }
122
123         //
124         // OBJECT METHODS
125         //
126
127         @Override
128         public String toString() {
129                 return String.format("%s/%s", name(), network().name());
130         }
131
132 }