Remove javadoc comments from overriding methods.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / DefaultSone.java
1 /*
2  * Sone - SoneImpl.java - Copyright © 2010–2013 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.impl;
19
20 import static com.google.common.base.Preconditions.checkNotNull;
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Set;
27 import java.util.concurrent.CopyOnWriteArraySet;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30
31 import net.pterodactylus.sone.core.Options;
32 import net.pterodactylus.sone.data.Album;
33 import net.pterodactylus.sone.data.Client;
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.AlbumBuilder;
40 import net.pterodactylus.sone.database.Database;
41 import net.pterodactylus.sone.database.PostBuilder;
42 import net.pterodactylus.sone.database.PostReplyBuilder;
43 import net.pterodactylus.sone.freenet.wot.Identity;
44 import net.pterodactylus.util.logging.Logging;
45
46 import freenet.keys.FreenetURI;
47
48 import com.google.common.base.Optional;
49 import com.google.common.hash.Hasher;
50 import com.google.common.hash.Hashing;
51
52 /**
53  * Dumb, store-everything-in-memory {@link Sone} implementation.
54  *
55  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
56  */
57 public class DefaultSone implements Sone {
58
59         /** The logger. */
60         private static final Logger logger = Logging.getLogger(DefaultSone.class);
61
62         /** The database. */
63         private final Database database;
64
65         /** The ID of this Sone. */
66         private final String id;
67
68         /** Whether the Sone is local. */
69         private final boolean local;
70
71         /** The identity of this Sone. */
72         private Identity identity;
73
74         /** The URI under which the Sone is stored in Freenet. */
75         private volatile FreenetURI requestUri;
76
77         /** The URI used to insert a new version of this Sone. */
78         /* This will be null for remote Sones! */
79         private volatile FreenetURI insertUri;
80
81         /** The latest edition of the Sone. */
82         private volatile long latestEdition;
83
84         /** The time of the last inserted update. */
85         private volatile long time;
86
87         /** The status of this Sone. */
88         private volatile SoneStatus status = SoneStatus.unknown;
89
90         /** The profile of this Sone. */
91         private volatile Profile profile = new Profile(this);
92
93         /** The client used by the Sone. */
94         private volatile Client client;
95
96         /** Whether this Sone is known. */
97         private volatile boolean known;
98
99         /** All friend Sones. */
100         private final Set<String> friendSones = new CopyOnWriteArraySet<String>();
101
102         /** All posts. */
103         private final Set<Post> posts = new CopyOnWriteArraySet<Post>();
104
105         /** All replies. */
106         private final Set<PostReply> replies = new CopyOnWriteArraySet<PostReply>();
107
108         /** The IDs of all liked posts. */
109         private final Set<String> likedPostIds = new CopyOnWriteArraySet<String>();
110
111         /** The IDs of all liked replies. */
112         private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
113
114         /** The root album containing all albums. */
115         private final Album rootAlbum;
116
117         /** Sone-specific options. */
118         private Options options = new Options();
119
120         /**
121          * Creates a new Sone.
122          *
123          * @param id
124          *              The ID of the Sone
125          * @param local
126          *              {@code true} if the Sone is a local Sone, {@code false} otherwise
127          */
128         public DefaultSone(Database database, String id, boolean local) {
129                 this.database = database;
130                 this.id = id;
131                 this.local = local;
132                 rootAlbum = new DefaultAlbumBuilder(database, this, null).build();
133         }
134
135         //
136         // ACCESSORS
137         //
138
139         public String getId() {
140                 return id;
141         }
142
143         public Identity getIdentity() {
144                 return identity;
145         }
146
147         public DefaultSone setIdentity(Identity identity) throws IllegalArgumentException {
148                 if (!identity.getId().equals(id)) {
149                         throw new IllegalArgumentException("Identity’s ID does not match Sone’s ID!");
150                 }
151                 this.identity = identity;
152                 return this;
153         }
154
155         public String getName() {
156                 return (identity != null) ? identity.getNickname() : null;
157         }
158
159         public boolean isLocal() {
160                 return local;
161         }
162
163         public FreenetURI getRequestUri() {
164                 return (requestUri != null) ? requestUri.setSuggestedEdition(latestEdition) : null;
165         }
166
167         public Sone setRequestUri(FreenetURI requestUri) {
168                 if (this.requestUri == null) {
169                         this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
170                         return this;
171                 }
172                 if (!this.requestUri.equalsKeypair(requestUri)) {
173                         logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", requestUri, this.requestUri));
174                         return this;
175                 }
176                 return this;
177         }
178
179         public FreenetURI getInsertUri() {
180                 return (insertUri != null) ? insertUri.setSuggestedEdition(latestEdition) : null;
181         }
182
183         public Sone setInsertUri(FreenetURI insertUri) {
184                 if (this.insertUri == null) {
185                         this.insertUri = insertUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
186                         return this;
187                 }
188                 if (!this.insertUri.equalsKeypair(insertUri)) {
189                         logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", insertUri, this.insertUri));
190                         return this;
191                 }
192                 return this;
193         }
194
195         public long getLatestEdition() {
196                 return latestEdition;
197         }
198
199         public void setLatestEdition(long latestEdition) {
200                 if (!(latestEdition > this.latestEdition)) {
201                         logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition));
202                         return;
203                 }
204                 this.latestEdition = latestEdition;
205         }
206
207         public long getTime() {
208                 return time;
209         }
210
211         public Sone setTime(long time) {
212                 this.time = time;
213                 return this;
214         }
215
216         public SoneStatus getStatus() {
217                 return status;
218         }
219
220         public Sone setStatus(SoneStatus status) {
221                 this.status = checkNotNull(status, "status must not be null");
222                 return this;
223         }
224
225         public Profile getProfile() {
226                 return new Profile(profile);
227         }
228
229         public void setProfile(Profile profile) {
230                 this.profile = new Profile(profile);
231         }
232
233         public Client getClient() {
234                 return client;
235         }
236
237         public Sone setClient(Client client) {
238                 this.client = client;
239                 return this;
240         }
241
242         public boolean isKnown() {
243                 return known;
244         }
245
246         public Sone setKnown(boolean known) {
247                 this.known = known;
248                 return this;
249         }
250
251         public List<String> getFriends() {
252                 List<String> friends = new ArrayList<String>(friendSones);
253                 return friends;
254         }
255
256         public boolean hasFriend(String friendSoneId) {
257                 return friendSones.contains(friendSoneId);
258         }
259
260         public Sone addFriend(String friendSone) {
261                 if (!friendSone.equals(id)) {
262                         friendSones.add(friendSone);
263                 }
264                 return this;
265         }
266
267         public Sone removeFriend(String friendSoneId) {
268                 friendSones.remove(friendSoneId);
269                 return this;
270         }
271
272         public List<Post> getPosts() {
273                 List<Post> sortedPosts;
274                 synchronized (this) {
275                         sortedPosts = new ArrayList<Post>(posts);
276                 }
277                 Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
278                 return sortedPosts;
279         }
280
281         public Sone setPosts(Collection<Post> posts) {
282                 synchronized (this) {
283                         this.posts.clear();
284                         this.posts.addAll(posts);
285                 }
286                 return this;
287         }
288
289         public void addPost(Post post) {
290                 if (post.getSone().equals(this) && posts.add(post)) {
291                         logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName()));
292                 }
293         }
294
295         public void removePost(Post post) {
296                 if (post.getSone().equals(this)) {
297                         posts.remove(post);
298                 }
299         }
300
301         public Set<PostReply> getReplies() {
302                 return Collections.unmodifiableSet(replies);
303         }
304
305         public Sone setReplies(Collection<PostReply> replies) {
306                 this.replies.clear();
307                 this.replies.addAll(replies);
308                 return this;
309         }
310
311         public void addReply(PostReply reply) {
312                 if (reply.getSone().equals(this)) {
313                         replies.add(reply);
314                 }
315         }
316
317         public void removeReply(PostReply reply) {
318                 if (reply.getSone().equals(this)) {
319                         replies.remove(reply);
320                 }
321         }
322
323         public Set<String> getLikedPostIds() {
324                 return Collections.unmodifiableSet(likedPostIds);
325         }
326
327         public Sone setLikePostIds(Set<String> likedPostIds) {
328                 this.likedPostIds.clear();
329                 this.likedPostIds.addAll(likedPostIds);
330                 return this;
331         }
332
333         public boolean isLikedPostId(String postId) {
334                 return likedPostIds.contains(postId);
335         }
336
337         public Sone addLikedPostId(String postId) {
338                 likedPostIds.add(postId);
339                 return this;
340         }
341
342         public Sone removeLikedPostId(String postId) {
343                 likedPostIds.remove(postId);
344                 return this;
345         }
346
347         public Set<String> getLikedReplyIds() {
348                 return Collections.unmodifiableSet(likedReplyIds);
349         }
350
351         public Sone setLikeReplyIds(Set<String> likedReplyIds) {
352                 this.likedReplyIds.clear();
353                 this.likedReplyIds.addAll(likedReplyIds);
354                 return this;
355         }
356
357         public boolean isLikedReplyId(String replyId) {
358                 return likedReplyIds.contains(replyId);
359         }
360
361         public Sone addLikedReplyId(String replyId) {
362                 likedReplyIds.add(replyId);
363                 return this;
364         }
365
366         public Sone removeLikedReplyId(String replyId) {
367                 likedReplyIds.remove(replyId);
368                 return this;
369         }
370
371         public Album getRootAlbum() {
372                 return rootAlbum;
373         }
374
375         public Options getOptions() {
376                 return options;
377         }
378
379         /* TODO - remove this method again, maybe add an option provider */
380         public void setOptions(Options options) {
381                 this.options = options;
382         }
383
384         @Override
385         public AlbumBuilder newAlbumBuilder() {
386                 return new DefaultAlbumBuilder(database, this, rootAlbum.getId());
387         }
388
389         public PostBuilder newPostBuilder() {
390                 return new DefaultPostBuilder(database, getId()) {
391                         @Override
392                         public Post build(Optional<PostCreated> postCreated) {
393                                 Post post = super.build(postCreated);
394                                 database.storePost(post);
395                                 return post;
396                         }
397                 };
398         }
399
400         @Override
401         public PostReplyBuilder newPostReplyBuilder(String postId) throws IllegalStateException {
402                 return new DefaultPostReplyBuilder(database, getId(), postId) {
403                         @Override
404                         public PostReply build(Optional<PostReplyCreated> postReplyCreated) {
405                                 PostReply postReply = super.build(postReplyCreated);
406                                 database.storePostReply(postReply);
407                                 return postReply;
408                         }
409                 };
410         }
411
412         //
413         // FINGERPRINTABLE METHODS
414         //
415
416         @Override
417         public synchronized String getFingerprint() {
418                 Hasher hash = Hashing.sha256().newHasher();
419                 hash.putString(profile.getFingerprint());
420
421                 hash.putString("Posts(");
422                 for (Post post : getPosts()) {
423                         hash.putString("Post(").putString(post.getId()).putString(")");
424                 }
425                 hash.putString(")");
426
427                 List<PostReply> replies = new ArrayList<PostReply>(getReplies());
428                 Collections.sort(replies, Reply.TIME_COMPARATOR);
429                 hash.putString("Replies(");
430                 for (PostReply reply : replies) {
431                         hash.putString("Reply(").putString(reply.getId()).putString(")");
432                 }
433                 hash.putString(")");
434
435                 List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
436                 Collections.sort(likedPostIds);
437                 hash.putString("LikedPosts(");
438                 for (String likedPostId : likedPostIds) {
439                         hash.putString("Post(").putString(likedPostId).putString(")");
440                 }
441                 hash.putString(")");
442
443                 List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
444                 Collections.sort(likedReplyIds);
445                 hash.putString("LikedReplies(");
446                 for (String likedReplyId : likedReplyIds) {
447                         hash.putString("Reply(").putString(likedReplyId).putString(")");
448                 }
449                 hash.putString(")");
450
451                 hash.putString("Albums(");
452                 for (Album album : rootAlbum.getAlbums()) {
453                         if (!Album.NOT_EMPTY.apply(album)) {
454                                 continue;
455                         }
456                         hash.putString(album.getFingerprint());
457                 }
458                 hash.putString(")");
459
460                 return hash.hash().toString();
461         }
462
463         //
464         // INTERFACE Comparable<Sone>
465         //
466
467         @Override
468         public int compareTo(Sone sone) {
469                 return NICE_NAME_COMPARATOR.compare(this, sone);
470         }
471
472         //
473         // OBJECT METHODS
474         //
475
476         @Override
477         public int hashCode() {
478                 return id.hashCode();
479         }
480
481         @Override
482         public boolean equals(Object object) {
483                 if (!(object instanceof Sone)) {
484                         return false;
485                 }
486                 return ((Sone) object).getId().equals(id);
487         }
488
489         @Override
490         public String toString() {
491                 return getClass().getName() + "[identity=" + identity + ",requestUri=" + requestUri + ",insertUri(" + String.valueOf(insertUri).length() + "),friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";
492         }
493
494 }