Remove the shell stuff, make objects mutable.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Sone.java
1 /*
2  * FreenetSone - Sone.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.Comparator;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
26 import java.util.UUID;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29
30 import net.pterodactylus.util.logging.Logging;
31 import freenet.keys.FreenetURI;
32
33 /**
34  * A Sone defines everything about a user: her profile, her status updates, her
35  * replies, her likes and dislikes, etc.
36  * <p>
37  * Operations that modify the Sone need to synchronize on the Sone in question.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class Sone {
42
43         /** The logger. */
44         private static final Logger logger = Logging.getLogger(Sone.class);
45
46         /** A GUID for this Sone. */
47         private final UUID id;
48
49         /** The name of this Sone. */
50         private String name;
51
52         /** The URI under which the Sone is stored in Freenet. */
53         private FreenetURI requestUri;
54
55         /** The URI used to insert a new version of this Sone. */
56         /* This will be null for remote Sones! */
57         private FreenetURI insertUri;
58
59         /** The profile of this Sone. */
60         private Profile profile;
61
62         /** All friend Sones. */
63         private final Set<Sone> friendSones = new HashSet<Sone>();
64
65         /** All posts. */
66         private final List<Post> posts = new ArrayList<Post>();
67
68         /** All replies. */
69         private final Set<Reply> replies = new HashSet<Reply>();
70
71         /** Modification count. */
72         private volatile long modificationCounter = 0;
73
74         /**
75          * Creates a new Sone.
76          *
77          * @param id
78          *            The ID of this Sone
79          */
80         public Sone(String id) {
81                 this.id = UUID.fromString(id);
82         }
83
84         //
85         // ACCESSORS
86         //
87
88         /**
89          * Returns the ID of this Sone.
90          *
91          * @return The ID of this Sone
92          */
93         public String getId() {
94                 return id.toString();
95         }
96
97         /**
98          * Returns the name of this Sone.
99          *
100          * @return The name of this Sone
101          */
102         public String getName() {
103                 return name;
104         }
105
106         /**
107          * Sets the name of this Sone.
108          *
109          * @param name
110          *            The name of this Sone
111          * @return This sone (for method chaining)
112          */
113         public Sone setName(String name) {
114                 this.name = name;
115                 return this;
116         }
117
118         /**
119          * Returns the request URI of this Sone.
120          *
121          * @return The request URI of this Sone
122          */
123         public FreenetURI getRequestUri() {
124                 return requestUri;
125         }
126
127         /**
128          * Sets the request URI of this Sone.
129          *
130          * @param requestUri
131          *            The request URI of this Sone
132          * @return This Sone (for method chaining)
133          */
134         public Sone setRequestUri(FreenetURI requestUri) {
135                 this.requestUri = requestUri;
136                 return this;
137         }
138
139         /**
140          * Returns the insert URI of this Sone.
141          *
142          * @return The insert URI of this Sone
143          */
144         public FreenetURI getInsertUri() {
145                 return insertUri;
146         }
147
148         /**
149          * Sets the insert URI of this Sone.
150          *
151          * @param insertUri
152          *            The insert URI of this Sone
153          * @return This Sone (for method chaining)
154          */
155         public Sone setInsertUri(FreenetURI insertUri) {
156                 this.insertUri = insertUri;
157                 return this;
158         }
159
160         /**
161          * Returns a copy of the profile. If you want to update values in the
162          * profile of this Sone, update the values in the returned {@link Profile}
163          * and use {@link #setProfile(Profile)} to change the profile in this Sone.
164          *
165          * @return A copy of the profile
166          */
167         public Profile getProfile() {
168                 return new Profile(profile);
169         }
170
171         /**
172          * Sets the profile of this Sone. A copy of the given profile is stored so
173          * that subsequent modifications of the given profile are not reflected in
174          * this Sone!
175          *
176          * @param profile
177          *            The profile to set
178          */
179         public synchronized void setProfile(Profile profile) {
180                 this.profile = new Profile(profile);
181                 modificationCounter++;
182         }
183
184         /**
185          * Returns all friend Sones of this Sone.
186          *
187          * @return The friend Sones of this Sone
188          */
189         public Set<Sone> getFriends() {
190                 return Collections.unmodifiableSet(friendSones);
191         }
192
193         /**
194          * Returns whether this Sone has the given Sone as a friend Sone.
195          *
196          * @param friendSone
197          *            The friend Sone to check for
198          * @return {@code true} if this Sone has the given Sone as a friend,
199          *         {@code false} otherwise
200          */
201         public boolean hasFriend(Sone friendSone) {
202                 return friendSones.contains(friendSone);
203         }
204
205         /**
206          * Adds the given Sone as a friend Sone.
207          *
208          * @param friendSone
209          *            The friend Sone to add
210          * @return This Sone (for method chaining)
211          */
212         public synchronized Sone addFriend(Sone friendSone) {
213                 if (!friendSone.equals(this) && friendSones.add(friendSone)) {
214                         modificationCounter++;
215                 }
216                 return this;
217         }
218
219         /**
220          * Removes the given Sone as a friend Sone.
221          *
222          * @param friendSone
223          *            The friend Sone to remove
224          * @return This Sone (for method chaining)
225          */
226         public synchronized Sone removeFriend(Sone friendSone) {
227                 if (friendSones.remove(friendSone)) {
228                         modificationCounter++;
229                 }
230                 return this;
231         }
232
233         /**
234          * Returns the list of posts of this Sone, sorted by time, newest first.
235          *
236          * @return All posts of this Sone
237          */
238         public List<Post> getPosts() {
239                 List<Post> sortedPosts = new ArrayList<Post>(posts);
240                 Collections.sort(sortedPosts, new Comparator<Post>() {
241
242                         @Override
243                         public int compare(Post leftPost, Post rightPost) {
244                                 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
245                         }
246
247                 });
248                 return sortedPosts;
249         }
250
251         /**
252          * Adds the given post to this Sone. The post will not be added if its
253          * {@link Post#getSone() Sone} is not this Sone.
254          *
255          * @param post
256          *            The post to add
257          */
258         public synchronized void addPost(Post post) {
259                 if (post.getSone().equals(this) && posts.add(post)) {
260                         logger.log(Level.FINEST, "Adding %s to “%s”.", new Object[] { post, getName() });
261                         modificationCounter++;
262                 }
263         }
264
265         /**
266          * Removes the given post from this Sone.
267          *
268          * @param post
269          *            The post to remove
270          */
271         public synchronized void removePost(Post post) {
272                 if (post.getSone().equals(this) && posts.remove(post)) {
273                         modificationCounter++;
274                 }
275         }
276
277         /**
278          * Returns all replies this Sone made.
279          *
280          * @return All replies this Sone made
281          */
282         public Set<Reply> getReplies() {
283                 logger.log(Level.FINEST, "Friends of %s: %s", new Object[] { this, friendSones });
284                 return Collections.unmodifiableSet(replies);
285         }
286
287         /**
288          * Adds a reply to this Sone. If the given reply was not made by this Sone,
289          * nothing is added to this Sone.
290          *
291          * @param reply
292          *            The reply to add
293          */
294         public synchronized void addReply(Reply reply) {
295                 if (reply.getSone().equals(this) && replies.add(reply)) {
296                         modificationCounter++;
297                 }
298         }
299
300         /**
301          * Removes a reply from this Sone.
302          *
303          * @param reply
304          *            The reply to remove
305          */
306         public synchronized void removeReply(Reply reply) {
307                 if (reply.getSone().equals(this) && replies.remove(reply)) {
308                         modificationCounter++;
309                 }
310         }
311
312         /**
313          * Returns the modification counter.
314          *
315          * @return The modification counter
316          */
317         public synchronized long getModificationCounter() {
318                 return modificationCounter;
319         }
320
321         /**
322          * Sets the modification counter.
323          *
324          * @param modificationCounter
325          *            The new modification counter
326          */
327         public synchronized void setModificationCounter(long modificationCounter) {
328                 this.modificationCounter = modificationCounter;
329         }
330
331         /**
332          * Updates the suggested edition in both the request URI and the insert URI.
333          *
334          * @param requestUri
335          *            The request URI that resulted from an insert
336          */
337         public void updateUris(FreenetURI requestUri) {
338                 /* TODO - check for the correct URI. */
339                 long latestEdition = requestUri.getSuggestedEdition();
340                 this.requestUri = this.requestUri.setSuggestedEdition(latestEdition);
341                 if (this.insertUri != null) {
342                         this.insertUri = this.insertUri.setSuggestedEdition(latestEdition);
343                 }
344         }
345
346         //
347         // OBJECT METHODS
348         //
349
350         /**
351          * {@inheritDoc}
352          */
353         @Override
354         public int hashCode() {
355                 return id.hashCode();
356         }
357
358         /**
359          * {@inheritDoc}
360          */
361         @Override
362         public boolean equals(Object object) {
363                 if (!(object instanceof Sone)) {
364                         return false;
365                 }
366                 return ((Sone) object).id.equals(id);
367         }
368
369         /**
370          * {@inheritDoc}
371          */
372         @Override
373         public String toString() {
374                 return getClass().getName() + "[id=" + id + ",name=" + name + ",requestUri=" + requestUri + ",insertUri=" + insertUri + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";
375         }
376
377 }