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