import static com.google.common.base.Optional.fromNullable;
import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.collect.FluentIterable.from;
import static net.pterodactylus.sone.data.Reply.TIME_COMPARATOR;
import java.util.ArrayList;
import net.pterodactylus.util.config.ConfigurationException;
import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.SortedSetMultimap;
/** All posts by their Sones. */
private final Multimap<String, Post> sonePosts = HashMultimap.create();
- /** All posts by their recipient. */
- private final Multimap<String, Post> recipientPosts = HashMultimap.create();
-
/** Whether posts are known. */
private final Set<String> knownPosts = new HashSet<String>();
}
}, TIME_COMPARATOR);
- /** Replies by post. */
- private final SortedSetMultimap<String, PostReply> postReplies = TreeMultimap.create(new Comparator<String>() {
-
- @Override
- public int compare(String leftString, String rightString) {
- return leftString.compareTo(rightString);
- }
- }, TIME_COMPARATOR);
-
/** Whether post replies are known. */
private final Set<String> knownPostReplies = new HashSet<String>();
/** {@inheritDocs} */
@Override
- public Collection<Post> getDirectedPosts(String recipientId) {
+ public Collection<Post> getDirectedPosts(final String recipientId) {
lock.readLock().lock();
try {
- Collection<Post> posts = recipientPosts.get(recipientId);
- return (posts == null) ? Collections.<Post>emptySet() : new HashSet<Post>(posts);
+ return from(sonePosts.values()).filter(new Predicate<Post>() {
+ @Override
+ public boolean apply(Post post) {
+ return post.getRecipientId().asSet().contains(recipientId);
+ }
+ }).toSet();
} finally {
lock.readLock().unlock();
}
try {
allPosts.put(post.getId(), post);
getPostsFrom(post.getSone().getId()).add(post);
- if (post.getRecipientId().isPresent()) {
- getPostsTo(post.getRecipientId().get()).add(post);
- }
} finally {
lock.writeLock().unlock();
}
try {
allPosts.remove(post.getId());
getPostsFrom(post.getSone().getId()).remove(post);
- if (post.getRecipientId().isPresent()) {
- getPostsTo(post.getRecipientId().get()).remove(post);
- }
post.getSone().removePost(post);
} finally {
lock.writeLock().unlock();
Collection<Post> oldPosts = getPostsFrom(sone.getId());
for (Post post : oldPosts) {
allPosts.remove(post.getId());
- if (post.getRecipientId().isPresent()) {
- getPostsTo(post.getRecipientId().get()).remove(post);
- }
}
/* add new posts. */
getPostsFrom(sone.getId()).addAll(posts);
for (Post post : posts) {
allPosts.put(post.getId(), post);
- if (post.getRecipientId().isPresent()) {
- getPostsTo(post.getRecipientId().get()).add(post);
- }
}
} finally {
lock.writeLock().unlock();
getPostsFrom(sone.getId()).clear();
for (Post post : sone.getPosts()) {
allPosts.remove(post.getId());
- if (post.getRecipientId().isPresent()) {
- getPostsTo(post.getRecipientId().get()).remove(post);
- }
}
} finally {
lock.writeLock().unlock();
/** {@inheritDocs} */
@Override
- public List<PostReply> getReplies(String postId) {
+ public List<PostReply> getReplies(final String postId) {
lock.readLock().lock();
try {
- if (!postReplies.containsKey(postId)) {
- return Collections.emptyList();
- }
- return new ArrayList<PostReply>(postReplies.get(postId));
+ return from(allPostReplies.values())
+ .filter(new Predicate<PostReply>() {
+ @Override
+ public boolean apply(PostReply postReply) {
+ return postReply.getPostId().equals(postId);
+ }
+ }).toSortedList(TIME_COMPARATOR);
} finally {
lock.readLock().unlock();
}
lock.writeLock().lock();
try {
allPostReplies.put(postReply.getId(), postReply);
- postReplies.put(postReply.getPostId(), postReply);
} finally {
lock.writeLock().unlock();
}
for (PostReply postReply : postReplies) {
allPostReplies.put(postReply.getId(), postReply);
sonePostReplies.put(postReply.getSone().getId(), postReply);
- this.postReplies.put(postReply.getPostId(), postReply);
}
} finally {
lock.writeLock().unlock();
lock.writeLock().lock();
try {
allPostReplies.remove(postReply.getId());
- if (postReplies.containsKey(postReply.getPostId())) {
- postReplies.get(postReply.getPostId()).remove(postReply);
- }
} finally {
lock.writeLock().unlock();
}
}
}
- /**
- * Gets all posts that are directed the given Sone, creating a new collection
- * if there is none yet.
- *
- * @param recipientId
- * The ID of the Sone to get the posts for
- * @return All posts
- */
- private Collection<Post> getPostsTo(String recipientId) {
- lock.readLock().lock();
- try {
- return recipientPosts.get(recipientId);
- } finally {
- lock.readLock().unlock();
- }
- }
-
/** Loads the known posts. */
private void loadKnownPosts() {
lock.writeLock().lock();