Turn Sone into an interface, add in-memory implementation of Sone.
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemorySone.java
1 /*
2  * Sone - MemorySone.java - Copyright © 2010–2012 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.database.memory;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.concurrent.CopyOnWriteArrayList;
26 import java.util.concurrent.CopyOnWriteArraySet;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29
30 import net.pterodactylus.sone.core.Options;
31 import net.pterodactylus.sone.data.Album;
32 import net.pterodactylus.sone.data.Client;
33 import net.pterodactylus.sone.data.Image;
34 import net.pterodactylus.sone.data.Post;
35 import net.pterodactylus.sone.data.PostReply;
36 import net.pterodactylus.sone.data.Profile;
37 import net.pterodactylus.sone.data.Reply;
38 import net.pterodactylus.sone.data.Sone;
39 import net.pterodactylus.sone.database.Database;
40 import net.pterodactylus.sone.freenet.wot.Identity;
41 import net.pterodactylus.util.logging.Logging;
42 import net.pterodactylus.util.validation.Validation;
43 import freenet.keys.FreenetURI;
44
45 /**
46  * Implementation of a {@link Sone} that keeps all added data in memory. A
47  * self-created instance of this class should be converted to a {@link Database}
48  * -based instance of {@link Sone} as soon as possible (unless it was returned
49  * by a {@link MemoryDatabase}).
50  *
51  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
52  */
53 public class MemorySone implements Sone {
54
55         /** The logger. */
56         private static final Logger logger = Logging.getLogger(Sone.class);
57
58         /** The ID of this Sone. */
59         private final String id;
60
61         /** Whether this is a local Sone. */
62         private final boolean local;
63
64         /** The identity of this Sone. */
65         private Identity identity;
66
67         /** The URI under which the Sone is stored in Freenet. */
68         private volatile FreenetURI requestUri;
69
70         /** The URI used to insert a new version of this Sone. */
71         /* This will be null for remote Sones! */
72         private volatile FreenetURI insertUri;
73
74         /** The latest edition of the Sone. */
75         private volatile long latestEdition;
76
77         /** The time of the last inserted update. */
78         private volatile long time;
79
80         /** The status of this Sone. */
81         private volatile SoneStatus status = SoneStatus.unknown;
82
83         /** The profile of this Sone. */
84         private volatile Profile profile = new Profile(this);
85
86         /** The client used by the Sone. */
87         private volatile Client client;
88
89         /** Whether this Sone is known. */
90         private volatile boolean known;
91
92         /** All friend Sones. */
93         private final Set<String> friendSones = new CopyOnWriteArraySet<String>();
94
95         /** All posts. */
96         private final Set<Post> posts = new CopyOnWriteArraySet<Post>();
97
98         /** All replies. */
99         private final Set<PostReply> replies = new CopyOnWriteArraySet<PostReply>();
100
101         /** The IDs of all liked posts. */
102         private final Set<String> likedPostIds = new CopyOnWriteArraySet<String>();
103
104         /** The IDs of all liked replies. */
105         private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
106
107         /** The albums of this Sone. */
108         private final List<Album> albums = new CopyOnWriteArrayList<Album>();
109
110         /** Sone-specific options. */
111         private final Options options = new Options();
112
113         /**
114          * Creates a new Sone.
115          *
116          * @param id
117          *            The ID of the Sone
118          * @param local
119          *            {@code true} if this Sone is local, {@code false} otherwise
120          */
121         public MemorySone(String id, boolean local) {
122                 this.id = id;
123                 this.local = local;
124         }
125
126         //
127         // ACCESSORS
128         //
129
130         /**
131          * {@inheritDoc}
132          */
133         @Override
134         public String getId() {
135                 return id;
136         }
137
138         /**
139          * {@inheritDoc}
140          */
141         @Override
142         public Identity getIdentity() {
143                 return identity;
144         }
145
146         /**
147          * {@inheritDoc}
148          */
149         @Override
150         public Sone setIdentity(Identity identity) throws IllegalArgumentException {
151                 if (!identity.getId().equals(id)) {
152                         throw new IllegalArgumentException("Identity’s ID does not match Sone’s ID!");
153                 }
154                 this.identity = identity;
155                 return this;
156         }
157
158         /**
159          * {@inheritDoc}
160          */
161         @Override
162         public boolean isLocal() {
163                 return local;
164         }
165
166         /**
167          * {@inheritDoc}
168          */
169         @Override
170         public String getName() {
171                 return (identity != null) ? identity.getNickname() : null;
172         }
173
174         /**
175          * {@inheritDoc}
176          */
177         @Override
178         public FreenetURI getRequestUri() {
179                 return (requestUri != null) ? requestUri.setSuggestedEdition(latestEdition) : null;
180         }
181
182         /**
183          * {@inheritDoc}
184          */
185         @Override
186         public Sone setRequestUri(FreenetURI requestUri) {
187                 if (this.requestUri == null) {
188                         this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
189                         return this;
190                 }
191                 if (!this.requestUri.equalsKeypair(requestUri)) {
192                         logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", requestUri, this.requestUri));
193                         return this;
194                 }
195                 return this;
196         }
197
198         /**
199          * {@inheritDoc}
200          */
201         @Override
202         public FreenetURI getInsertUri() {
203                 return (insertUri != null) ? insertUri.setSuggestedEdition(latestEdition) : null;
204         }
205
206         /**
207          * {@inheritDoc}
208          */
209         @Override
210         public Sone setInsertUri(FreenetURI insertUri) {
211                 if (this.insertUri == null) {
212                         this.insertUri = insertUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
213                         return this;
214                 }
215                 if (!this.insertUri.equalsKeypair(insertUri)) {
216                         logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", insertUri, this.insertUri));
217                         return this;
218                 }
219                 return this;
220         }
221
222         /**
223          * {@inheritDoc}
224          */
225         @Override
226         public long getLatestEdition() {
227                 return latestEdition;
228         }
229
230         /**
231          * {@inheritDoc}
232          */
233         @Override
234         public void setLatestEdition(long latestEdition) {
235                 if (!(latestEdition > this.latestEdition)) {
236                         logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
237                         return;
238                 }
239                 this.latestEdition = latestEdition;
240         }
241
242         /**
243          * {@inheritDoc}
244          */
245         @Override
246         public long getTime() {
247                 return time;
248         }
249
250         /**
251          * {@inheritDoc}
252          */
253         @Override
254         public Sone setTime(long time) {
255                 this.time = time;
256                 return this;
257         }
258
259         /**
260          * {@inheritDoc}
261          */
262         @Override
263         public SoneStatus getStatus() {
264                 return status;
265         }
266
267         /**
268          * {@inheritDoc}
269          */
270         @Override
271         public Sone setStatus(SoneStatus status) {
272                 Validation.begin().isNotNull("Sone Status", status).check();
273                 this.status = status;
274                 return this;
275         }
276
277         /**
278          * {@inheritDoc}
279          */
280         @Override
281         public Profile getProfile() {
282                 return new Profile(profile);
283         }
284
285         /**
286          * {@inheritDoc}
287          */
288         @Override
289         public void setProfile(Profile profile) {
290                 this.profile = new Profile(profile);
291         }
292
293         /**
294          * {@inheritDoc}
295          */
296         @Override
297         public Client getClient() {
298                 return client;
299         }
300
301         /**
302          * {@inheritDoc}
303          */
304         @Override
305         public Sone setClient(Client client) {
306                 this.client = client;
307                 return this;
308         }
309
310         /**
311          * {@inheritDoc}
312          */
313         @Override
314         public boolean isKnown() {
315                 return known;
316         }
317
318         /**
319          * {@inheritDoc}
320          */
321         @Override
322         public Sone setKnown(boolean known) {
323                 this.known = known;
324                 return this;
325         }
326
327         /**
328          * {@inheritDoc}
329          */
330         @Override
331         public List<String> getFriends() {
332                 List<String> friends = new ArrayList<String>(friendSones);
333                 return friends;
334         }
335
336         /**
337          * {@inheritDoc}
338          */
339         @Override
340         public boolean hasFriend(String friendSoneId) {
341                 return friendSones.contains(friendSoneId);
342         }
343
344         /**
345          * {@inheritDoc}
346          */
347         @Override
348         public Sone addFriend(String friendSone) {
349                 if (!friendSone.equals(id)) {
350                         friendSones.add(friendSone);
351                 }
352                 return this;
353         }
354
355         /**
356          * {@inheritDoc}
357          */
358         @Override
359         public Sone removeFriend(String friendSoneId) {
360                 friendSones.remove(friendSoneId);
361                 return this;
362         }
363
364         /**
365          * {@inheritDoc}
366          */
367         @Override
368         public List<Post> getPosts() {
369                 List<Post> sortedPosts;
370                 synchronized (this) {
371                         sortedPosts = new ArrayList<Post>(posts);
372                 }
373                 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
374                 return sortedPosts;
375         }
376
377         /**
378          * {@inheritDoc}
379          */
380         @Override
381         public Sone setPosts(Collection<Post> posts) {
382                 synchronized (this) {
383                         this.posts.clear();
384                         this.posts.addAll(posts);
385                 }
386                 return this;
387         }
388
389         /**
390          * {@inheritDoc}
391          */
392         @Override
393         public void addPost(Post post) {
394                 if (post.getSone().equals(this) && posts.add(post)) {
395                         logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
396                 }
397         }
398
399         /**
400          * {@inheritDoc}
401          */
402         @Override
403         public void removePost(Post post) {
404                 if (post.getSone().equals(this)) {
405                         posts.remove(post);
406                 }
407         }
408
409         /**
410          * {@inheritDoc}
411          */
412         @Override
413         public Set<PostReply> getReplies() {
414                 return Collections.unmodifiableSet(replies);
415         }
416
417         /**
418          * {@inheritDoc}
419          */
420         @Override
421         public Sone setReplies(Collection<PostReply> replies) {
422                 this.replies.clear();
423                 this.replies.addAll(replies);
424                 return this;
425         }
426
427         /**
428          * {@inheritDoc}
429          */
430         @Override
431         public void addReply(PostReply reply) {
432                 if (reply.getSone().equals(this)) {
433                         replies.add(reply);
434                 }
435         }
436
437         /**
438          * {@inheritDoc}
439          */
440         @Override
441         public void removeReply(PostReply reply) {
442                 if (reply.getSone().equals(this)) {
443                         replies.remove(reply);
444                 }
445         }
446
447         /**
448          * {@inheritDoc}
449          */
450         @Override
451         public Set<String> getLikedPostIds() {
452                 return Collections.unmodifiableSet(likedPostIds);
453         }
454
455         /**
456          * {@inheritDoc}
457          */
458         @Override
459         public Sone setLikePostIds(Set<String> likedPostIds) {
460                 this.likedPostIds.clear();
461                 this.likedPostIds.addAll(likedPostIds);
462                 return this;
463         }
464
465         /**
466          * {@inheritDoc}
467          */
468         @Override
469         public boolean isLikedPostId(String postId) {
470                 return likedPostIds.contains(postId);
471         }
472
473         /**
474          * {@inheritDoc}
475          */
476         @Override
477         public Sone addLikedPostId(String postId) {
478                 likedPostIds.add(postId);
479                 return this;
480         }
481
482         /**
483          * {@inheritDoc}
484          */
485         @Override
486         public Sone removeLikedPostId(String postId) {
487                 likedPostIds.remove(postId);
488                 return this;
489         }
490
491         /**
492          * {@inheritDoc}
493          */
494         @Override
495         public Set<String> getLikedReplyIds() {
496                 return Collections.unmodifiableSet(likedReplyIds);
497         }
498
499         /**
500          * {@inheritDoc}
501          */
502         @Override
503         public Sone setLikeReplyIds(Set<String> likedReplyIds) {
504                 this.likedReplyIds.clear();
505                 this.likedReplyIds.addAll(likedReplyIds);
506                 return this;
507         }
508
509         /**
510          * {@inheritDoc}
511          */
512         @Override
513         public boolean isLikedReplyId(String replyId) {
514                 return likedReplyIds.contains(replyId);
515         }
516
517         /**
518          * {@inheritDoc}
519          */
520         @Override
521         public Sone addLikedReplyId(String replyId) {
522                 likedReplyIds.add(replyId);
523                 return this;
524         }
525
526         /**
527          * {@inheritDoc}
528          */
529         @Override
530         public Sone removeLikedReplyId(String replyId) {
531                 likedReplyIds.remove(replyId);
532                 return this;
533         }
534
535         /**
536          * {@inheritDoc}
537          */
538         @Override
539         public List<Album> getAlbums() {
540                 return Collections.unmodifiableList(albums);
541         }
542
543         /**
544          * {@inheritDoc}
545          */
546         @Override
547         public List<Album> getAllAlbums() {
548                 List<Album> flatAlbums = new ArrayList<Album>();
549                 flatAlbums.addAll(albums);
550                 int lastAlbumIndex = 0;
551                 while (lastAlbumIndex < flatAlbums.size()) {
552                         int previousAlbumCount = flatAlbums.size();
553                         for (Album album : new ArrayList<Album>(flatAlbums.subList(lastAlbumIndex, flatAlbums.size()))) {
554                                 flatAlbums.addAll(album.getAlbums());
555                         }
556                         lastAlbumIndex = previousAlbumCount;
557                 }
558                 return flatAlbums;
559         }
560
561         /**
562          * {@inheritDoc}
563          */
564         @Override
565         public List<Image> getAllImages() {
566                 List<Image> allImages = new ArrayList<Image>();
567                 for (Album album : getAllAlbums()) {
568                         allImages.addAll(album.getImages());
569                 }
570                 return allImages;
571         }
572
573         /**
574          * {@inheritDoc}
575          */
576         @Override
577         public void addAlbum(Album album) {
578                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).check();
579                 albums.add(album);
580         }
581
582         /**
583          * {@inheritDoc}
584          */
585         @Override
586         public void setAlbums(Collection<? extends Album> albums) {
587                 Validation.begin().isNotNull("Albums", albums).check();
588                 this.albums.clear();
589                 for (Album album : albums) {
590                         addAlbum(album);
591                 }
592         }
593
594         /**
595          * {@inheritDoc}
596          */
597         @Override
598         public void removeAlbum(Album album) {
599                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).check();
600                 albums.remove(album);
601         }
602
603         /**
604          * {@inheritDoc}
605          */
606         @Override
607         public Album moveAlbumUp(Album album) {
608                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).isNull("Album Parent", album.getParent()).check();
609                 int oldIndex = albums.indexOf(album);
610                 if (oldIndex <= 0) {
611                         return null;
612                 }
613                 albums.remove(oldIndex);
614                 albums.add(oldIndex - 1, album);
615                 return albums.get(oldIndex);
616         }
617
618         /**
619          * {@inheritDoc}
620          */
621         @Override
622         public Album moveAlbumDown(Album album) {
623                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).isNull("Album Parent", album.getParent()).check();
624                 int oldIndex = albums.indexOf(album);
625                 if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
626                         return null;
627                 }
628                 albums.remove(oldIndex);
629                 albums.add(oldIndex + 1, album);
630                 return albums.get(oldIndex);
631         }
632
633         /**
634          * {@inheritDoc}
635          */
636         @Override
637         public Options getOptions() {
638                 return options;
639         }
640
641         //
642         // FINGERPRINTABLE METHODS
643         //
644
645         /**
646          * {@inheritDoc}
647          */
648         @Override
649         public synchronized String getFingerprint() {
650                 StringBuilder fingerprint = new StringBuilder();
651                 fingerprint.append(profile.getFingerprint());
652
653                 fingerprint.append("Posts(");
654                 for (Post post : getPosts()) {
655                         fingerprint.append("Post(").append(post.getId()).append(')');
656                 }
657                 fingerprint.append(")");
658
659                 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
660                 Collections.sort(replies, Reply.TIME_COMPARATOR);
661                 fingerprint.append("Replies(");
662                 for (PostReply reply : replies) {
663                         fingerprint.append("Reply(").append(reply.getId()).append(')');
664                 }
665                 fingerprint.append(')');
666
667                 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
668                 Collections.sort(likedPostIds);
669                 fingerprint.append("LikedPosts(");
670                 for (String likedPostId : likedPostIds) {
671                         fingerprint.append("Post(").append(likedPostId).append(')');
672                 }
673                 fingerprint.append(')');
674
675                 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
676                 Collections.sort(likedReplyIds);
677                 fingerprint.append("LikedReplies(");
678                 for (String likedReplyId : likedReplyIds) {
679                         fingerprint.append("Reply(").append(likedReplyId).append(')');
680                 }
681                 fingerprint.append(')');
682
683                 fingerprint.append("Albums(");
684                 for (Album album : albums) {
685                         fingerprint.append(album.getFingerprint());
686                 }
687                 fingerprint.append(')');
688
689                 return fingerprint.toString();
690         }
691
692         //
693         // INTERFACE Comparable<Sone>
694         //
695
696         /**
697          * {@inheritDoc}
698          */
699         @Override
700         public int compareTo(Sone sone) {
701                 return NICE_NAME_COMPARATOR.compare(this, sone);
702         }
703
704         //
705         // OBJECT METHODS
706         //
707
708         /**
709          * {@inheritDoc}
710          */
711         @Override
712         public int hashCode() {
713                 return id.hashCode();
714         }
715
716         /**
717          * {@inheritDoc}
718          */
719         @Override
720         public boolean equals(Object object) {
721                 if (!(object instanceof Sone)) {
722                         return false;
723                 }
724                 return ((Sone) object).getId().equals(id);
725         }
726
727         /**
728          * {@inheritDoc}
729          */
730         @Override
731         public String toString() {
732                 return getClass().getName() + "[identity=" + identity + ",requestUri=" + requestUri + ",insertUri(" + String.valueOf(insertUri).length() + "),friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";
733         }
734
735 }