From 39ed514e96c15e07aba9478d3cbf304e5f1fda8f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 27 Jan 2012 10:28:18 +0100 Subject: [PATCH] =?utf8?q?Store=20a=20post=E2=80=99s=20known-status=20in?= =?utf8?q?=20the=20post=20itself.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../java/net/pterodactylus/sone/core/Core.java | 51 ++++++---------------- .../java/net/pterodactylus/sone/data/Post.java | 24 ++++++++++ .../pterodactylus/sone/template/PostAccessor.java | 2 +- 3 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index ed34ef2..a4fca35 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -143,11 +143,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis /** All posts. */ private Map posts = new HashMap(); - /** All new posts. */ - private Set newPosts = new HashSet(); - /** All known posts. */ - /* synchronize access on {@link #newPosts}. */ private Set knownPosts = new HashSet(); /** All replies. */ @@ -576,20 +572,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis } /** - * Returns whether the given post ID is new. - * - * @param postId - * The post ID - * @return {@code true} if the post is considered to be new, {@code false} - * otherwise - */ - public boolean isNewPost(String postId) { - synchronized (newPosts) { - return !knownPosts.contains(postId) && newPosts.contains(postId); - } - } - - /** * Returns all posts that have the given Sone as recipient. * * @see Post#getRecipient() @@ -1200,15 +1182,17 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis } } List storedPosts = storedSone.getPosts(); - synchronized (newPosts) { + synchronized (knownPosts) { for (Post post : sone.getPosts()) { post.setSone(storedSone); if (!storedPosts.contains(post)) { if (post.getTime() < getSoneFollowingTime(sone)) { + sone.setKnown(true); knownPosts.add(post.getId()); } else if (!knownPosts.contains(post.getId())) { - newPosts.add(post.getId()); coreListenerManager.fireNewPostFound(post); + } else { + sone.setKnown(true); } } posts.put(post.getId(), post); @@ -1556,7 +1540,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis knownSones.add(friend); } } - synchronized (newPosts) { + synchronized (knownPosts) { for (Post post : posts) { knownPosts.add(post.getId()); } @@ -1638,10 +1622,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis synchronized (posts) { posts.put(post.getId(), post); } - synchronized (newPosts) { - newPosts.add(post.getId()); - coreListenerManager.fireNewPostFound(post); - } + coreListenerManager.fireNewPostFound(post); sone.addPost(post); touchConfiguration(); localElementTicker.registerEvent(System.currentTimeMillis() + 10 * 1000, new Runnable() { @@ -1673,10 +1654,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis posts.remove(post.getId()); } coreListenerManager.firePostRemoved(post); - synchronized (newPosts) { - markPostKnown(post); - knownPosts.remove(post.getId()); - } + markPostKnown(post); touchConfiguration(); } @@ -1688,8 +1666,8 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis * The post to mark as known */ public void markPostKnown(Post post) { - synchronized (newPosts) { - newPosts.remove(post.getId()); + post.setKnown(true); + synchronized (knownPosts) { if (knownPosts.add(post.getId())) { coreListenerManager.fireMarkPostKnown(post); touchConfiguration(); @@ -2234,7 +2212,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis /* save known posts. */ int postCounter = 0; - synchronized (newPosts) { + synchronized (knownPosts) { for (String knownPostId : knownPosts) { configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId); } @@ -2374,7 +2352,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis if (knownPostId == null) { break; } - synchronized (newPosts) { + synchronized (knownPosts) { knownPosts.add(knownPostId); } } @@ -2516,10 +2494,9 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis return; } synchronized (posts) { - synchronized (newPosts) { + synchronized (knownPosts) { for (Post post : sone.getPosts()) { posts.remove(post.getId()); - newPosts.remove(post.getId()); coreListenerManager.firePostRemoved(post); } } @@ -2657,8 +2634,8 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis * * @param insertionDelay * The insertion delay to validate - * @return {@code true} if the given insertion delay was valid, {@code - * false} otherwise + * @return {@code true} if the given insertion delay was valid, + * {@code false} otherwise */ public boolean validateInsertionDelay(Integer insertionDelay) { return options.getIntegerOption("InsertionDelay").validate(insertionDelay); diff --git a/src/main/java/net/pterodactylus/sone/data/Post.java b/src/main/java/net/pterodactylus/sone/data/Post.java index a28bf9f..1a29480 100644 --- a/src/main/java/net/pterodactylus/sone/data/Post.java +++ b/src/main/java/net/pterodactylus/sone/data/Post.java @@ -65,6 +65,9 @@ public class Post { /** The text of the post. */ private volatile String text; + /** Whether the post is known. */ + private volatile boolean known; + /** * Creates a new post. * @@ -219,6 +222,27 @@ public class Post { return this; } + /** + * Returns whether this post is known. + * + * @return {@code true} if this post is known, {@code false} otherwise + */ + public boolean isKnown() { + return known; + } + + /** + * Sets whether this post is known. + * + * @param known + * {@code true} if this post is known, {@code false} otherwise + * @return This post + */ + public Post setKnown(boolean known) { + this.known = known; + return this; + } + // // OBJECT METHODS // diff --git a/src/main/java/net/pterodactylus/sone/template/PostAccessor.java b/src/main/java/net/pterodactylus/sone/template/PostAccessor.java index bef94d6..93ff569 100644 --- a/src/main/java/net/pterodactylus/sone/template/PostAccessor.java +++ b/src/main/java/net/pterodactylus/sone/template/PostAccessor.java @@ -63,7 +63,7 @@ public class PostAccessor extends ReflectionAccessor { Sone currentSone = (Sone) templateContext.get("currentSone"); return (currentSone != null) && (currentSone.isLikedPostId(post.getId())); } else if (member.equals("new")) { - return core.isNewPost(post.getId()); + return !post.isKnown(); } else if (member.equals("bookmarked")) { return core.isBookmarked(post); } else if (member.equals("loaded")) { -- 2.7.4