Use a multimap to store posts with recipienets.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 2 Jun 2014 04:43:10 +0000 (06:43 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 10 Jun 2014 10:45:03 +0000 (12:45 +0200)
src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java

index 238c533..d7e9f58 100644 (file)
@@ -84,7 +84,7 @@ public class MemoryDatabase extends AbstractService implements Database {
        private final Multimap<String, Post> sonePosts = HashMultimap.create();
 
        /** All posts by their recipient. */
-       private final Map<String, Collection<Post>> recipientPosts = new HashMap<String, Collection<Post>>();
+       private final Multimap<String, Post> recipientPosts = HashMultimap.create();
 
        /** Whether posts are known. */
        private final Set<String> knownPosts = new HashSet<String>();
@@ -627,26 +627,12 @@ public class MemoryDatabase extends AbstractService implements Database {
         * @return All posts
         */
        private Collection<Post> getPostsTo(String recipientId) {
-               Collection<Post> posts = null;
                lock.readLock().lock();
                try {
-                       posts = recipientPosts.get(recipientId);
+                       return recipientPosts.get(recipientId);
                } finally {
                        lock.readLock().unlock();
                }
-               if (posts != null) {
-                       return posts;
-               }
-
-               posts = new HashSet<Post>();
-               lock.writeLock().lock();
-               try {
-                       recipientPosts.put(recipientId, posts);
-               } finally {
-                       lock.writeLock().unlock();
-               }
-
-               return posts;
        }
 
        /** Loads the known posts. */