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