X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2Fimpl%2FSoneImpl.java;h=049460727198e6166cbfe8224d49f628a7b0de4f;hp=32cefb16398b04b3bcb16ddf5f287eedd488df51;hb=ffd92ca2374c0b2218e583d02e0bdd24b8c110ae;hpb=1bc68398c8912a992d87087b79ad9bbea8964c76 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 32cefb1..0494607 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–2013 David Roden + * Sone - SoneImpl.java - Copyright © 2010–2016 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 @@ -31,8 +31,10 @@ 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.AlbumImpl; import net.pterodactylus.sone.data.Client; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; @@ -41,6 +43,7 @@ import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.SoneOptions; import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions; +import net.pterodactylus.sone.database.Database; import net.pterodactylus.sone.freenet.wot.Identity; import net.pterodactylus.sone.freenet.wot.OwnIdentity; @@ -59,7 +62,10 @@ import com.google.common.hash.Hashing; public class SoneImpl implements Sone { /** The logger. */ - private static final Logger logger = getLogger("Sone.Data"); + private static final Logger logger = getLogger(SoneImpl.class.getName()); + + /** The database. */ + private final Database database; /** The ID of this Sone. */ private final String id; @@ -88,9 +94,6 @@ public class SoneImpl implements Sone { /** Whether this Sone is known. */ private volatile boolean known; - /** All friend Sones. */ - private final Set friendSones = new CopyOnWriteArraySet(); - /** All posts. */ private final Set posts = new CopyOnWriteArraySet(); @@ -112,12 +115,14 @@ public class SoneImpl implements Sone { /** * Creates a new Sone. * + * @param database The database * @param identity * The identity of the Sone * @param local * {@code true} if the Sone is a local Sone, {@code false} otherwise */ - public SoneImpl(Identity identity, boolean local) { + public SoneImpl(Database database, Identity identity, boolean local) { + this.database = database; this.id = identity.getId(); this.identity = identity; this.local = local; @@ -132,6 +137,7 @@ public class SoneImpl implements Sone { * * @return The identity of this Sone */ + @Nonnull public String getId() { return id; } @@ -141,6 +147,7 @@ public class SoneImpl implements Sone { * * @return The identity of this Sone */ + @Nonnull public Identity getIdentity() { return identity; } @@ -150,6 +157,7 @@ public class SoneImpl implements Sone { * * @return The name of this Sone */ + @Nonnull public String getName() { return (identity != null) ? identity.getNickname() : null; } @@ -168,6 +176,7 @@ public class SoneImpl implements Sone { * * @return The request URI of this Sone */ + @Nonnull public FreenetURI getRequestUri() { try { return new FreenetURI(getIdentity().getRequestUri()) @@ -187,6 +196,7 @@ public class SoneImpl implements Sone { * * @return The insert URI of this Sone */ + @Nullable public FreenetURI getInsertUri() { if (!isLocal()) { return null; @@ -242,6 +252,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; @@ -252,6 +263,7 @@ public class SoneImpl implements Sone { * * @return The status of this Sone */ + @Nonnull public SoneStatus getStatus() { return status; } @@ -265,7 +277,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; } @@ -277,6 +290,7 @@ public class SoneImpl implements Sone { * * @return A copy of the profile */ + @Nonnull public Profile getProfile() { return new Profile(profile); } @@ -289,7 +303,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); } @@ -298,6 +312,7 @@ public class SoneImpl implements Sone { * * @return The client used by this Sone, or {@code null} */ + @Nullable public Client getClient() { return client; } @@ -309,7 +324,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; } @@ -330,6 +346,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; @@ -340,8 +357,9 @@ public class SoneImpl implements Sone { * * @return The friend Sones of this Sone */ - public List getFriends() { - return new ArrayList(friendSones); + @Nonnull + public Collection getFriends() { + return database.getFriends(this); } /** @@ -352,34 +370,8 @@ 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) { - return friendSones.contains(friendSoneId); - } - - /** - * Adds the given Sone as a friend Sone. - * - * @param friendSone - * The friend Sone to add - * @return This Sone (for method chaining) - */ - public Sone addFriend(String friendSone) { - if (!friendSone.equals(id)) { - friendSones.add(friendSone); - } - return this; - } - - /** - * Removes the given Sone as a friend Sone. - * - * @param friendSoneId - * The ID of the friend Sone to remove - * @return This Sone (for method chaining) - */ - public Sone removeFriend(String friendSoneId) { - friendSones.remove(friendSoneId); - return this; + public boolean hasFriend(@Nonnull String friendSoneId) { + return database.isFriend(this, friendSoneId); } /** @@ -387,12 +379,13 @@ public class SoneImpl implements Sone { * * @return All posts of this Sone */ + @Nonnull public List getPosts() { List sortedPosts; synchronized (this) { sortedPosts = new ArrayList(posts); } - Collections.sort(sortedPosts, Post.TIME_COMPARATOR); + Collections.sort(sortedPosts, Post.NEWEST_FIRST); return sortedPosts; } @@ -403,7 +396,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); @@ -418,7 +412,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())); } @@ -430,7 +424,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); } @@ -441,6 +435,7 @@ public class SoneImpl implements Sone { * * @return All replies this Sone made */ + @Nonnull public Set getReplies() { return Collections.unmodifiableSet(replies); } @@ -452,7 +447,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; @@ -465,7 +461,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); } @@ -477,7 +473,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); } @@ -488,6 +484,7 @@ public class SoneImpl implements Sone { * * @return All liked posts’ IDs */ + @Nonnull public Set getLikedPostIds() { return Collections.unmodifiableSet(likedPostIds); } @@ -499,7 +496,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; @@ -513,7 +511,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); } @@ -524,7 +522,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; } @@ -534,11 +533,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; } /** @@ -546,6 +543,7 @@ public class SoneImpl implements Sone { * * @return All liked replies’ IDs */ + @Nonnull public Set getLikedReplyIds() { return Collections.unmodifiableSet(likedReplyIds); } @@ -557,7 +555,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; @@ -571,7 +570,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); } @@ -582,7 +581,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; } @@ -592,11 +592,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; } /** @@ -604,6 +602,7 @@ public class SoneImpl implements Sone { * * @return The root album of this Sone */ + @Nonnull public Album getRootAlbum() { return rootAlbum; } @@ -613,6 +612,7 @@ public class SoneImpl implements Sone { * * @return The options of this Sone */ + @Nonnull public SoneOptions getOptions() { return options; } @@ -624,7 +624,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; } @@ -712,7 +712,7 @@ public class SoneImpl implements Sone { /** {@inheritDoc} */ @Override public String toString() { - return getClass().getName() + "[identity=" + identity + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]"; + return getClass().getName() + "[identity=" + identity + ",posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]"; } }