X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2Fimpl%2FSoneImpl.java;h=1069550b7c071e40c7173e3cfe2f8e01824b8e4e;hb=refs%2Ftags%2Fv81;hp=5024be2481c0affd999c0276e815d6ffc42fee30;hpb=419098bcd6215125408b29e60bd888e60979d37b;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java
index 5024be2..1069550 100644
--- a/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java
+++ b/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java
@@ -1,5 +1,5 @@
/*
- * Sone - SoneImpl.java - Copyright © 2010â2015 David Roden
+ * Sone - SoneImpl.java - Copyright © 2010â2020 David Roden
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -19,6 +19,7 @@ package net.pterodactylus.sone.data.impl;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.lang.String.format;
+import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.logging.Logger.getLogger;
import java.net.MalformedURLException;
@@ -31,6 +32,9 @@ import java.util.concurrent.CopyOnWriteArraySet;
import java.util.logging.Level;
import java.util.logging.Logger;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.Client;
import net.pterodactylus.sone.data.Post;
@@ -53,8 +57,6 @@ import com.google.common.hash.Hashing;
* {@link Sone} implementation.
*
* Operations that modify the Sone need to synchronize on the Sone in question.
- *
- * @author David âBombeâ Roden
*/
public class SoneImpl implements Sone {
@@ -92,16 +94,16 @@ public class SoneImpl implements Sone {
private volatile boolean known;
/** All posts. */
- private final Set posts = new CopyOnWriteArraySet();
+ private final Set posts = new CopyOnWriteArraySet<>();
/** All replies. */
- private final Set replies = new CopyOnWriteArraySet();
+ private final Set replies = new CopyOnWriteArraySet<>();
/** The IDs of all liked posts. */
- private final Set likedPostIds = new CopyOnWriteArraySet();
+ private final Set likedPostIds = new CopyOnWriteArraySet<>();
/** The IDs of all liked replies. */
- private final Set likedReplyIds = new CopyOnWriteArraySet();
+ private final Set likedReplyIds = new CopyOnWriteArraySet<>();
/** The root album containing all albums. */
private final Album rootAlbum = new AlbumImpl(this);
@@ -134,6 +136,7 @@ public class SoneImpl implements Sone {
*
* @return The identity of this Sone
*/
+ @Nonnull
public String getId() {
return id;
}
@@ -143,6 +146,7 @@ public class SoneImpl implements Sone {
*
* @return The identity of this Sone
*/
+ @Nonnull
public Identity getIdentity() {
return identity;
}
@@ -152,6 +156,7 @@ public class SoneImpl implements Sone {
*
* @return The name of this Sone
*/
+ @Nonnull
public String getName() {
return (identity != null) ? identity.getNickname() : null;
}
@@ -170,6 +175,7 @@ public class SoneImpl implements Sone {
*
* @return The request URI of this Sone
*/
+ @Nonnull
public FreenetURI getRequestUri() {
try {
return new FreenetURI(getIdentity().getRequestUri())
@@ -189,6 +195,7 @@ public class SoneImpl implements Sone {
*
* @return The insert URI of this Sone
*/
+ @Nullable
public FreenetURI getInsertUri() {
if (!isLocal()) {
return null;
@@ -244,6 +251,7 @@ public class SoneImpl implements Sone {
* The time of the update (in milliseconds since Jan 1, 1970 UTC)
* @return This Sone (for method chaining)
*/
+ @Nonnull
public Sone setTime(long time) {
this.time = time;
return this;
@@ -254,6 +262,7 @@ public class SoneImpl implements Sone {
*
* @return The status of this Sone
*/
+ @Nonnull
public SoneStatus getStatus() {
return status;
}
@@ -267,7 +276,8 @@ public class SoneImpl implements Sone {
* @throws IllegalArgumentException
* if {@code status} is {@code null}
*/
- public Sone setStatus(SoneStatus status) {
+ @Nonnull
+ public Sone setStatus(@Nonnull SoneStatus status) {
this.status = checkNotNull(status, "status must not be null");
return this;
}
@@ -279,6 +289,7 @@ public class SoneImpl implements Sone {
*
* @return A copy of the profile
*/
+ @Nonnull
public Profile getProfile() {
return new Profile(profile);
}
@@ -291,7 +302,7 @@ public class SoneImpl implements Sone {
* @param profile
* The profile to set
*/
- public void setProfile(Profile profile) {
+ public void setProfile(@Nonnull Profile profile) {
this.profile = new Profile(profile);
}
@@ -300,6 +311,7 @@ public class SoneImpl implements Sone {
*
* @return The client used by this Sone, or {@code null}
*/
+ @Nullable
public Client getClient() {
return client;
}
@@ -311,7 +323,8 @@ public class SoneImpl implements Sone {
* The client used by this Sone, or {@code null}
* @return This Sone (for method chaining)
*/
- public Sone setClient(Client client) {
+ @Nonnull
+ public Sone setClient(@Nullable Client client) {
this.client = client;
return this;
}
@@ -332,6 +345,7 @@ public class SoneImpl implements Sone {
* {@code true} if this Sone is known, {@code false} otherwise
* @return This Sone
*/
+ @Nonnull
public Sone setKnown(boolean known) {
this.known = known;
return this;
@@ -342,6 +356,7 @@ public class SoneImpl implements Sone {
*
* @return The friend Sones of this Sone
*/
+ @Nonnull
public Collection getFriends() {
return database.getFriends(this);
}
@@ -354,7 +369,7 @@ public class SoneImpl implements Sone {
* @return {@code true} if this Sone has the given Sone as a friend, {@code
* false} otherwise
*/
- public boolean hasFriend(String friendSoneId) {
+ public boolean hasFriend(@Nonnull String friendSoneId) {
return database.isFriend(this, friendSoneId);
}
@@ -363,12 +378,13 @@ public class SoneImpl implements Sone {
*
* @return All posts of this Sone
*/
+ @Nonnull
public List getPosts() {
List sortedPosts;
synchronized (this) {
- sortedPosts = new ArrayList(posts);
+ sortedPosts = new ArrayList<>(posts);
}
- Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
+ Collections.sort(sortedPosts, Post.NEWEST_FIRST);
return sortedPosts;
}
@@ -379,7 +395,8 @@ public class SoneImpl implements Sone {
* The new (and only) posts of this Sone
* @return This Sone (for method chaining)
*/
- public Sone setPosts(Collection posts) {
+ @Nonnull
+ public Sone setPosts(@Nonnull Collection posts) {
synchronized (this) {
this.posts.clear();
this.posts.addAll(posts);
@@ -394,7 +411,7 @@ public class SoneImpl implements Sone {
* @param post
* The post to add
*/
- public void addPost(Post post) {
+ public void addPost(@Nonnull Post post) {
if (post.getSone().equals(this) && posts.add(post)) {
logger.log(Level.FINEST, String.format("Adding %s to â%sâ.", post, getName()));
}
@@ -406,7 +423,7 @@ public class SoneImpl implements Sone {
* @param post
* The post to remove
*/
- public void removePost(Post post) {
+ public void removePost(@Nonnull Post post) {
if (post.getSone().equals(this)) {
posts.remove(post);
}
@@ -417,6 +434,7 @@ public class SoneImpl implements Sone {
*
* @return All replies this Sone made
*/
+ @Nonnull
public Set getReplies() {
return Collections.unmodifiableSet(replies);
}
@@ -428,7 +446,8 @@ public class SoneImpl implements Sone {
* The new (and only) replies of this Sone
* @return This Sone (for method chaining)
*/
- public Sone setReplies(Collection replies) {
+ @Nonnull
+ public Sone setReplies(@Nonnull Collection replies) {
this.replies.clear();
this.replies.addAll(replies);
return this;
@@ -441,7 +460,7 @@ public class SoneImpl implements Sone {
* @param reply
* The reply to add
*/
- public void addReply(PostReply reply) {
+ public void addReply(@Nonnull PostReply reply) {
if (reply.getSone().equals(this)) {
replies.add(reply);
}
@@ -453,7 +472,7 @@ public class SoneImpl implements Sone {
* @param reply
* The reply to remove
*/
- public void removeReply(PostReply reply) {
+ public void removeReply(@Nonnull PostReply reply) {
if (reply.getSone().equals(this)) {
replies.remove(reply);
}
@@ -464,6 +483,7 @@ public class SoneImpl implements Sone {
*
* @return All liked postsâ IDs
*/
+ @Nonnull
public Set getLikedPostIds() {
return Collections.unmodifiableSet(likedPostIds);
}
@@ -475,7 +495,8 @@ public class SoneImpl implements Sone {
* All liked postsâ IDs
* @return This Sone (for method chaining)
*/
- public Sone setLikePostIds(Set likedPostIds) {
+ @Nonnull
+ public Sone setLikePostIds(@Nonnull Set likedPostIds) {
this.likedPostIds.clear();
this.likedPostIds.addAll(likedPostIds);
return this;
@@ -489,7 +510,7 @@ public class SoneImpl implements Sone {
* @return {@code true} if this Sone likes the given post, {@code false}
* otherwise
*/
- public boolean isLikedPostId(String postId) {
+ public boolean isLikedPostId(@Nonnull String postId) {
return likedPostIds.contains(postId);
}
@@ -500,7 +521,8 @@ public class SoneImpl implements Sone {
* The ID of the post
* @return This Sone (for method chaining)
*/
- public Sone addLikedPostId(String postId) {
+ @Nonnull
+ public Sone addLikedPostId(@Nonnull String postId) {
likedPostIds.add(postId);
return this;
}
@@ -510,11 +532,9 @@ public class SoneImpl implements Sone {
*
* @param postId
* The ID of the post
- * @return This Sone (for method chaining)
*/
- public Sone removeLikedPostId(String postId) {
+ public void removeLikedPostId(@Nonnull String postId) {
likedPostIds.remove(postId);
- return this;
}
/**
@@ -522,6 +542,7 @@ public class SoneImpl implements Sone {
*
* @return All liked repliesâ IDs
*/
+ @Nonnull
public Set getLikedReplyIds() {
return Collections.unmodifiableSet(likedReplyIds);
}
@@ -533,7 +554,8 @@ public class SoneImpl implements Sone {
* All liked repliesâ IDs
* @return This Sone (for method chaining)
*/
- public Sone setLikeReplyIds(Set likedReplyIds) {
+ @Nonnull
+ public Sone setLikeReplyIds(@Nonnull Set likedReplyIds) {
this.likedReplyIds.clear();
this.likedReplyIds.addAll(likedReplyIds);
return this;
@@ -547,7 +569,7 @@ public class SoneImpl implements Sone {
* @return {@code true} if this Sone likes the given reply, {@code false}
* otherwise
*/
- public boolean isLikedReplyId(String replyId) {
+ public boolean isLikedReplyId(@Nonnull String replyId) {
return likedReplyIds.contains(replyId);
}
@@ -558,7 +580,8 @@ public class SoneImpl implements Sone {
* The ID of the reply
* @return This Sone (for method chaining)
*/
- public Sone addLikedReplyId(String replyId) {
+ @Nonnull
+ public Sone addLikedReplyId(@Nonnull String replyId) {
likedReplyIds.add(replyId);
return this;
}
@@ -568,11 +591,9 @@ public class SoneImpl implements Sone {
*
* @param replyId
* The ID of the reply
- * @return This Sone (for method chaining)
*/
- public Sone removeLikedReplyId(String replyId) {
+ public void removeLikedReplyId(@Nonnull String replyId) {
likedReplyIds.remove(replyId);
- return this;
}
/**
@@ -580,6 +601,7 @@ public class SoneImpl implements Sone {
*
* @return The root album of this Sone
*/
+ @Nonnull
public Album getRootAlbum() {
return rootAlbum;
}
@@ -589,6 +611,7 @@ public class SoneImpl implements Sone {
*
* @return The options of this Sone
*/
+ @Nonnull
public SoneOptions getOptions() {
return options;
}
@@ -600,7 +623,7 @@ public class SoneImpl implements Sone {
* The options of this Sone
*/
/* TODO - remove this method again, maybe add an option provider */
- public void setOptions(SoneOptions options) {
+ public void setOptions(@Nonnull SoneOptions options) {
this.options = options;
}
@@ -612,46 +635,46 @@ public class SoneImpl implements Sone {
@Override
public synchronized String getFingerprint() {
Hasher hash = Hashing.sha256().newHasher();
- hash.putString(profile.getFingerprint());
+ hash.putString(profile.getFingerprint(), UTF_8);
- hash.putString("Posts(");
+ hash.putString("Posts(", UTF_8);
for (Post post : getPosts()) {
- hash.putString("Post(").putString(post.getId()).putString(")");
+ hash.putString("Post(", UTF_8).putString(post.getId(), UTF_8).putString(")", UTF_8);
}
- hash.putString(")");
+ hash.putString(")", UTF_8);
- List replies = new ArrayList(getReplies());
+ List replies = new ArrayList<>(getReplies());
Collections.sort(replies, Reply.TIME_COMPARATOR);
- hash.putString("Replies(");
+ hash.putString("Replies(", UTF_8);
for (PostReply reply : replies) {
- hash.putString("Reply(").putString(reply.getId()).putString(")");
+ hash.putString("Reply(", UTF_8).putString(reply.getId(), UTF_8).putString(")", UTF_8);
}
- hash.putString(")");
+ hash.putString(")", UTF_8);
- List likedPostIds = new ArrayList(getLikedPostIds());
+ List likedPostIds = new ArrayList<>(getLikedPostIds());
Collections.sort(likedPostIds);
- hash.putString("LikedPosts(");
+ hash.putString("LikedPosts(", UTF_8);
for (String likedPostId : likedPostIds) {
- hash.putString("Post(").putString(likedPostId).putString(")");
+ hash.putString("Post(", UTF_8).putString(likedPostId, UTF_8).putString(")", UTF_8);
}
- hash.putString(")");
+ hash.putString(")", UTF_8);
- List likedReplyIds = new ArrayList(getLikedReplyIds());
+ List likedReplyIds = new ArrayList<>(getLikedReplyIds());
Collections.sort(likedReplyIds);
- hash.putString("LikedReplies(");
+ hash.putString("LikedReplies(", UTF_8);
for (String likedReplyId : likedReplyIds) {
- hash.putString("Reply(").putString(likedReplyId).putString(")");
+ hash.putString("Reply(", UTF_8).putString(likedReplyId, UTF_8).putString(")", UTF_8);
}
- hash.putString(")");
+ hash.putString(")", UTF_8);
- hash.putString("Albums(");
+ hash.putString("Albums(", UTF_8);
for (Album album : rootAlbum.getAlbums()) {
if (!Album.NOT_EMPTY.apply(album)) {
continue;
}
- hash.putString(album.getFingerprint());
+ hash.putString(album.getFingerprint(), UTF_8);
}
- hash.putString(")");
+ hash.putString(")", UTF_8);
return hash.hash().toString();
}