Return unmodifiable view of packs
[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.Collections;
24 import java.util.Iterator;
25 import java.util.Map;
26
27 import com.google.common.collect.Maps;
28
29 /**
30  * A bot is a client in a {@link Network} that carries {@link Pack}s which are
31  * available for download.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public class Bot implements Iterable<Pack> {
36
37         /** The network this bot is on. */
38         private final Network network;
39         private final String channel;
40
41         /** The packs this bot carries. */
42         private final Map<String, Pack> packs = Maps.newHashMap();
43         private final Map<String, Pack> packsByName = Maps.newHashMap();
44
45         /** The current name of the bot. */
46         private String name;
47
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");
52         }
53
54         //
55         // ACCESSORS
56         //
57
58         /**
59          * Returns the network the bot is on.
60          *
61          * @return The network the bot is on
62          */
63         public Network network() {
64                 return network;
65         }
66
67         public String channel() {
68                 return channel;
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                 synchronized (packs) {
87                         return Collections.unmodifiableCollection(packs.values());
88                 }
89         }
90
91         //
92         // MUTATORS
93         //
94
95         /**
96          * Sets the current name of this bot.
97          *
98          * @param name
99          *              The name of this bot
100          * @return This bot
101          */
102         public Bot name(String name) {
103                 this.name = checkNotNull(name, "name must not be null");
104                 return this;
105         }
106
107         //
108         // ACTIONS
109         //
110
111         /**
112          * Adds the given pack to this bot.
113          *
114          * @param pack
115          *              The pack to add
116          */
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());
122                         }
123                         packs.put(pack.id(), pack);
124                         packsByName.put(pack.name(), pack);
125                 }
126         }
127
128         //
129         // ITERABLE METHODS
130         //
131
132         @Override
133         public Iterator<Pack> iterator() {
134                 return packs().iterator();
135         }
136
137         //
138         // OBJECT METHODS
139         //
140
141         @Override
142         public boolean equals(Object object) {
143                 if (!(object instanceof Bot)) {
144                         return false;
145                 }
146                 Bot bot = (Bot) object;
147                 return network().equals(bot.network()) && name().equals(bot.name());
148         }
149
150         @Override
151         public int hashCode() {
152                 return network().hashCode() ^ name().hashCode();
153         }
154
155         @Override
156         public String toString() {
157                 return String.format("%s/%s", name(), network().name());
158         }
159
160 }