Adding and removing friends does not modify a Sone anymore.
[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.Collection;
22 import java.util.Collections;
23 import java.util.Comparator;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
27 import java.util.UUID;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30
31 import net.pterodactylus.sone.template.SoneAccessor;
32 import net.pterodactylus.util.logging.Logging;
33 import freenet.keys.FreenetURI;
34
35 /**
36  * A Sone defines everything about a user: her profile, her status updates, her
37  * replies, her likes and dislikes, etc.
38  * <p>
39  * Operations that modify the Sone need to synchronize on the Sone in question.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class Sone {
44
45         /** The logger. */
46         private static final Logger logger = Logging.getLogger(Sone.class);
47
48         /** A GUID for this Sone. */
49         private final UUID id;
50
51         /** The name of this Sone. */
52         private String name;
53
54         /** The URI under which the Sone is stored in Freenet. */
55         private FreenetURI requestUri;
56
57         /** The URI used to insert a new version of this Sone. */
58         /* This will be null for remote Sones! */
59         private FreenetURI insertUri;
60
61         /** The time of the last inserted update. */
62         private long time;
63
64         /** The profile of this Sone. */
65         private Profile profile;
66
67         /** All friend Sones. */
68         private final Set<Sone> friendSones = new HashSet<Sone>();
69
70         /** All posts. */
71         private final Set<Post> posts = new HashSet<Post>();
72
73         /** All replies. */
74         private final Set<Reply> replies = new HashSet<Reply>();
75
76         /** The IDs of all blocked Sones. */
77         private final Set<String> blockedSoneIds = new HashSet<String>();
78
79         /** Modification count. */
80         private volatile long modificationCounter = 0;
81
82         /**
83          * Creates a new Sone.
84          *
85          * @param id
86          *            The ID of this Sone
87          */
88         public Sone(String id) {
89                 this.id = UUID.fromString(id);
90         }
91
92         //
93         // ACCESSORS
94         //
95
96         /**
97          * Returns the ID of this Sone.
98          *
99          * @return The ID of this Sone
100          */
101         public String getId() {
102                 return id.toString();
103         }
104
105         /**
106          * Returns the name of this Sone.
107          *
108          * @return The name of this Sone
109          */
110         public String getName() {
111                 return name;
112         }
113
114         /**
115          * Sets the name of this Sone.
116          *
117          * @param name
118          *            The name of this Sone
119          * @return This sone (for method chaining)
120          */
121         public Sone setName(String name) {
122                 this.name = name;
123                 return this;
124         }
125
126         /**
127          * Returns the request URI of this Sone.
128          *
129          * @return The request URI of this Sone
130          */
131         public FreenetURI getRequestUri() {
132                 return requestUri;
133         }
134
135         /**
136          * Sets the request URI of this Sone.
137          *
138          * @param requestUri
139          *            The request URI of this Sone
140          * @return This Sone (for method chaining)
141          */
142         public Sone setRequestUri(FreenetURI requestUri) {
143                 this.requestUri = requestUri;
144                 return this;
145         }
146
147         /**
148          * Returns the insert URI of this Sone.
149          *
150          * @return The insert URI of this Sone
151          */
152         public FreenetURI getInsertUri() {
153                 return insertUri;
154         }
155
156         /**
157          * Sets the insert URI of this Sone.
158          *
159          * @param insertUri
160          *            The insert URI of this Sone
161          * @return This Sone (for method chaining)
162          */
163         public Sone setInsertUri(FreenetURI insertUri) {
164                 this.insertUri = insertUri;
165                 return this;
166         }
167
168         /**
169          * Return the time of the last inserted update of this Sone.
170          *
171          * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
172          */
173         public long getTime() {
174                 return time;
175         }
176
177         /**
178          * Sets the time of the last inserted update of this Sone.
179          *
180          * @param time
181          *            The time of the update (in milliseconds since Jan 1, 1970 UTC)
182          * @return This Sone (for method chaining)
183          */
184         public Sone setTime(long time) {
185                 this.time = time;
186                 return this;
187         }
188
189         /**
190          * Returns a copy of the profile. If you want to update values in the
191          * profile of this Sone, update the values in the returned {@link Profile}
192          * and use {@link #setProfile(Profile)} to change the profile in this Sone.
193          *
194          * @return A copy of the profile
195          */
196         public Profile getProfile() {
197                 return new Profile(profile);
198         }
199
200         /**
201          * Sets the profile of this Sone. A copy of the given profile is stored so
202          * that subsequent modifications of the given profile are not reflected in
203          * this Sone!
204          *
205          * @param profile
206          *            The profile to set
207          */
208         public synchronized void setProfile(Profile profile) {
209                 this.profile = new Profile(profile);
210                 modificationCounter++;
211         }
212
213         /**
214          * Returns all friend Sones of this Sone.
215          *
216          * @return The friend Sones of this Sone
217          */
218         public List<Sone> getFriends() {
219                 List<Sone> friends = new ArrayList<Sone>(friendSones);
220                 Collections.sort(friends, new Comparator<Sone>() {
221
222                         @Override
223                         public int compare(Sone leftSone, Sone rightSone) {
224                                 int diff = SoneAccessor.getNiceName(leftSone).compareTo(SoneAccessor.getNiceName(rightSone));
225                                 if (diff != 0) {
226                                         return diff;
227                                 }
228                                 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightSone.getTime() - leftSone.getTime()));
229                         }
230                 });
231                 return friends;
232         }
233
234         /**
235          * Sets all friends of this Sone at once.
236          *
237          * @param friends
238          *            The new (and only) friends of this Sone
239          * @return This Sone (for method chaining)
240          */
241         public Sone setFriends(Collection<Sone> friends) {
242                 friendSones.clear();
243                 friendSones.addAll(friends);
244                 return this;
245         }
246
247         /**
248          * Returns whether this Sone has the given Sone as a friend Sone.
249          *
250          * @param friendSone
251          *            The friend Sone to check for
252          * @return {@code true} if this Sone has the given Sone as a friend,
253          *         {@code false} otherwise
254          */
255         public boolean hasFriend(Sone friendSone) {
256                 return friendSones.contains(friendSone);
257         }
258
259         /**
260          * Adds the given Sone as a friend Sone.
261          *
262          * @param friendSone
263          *            The friend Sone to add
264          * @return This Sone (for method chaining)
265          */
266         public Sone addFriend(Sone friendSone) {
267                 if (!friendSone.equals(this)) {
268                         friendSones.add(friendSone);
269                 }
270                 return this;
271         }
272
273         /**
274          * Removes the given Sone as a friend Sone.
275          *
276          * @param friendSone
277          *            The friend Sone to remove
278          * @return This Sone (for method chaining)
279          */
280         public Sone removeFriend(Sone friendSone) {
281                 friendSones.remove(friendSone);
282                 return this;
283         }
284
285         /**
286          * Returns the list of posts of this Sone, sorted by time, newest first.
287          *
288          * @return All posts of this Sone
289          */
290         public List<Post> getPosts() {
291                 List<Post> sortedPosts = new ArrayList<Post>(posts);
292                 Collections.sort(sortedPosts, new Comparator<Post>() {
293
294                         @Override
295                         public int compare(Post leftPost, Post rightPost) {
296                                 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
297                         }
298
299                 });
300                 return sortedPosts;
301         }
302
303         /**
304          * Sets all posts of this Sone at once.
305          *
306          * @param posts
307          *            The new (and only) posts of this Sone
308          * @return This Sone (for method chaining)
309          */
310         public synchronized Sone setPosts(Collection<Post> posts) {
311                 this.posts.clear();
312                 this.posts.addAll(posts);
313                 modificationCounter++;
314                 return this;
315         }
316
317         /**
318          * Adds the given post to this Sone. The post will not be added if its
319          * {@link Post#getSone() Sone} is not this Sone.
320          *
321          * @param post
322          *            The post to add
323          */
324         public synchronized void addPost(Post post) {
325                 if (post.getSone().equals(this) && posts.add(post)) {
326                         logger.log(Level.FINEST, "Adding %s to “%s”.", new Object[] { post, getName() });
327                         modificationCounter++;
328                 }
329         }
330
331         /**
332          * Removes the given post from this Sone.
333          *
334          * @param post
335          *            The post to remove
336          */
337         public synchronized void removePost(Post post) {
338                 if (post.getSone().equals(this) && posts.remove(post)) {
339                         modificationCounter++;
340                 }
341         }
342
343         /**
344          * Returns all replies this Sone made.
345          *
346          * @return All replies this Sone made
347          */
348         public Set<Reply> getReplies() {
349                 logger.log(Level.FINEST, "Friends of %s: %s", new Object[] { this, friendSones });
350                 return Collections.unmodifiableSet(replies);
351         }
352
353         /**
354          * Sets all replies of this Sone at once.
355          *
356          * @param replies
357          *            The new (and only) replies of this Sone
358          * @return This Sone (for method chaining)
359          */
360         public synchronized Sone setReplies(Collection<Reply> replies) {
361                 this.replies.clear();
362                 this.replies.addAll(replies);
363                 modificationCounter++;
364                 return this;
365         }
366
367         /**
368          * Adds a reply to this Sone. If the given reply was not made by this Sone,
369          * nothing is added to this Sone.
370          *
371          * @param reply
372          *            The reply to add
373          */
374         public synchronized void addReply(Reply reply) {
375                 if (reply.getSone().equals(this) && replies.add(reply)) {
376                         modificationCounter++;
377                 }
378         }
379
380         /**
381          * Removes a reply from this Sone.
382          *
383          * @param reply
384          *            The reply to remove
385          */
386         public synchronized void removeReply(Reply reply) {
387                 if (reply.getSone().equals(this) && replies.remove(reply)) {
388                         modificationCounter++;
389                 }
390         }
391
392         /**
393          * Returns the IDs of all blocked Sones. These Sones will not propagated
394          * using the “known Sones” mechanism.
395          *
396          * @return The IDs of all blocked Sones
397          */
398         public Set<String> getBlockedSoneIds() {
399                 return Collections.unmodifiableSet(blockedSoneIds);
400         }
401
402         /**
403          * Returns whether the given Sone ID is blocked.
404          *
405          * @param soneId
406          *            The Sone ID to check
407          * @return {@code true} if the given Sone ID is blocked, {@code false}
408          *         otherwise
409          */
410         public boolean isSoneBlocked(String soneId) {
411                 return blockedSoneIds.contains(soneId);
412         }
413
414         /**
415          * Adds the given ID to the list of blocked IDs.
416          *
417          * @param soneId
418          *            The Sone ID to block
419          */
420         public synchronized void addBlockedSoneId(String soneId) {
421                 if (blockedSoneIds.add(soneId)) {
422                         modificationCounter++;
423                 }
424         }
425
426         /**
427          * Removes the given ID from the list of blocked IDs.
428          *
429          * @param soneId
430          *            The Sone ID to unblock
431          */
432         public synchronized void removeBlockedSoneId(String soneId) {
433                 if (blockedSoneIds.remove(soneId)) {
434                         modificationCounter++;
435                 }
436         }
437
438         /**
439          * Returns the modification counter.
440          *
441          * @return The modification counter
442          */
443         public synchronized long getModificationCounter() {
444                 return modificationCounter;
445         }
446
447         /**
448          * Sets the modification counter.
449          *
450          * @param modificationCounter
451          *            The new modification counter
452          */
453         public synchronized void setModificationCounter(long modificationCounter) {
454                 this.modificationCounter = modificationCounter;
455         }
456
457         /**
458          * Updates the suggested edition in both the request URI and the insert URI.
459          *
460          * @param requestUri
461          *            The request URI that resulted from an insert
462          */
463         public void updateUris(FreenetURI requestUri) {
464                 /* TODO - check for the correct URI. */
465                 long latestEdition = requestUri.getSuggestedEdition();
466                 this.requestUri = this.requestUri.setSuggestedEdition(latestEdition);
467                 if (this.insertUri != null) {
468                         this.insertUri = this.insertUri.setSuggestedEdition(latestEdition);
469                 }
470         }
471
472         //
473         // OBJECT METHODS
474         //
475
476         /**
477          * {@inheritDoc}
478          */
479         @Override
480         public int hashCode() {
481                 return id.hashCode();
482         }
483
484         /**
485          * {@inheritDoc}
486          */
487         @Override
488         public boolean equals(Object object) {
489                 if (!(object instanceof Sone)) {
490                         return false;
491                 }
492                 return ((Sone) object).id.equals(id);
493         }
494
495         /**
496          * {@inheritDoc}
497          */
498         @Override
499         public String toString() {
500                 return getClass().getName() + "[id=" + id + ",name=" + name + ",requestUri=" + requestUri + ",insertUri=" + insertUri + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";
501         }
502
503 }