/* synchronize access on {@link #newSones}. */
private Set<String> knownSones = new HashSet<String>();
+ /** All new posts. */
+ private Set<String> newPosts = new HashSet<String>();
+
+ /** All known posts. */
+ /* synchronize access on {@link #newPosts}. */
+ private Set<String> knownPosts = new HashSet<String>();
+
/** All posts. */
private Map<String, Post> posts = new HashMap<String, Post>();
}
/**
+ * Returns whether the given post ID is new. After this method returns it is
+ * marked a known post ID.
+ *
+ * @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) {
+ boolean isNew = !knownPosts.contains(postId) && newPosts.remove(postId);
+ knownPosts.add(postId);
+ return isNew;
+ }
+ }
+
+ /**
* Returns the reply with the given ID.
*
* @param replyId
for (Post post : storedSone.getPosts()) {
posts.remove(post.getId());
}
- for (Post post : sone.getPosts()) {
- posts.put(post.getId(), post);
+ synchronized (newPosts) {
+ for (Post post : sone.getPosts()) {
+ if (!storedSone.getPosts().contains(post) && !knownSones.contains(post.getId())) {
+ newPosts.add(post.getId());
+ }
+ posts.put(post.getId(), post);
+ }
}
}
synchronized (replies) {
knownSones.add(friend);
}
}
+ synchronized (newPosts) {
+ for (Post post : posts) {
+ knownPosts.add(post.getId());
+ }
+ }
}
/**
knownSones.add(knownSoneId);
}
}
+
+ /* load known posts. */
+ int postCounter = 0;
+ while (true) {
+ String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
+ if (knownPostId == null) {
+ break;
+ }
+ synchronized (newPosts) {
+ knownPosts.add(knownPostId);
+ }
+ }
+
}
/**
configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
}
+ /* save known posts. */
+ int postCounter = 0;
+ synchronized (newPosts) {
+ for (String knownPostId : knownPosts) {
+ configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
+ }
+ configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
+ }
+
} catch (ConfigurationException ce1) {
logger.log(Level.SEVERE, "Could not store configuration!", ce1);
}