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