Always create Shells with IDs.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / SoneShell.java
1 /*
2  * Sone - SoneShell.java - Copyright © 2010 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.sone.data;
19
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.UUID;
26
27 import freenet.keys.FreenetURI;
28
29 /**
30  * {@link Shell} around a {@link Sone} that has not yet been retrieved.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class SoneShell extends Sone implements Shell<Sone> {
35
36         /** The shell creator. */
37         public static final ShellCreator<Sone> creator = new ShellCreator<Sone>() {
38
39                 @Override
40                 public Shell<Sone> createShell(String id) {
41                         return new SoneShell().setId(UUID.fromString(id));
42                 }
43         };
44
45         /** A GUID for this Sone. */
46         private UUID id;
47
48         /** The name of this Sone. */
49         private String name;
50
51         /** The URI under which the Sone is stored in Freenet. */
52         private FreenetURI requestUri;
53
54         /** The profile of this Sone. */
55         private Profile profile;
56
57         /** All friend Sones. */
58         private final Set<Sone> friendSones = new HashSet<Sone>();
59
60         /** All posts. */
61         private final List<Post> posts = new ArrayList<Post>();
62
63         /** All replies. */
64         private final Set<Reply> replies = new HashSet<Reply>();
65
66         /**
67          * Creates a new Sone shell.
68          */
69         public SoneShell() {
70                 super(null, null, null);
71         }
72
73         //
74         // ACCESSORS
75         //
76
77         /**
78          * Returns the ID of this Sone.
79          *
80          * @return The ID of this Sone
81          */
82         @Override
83         public String getId() {
84                 return id.toString();
85         }
86
87         /**
88          * Sets the ID of the Sone.
89          *
90          * @param id
91          *            The ID of the Sone
92          * @return This Sone shell (for method chaining)
93          */
94         public SoneShell setId(UUID id) {
95                 this.id = id;
96                 return this;
97         }
98
99         /**
100          * Returns the name of this Sone.
101          *
102          * @return The name of this Sone
103          */
104         @Override
105         public String getName() {
106                 return name;
107         }
108
109         /**
110          * Sets the name of the Sone.
111          *
112          * @param name
113          *            The name of the Sone
114          * @return This Sone shell (for method chaining)
115          */
116         public SoneShell setName(String name) {
117                 this.name = name;
118                 return this;
119         }
120
121         /**
122          * Returns the request URI of this Sone.
123          *
124          * @return The request URI of this Sone
125          */
126         @Override
127         public FreenetURI getRequestUri() {
128                 return requestUri;
129         }
130
131         /**
132          * Sets the request URI of the Sone.
133          *
134          * @param requestUri
135          *            The request URI of the Sone
136          * @return This Sone shell (for method chaining)
137          */
138         public SoneShell setRequestUri(FreenetURI requestUri) {
139                 this.requestUri = requestUri;
140                 return this;
141         }
142
143         /**
144          * Returns a copy of the profile. If you want to update values in the
145          * profile of this Sone, update the values in the returned {@link Profile}
146          * and use {@link #setProfile(Profile)} to change the profile in this Sone.
147          *
148          * @return A copy of the profile
149          */
150         @Override
151         public Profile getProfile() {
152                 return new Profile(profile);
153         }
154
155         /**
156          * {@inheritDoc}
157          */
158         @Override
159         public void setProfile(Profile profile) {
160                 this.profile = profile;
161         }
162
163         /**
164          * Returns all friend Sones of this Sone.
165          *
166          * @return The friend Sones of this Sone
167          */
168         @Override
169         public Set<Sone> getFriendSones() {
170                 return Collections.unmodifiableSet(friendSones);
171         }
172
173         /**
174          * Returns whether this Sone has the given Sone as a friend Sone.
175          *
176          * @param friendSone
177          *            The friend Sone to check for
178          * @return {@code true} if this Sone has the given Sone as a friend,
179          *         {@code false} otherwise
180          */
181         @Override
182         public boolean hasFriendSone(Sone friendSone) {
183                 return friendSones.contains(friendSone);
184         }
185
186         /**
187          * Adds the given Sone as a friend Sone.
188          *
189          * @param friendSone
190          *            The friend Sone to add
191          * @return This Sone (for method chaining)
192          */
193         @Override
194         public Sone addFriendSone(Sone friendSone) {
195                 friendSones.add(friendSone);
196                 return this;
197         }
198
199         /**
200          * Removes the given Sone as a friend Sone.
201          *
202          * @param friendSone
203          *            The friend Sone to remove
204          * @return This Sone (for method chaining)
205          */
206         @Override
207         public Sone removeFriendSone(Sone friendSone) {
208                 friendSones.remove(friendSone);
209                 return this;
210         }
211
212         /**
213          * Returns the list of posts of this Sone.
214          *
215          * @return All posts of this Sone
216          */
217         @Override
218         public List<Post> getPosts() {
219                 return Collections.unmodifiableList(posts);
220         }
221
222         /**
223          * Adds the given post to this Sone. The post will not be added if its
224          * {@link Post#getSone() Sone} is not this Sone.
225          *
226          * @param post
227          *            The post to add
228          */
229         @Override
230         public void addPost(Post post) {
231                 posts.add(post);
232         }
233
234         /**
235          * Removes the given post from this Sone.
236          *
237          * @param post
238          *            The post to remove
239          */
240         @Override
241         public void removePost(Post post) {
242                 posts.remove(post);
243         }
244
245         /**
246          * Returns all replies this Sone made.
247          *
248          * @return All replies this Sone made
249          */
250         @Override
251         public Set<Reply> getReplies() {
252                 return Collections.unmodifiableSet(replies);
253         }
254
255         /**
256          * Adds a reply to this Sone. If the given reply was not made by this Sone,
257          * nothing is added to this Sone.
258          *
259          * @param reply
260          *            The reply to add
261          */
262         @Override
263         public void addReply(Reply reply) {
264                 replies.add(reply);
265         }
266
267         /**
268          * Removes a reply from this Sone.
269          *
270          * @param reply
271          *            The reply to remove
272          */
273         @Override
274         public void removeReply(Reply reply) {
275                 replies.remove(reply);
276         }
277
278         //
279         // INTERFACE Shell
280         //
281
282         /**
283          * {@inheritDoc}
284          */
285         @Override
286         public boolean canUnshell() {
287                 return (id != null) && (name != null) && (requestUri != null) && (profile != null);
288         }
289
290         /**
291          * {@inheritDoc}
292          */
293         @Override
294         public Sone getShelled() {
295                 if (canUnshell()) {
296                         Sone sone = new Sone(id, name, requestUri);
297                         sone.setProfile(profile);
298                         for (Sone friendSone : friendSones) {
299                                 sone.addFriendSone(friendSone);
300                         }
301                         for (Post post : posts) {
302                                 sone.addPost(post);
303                         }
304                         for (Reply reply : replies) {
305                                 sone.addReply(reply);
306                         }
307                         return sone;
308                 }
309                 return this;
310         }
311
312 }