}
}, 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 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();
}
import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.AlbumImpl;
import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.data.Sone;
import com.google.common.base.Optional;
}
@Test
+ public void postRepliesAreManagedCorrectly() {
+ Post firstPost = createPost(Optional.<String>absent());
+ PostReply firstPostFirstReply = createPostReply(firstPost, 1000L);
+ Post secondPost = createPost(Optional.<String>absent());
+ PostReply secondPostFirstReply = createPostReply(secondPost, 1000L);
+ PostReply secondPostSecondReply = createPostReply(secondPost, 2000L);
+ memoryDatabase.storePost(firstPost);
+ memoryDatabase.storePost(secondPost);
+ memoryDatabase.storePostReply(firstPostFirstReply);
+ memoryDatabase.storePostReply(secondPostFirstReply);
+ memoryDatabase.storePostReply(secondPostSecondReply);
+ assertThat(memoryDatabase.getReplies(firstPost.getId()),
+ contains(firstPostFirstReply));
+ assertThat(memoryDatabase.getReplies(secondPost.getId()),
+ contains(secondPostFirstReply, secondPostSecondReply));
+ }
+
+ private PostReply createPostReply(Post post, long time) {
+ PostReply postReply = mock(PostReply.class);
+ when(postReply.getId()).thenReturn(randomUUID().toString());
+ when(postReply.getTime()).thenReturn(time);
+ when(postReply.getPost()).thenReturn(of(post));
+ final String postId = post.getId();
+ when(postReply.getPostId()).thenReturn(postId);
+ return postReply;
+ }
+
+ @Test
public void testBasicAlbumFunctionality() {
Album newAlbum = new AlbumImpl(mock(Sone.class));
assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(Optional.<Album>absent()));