Move parsing of protocol version into its own method.
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemoryDatabase.java
index 0c38c2c..d7cef49 100644 (file)
@@ -46,10 +46,9 @@ import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.sone.database.DatabaseException;
-import net.pterodactylus.sone.database.PostBuilder;
 import net.pterodactylus.sone.database.PostDatabase;
-import net.pterodactylus.sone.database.PostReplyBuilder;
 import net.pterodactylus.sone.database.SoneBuilder;
+import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.util.config.Configuration;
 import net.pterodactylus.util.config.ConfigurationException;
 
@@ -57,6 +56,7 @@ import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.ListMultimap;
+import com.google.common.collect.Maps;
 import com.google.common.collect.SortedSetMultimap;
 import com.google.common.collect.TreeMultimap;
 import com.google.common.util.concurrent.AbstractService;
@@ -75,6 +75,7 @@ public class MemoryDatabase extends AbstractService implements Database {
        /** The configuration. */
        private final Configuration configuration;
 
+       private final Map<String, Identity> identities = Maps.newHashMap();
        private final Map<String, Sone> sones = new HashMap<String, Sone>();
 
        /** All posts by their ID. */
@@ -128,12 +129,6 @@ public class MemoryDatabase extends AbstractService implements Database {
        // DATABASE METHODS
        //
 
-       /**
-        * Saves the database.
-        *
-        * @throws DatabaseException
-        *              if an error occurs while saving
-        */
        @Override
        public void save() throws DatabaseException {
                saveKnownPosts();
@@ -144,7 +139,6 @@ public class MemoryDatabase extends AbstractService implements Database {
        // SERVICE METHODS
        //
 
-       /** {@inheritDocs} */
        @Override
        protected void doStart() {
                loadKnownPosts();
@@ -152,7 +146,6 @@ public class MemoryDatabase extends AbstractService implements Database {
                notifyStarted();
        }
 
-       /** {@inheritDocs} */
        @Override
        protected void doStop() {
                try {
@@ -164,6 +157,16 @@ public class MemoryDatabase extends AbstractService implements Database {
        }
 
        @Override
+       public Optional<Identity> getIdentity(String identityId) {
+               lock.readLock().lock();
+               try {
+                       return fromNullable(identities.get(identityId));
+               } finally {
+                       lock.readLock().unlock();
+               }
+       }
+
+       @Override
        public Optional<Sone> getSone(String soneId) {
                lock.readLock().lock();
                try {
@@ -212,7 +215,6 @@ public class MemoryDatabase extends AbstractService implements Database {
        // POSTPROVIDER METHODS
        //
 
-       /** {@inheritDocs} */
        @Override
        public Optional<Post> getPost(String postId) {
                lock.readLock().lock();
@@ -223,13 +225,11 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }
 
-       /** {@inheritDocs} */
        @Override
        public Collection<Post> getPosts(String soneId) {
                return new HashSet<Post>(getPostsFrom(soneId));
        }
 
-       /** {@inheritDocs} */
        @Override
        public Collection<Post> getDirectedPosts(String recipientId) {
                lock.readLock().lock();
@@ -242,20 +242,9 @@ public class MemoryDatabase extends AbstractService implements Database {
        }
 
        //
-       // POSTBUILDERFACTORY METHODS
-       //
-
-       /** {@inheritDocs} */
-       @Override
-       public PostBuilder newPostBuilder() {
-               return new MemoryPostBuilder(this);
-       }
-
-       //
        // POSTSTORE METHODS
        //
 
-       /** {@inheritDocs} */
        @Override
        public void storePost(Post post) {
                checkNotNull(post, "post must not be null");
@@ -271,7 +260,6 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }
 
-       /** {@inheritDocs} */
        @Override
        public void removePost(Post post) {
                checkNotNull(post, "post must not be null");
@@ -288,7 +276,6 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }
 
-       /** {@inheritDocs} */
        @Override
        public void storePosts(Sone sone, Collection<Post> posts) throws IllegalArgumentException {
                checkNotNull(sone, "sone must not be null");
@@ -323,7 +310,6 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }
 
-       /** {@inheritDocs} */
        @Override
        public void removePosts(Sone sone) {
                checkNotNull(sone, "sone must not be null");
@@ -346,7 +332,6 @@ public class MemoryDatabase extends AbstractService implements Database {
        // POSTREPLYPROVIDER METHODS
        //
 
-       /** {@inheritDocs} */
        @Override
        public Optional<PostReply> getPostReply(String id) {
                lock.readLock().lock();
@@ -357,7 +342,6 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }
 
-       /** {@inheritDocs} */
        @Override
        public List<PostReply> getReplies(String postId) {
                lock.readLock().lock();
@@ -372,20 +356,36 @@ public class MemoryDatabase extends AbstractService implements Database {
        }
 
        //
-       // POSTREPLYBUILDERFACTORY METHODS
+       // POSTREPLYSTORE METHODS
        //
 
-       /** {@inheritDocs} */
-       @Override
-       public PostReplyBuilder newPostReplyBuilder() {
-               return new MemoryPostReplyBuilder(this, this);
+       /**
+        * Returns whether the given post reply is known.
+        *
+        * @param postReply
+        *              The post reply
+        * @return {@code true} if the given post reply is known, {@code false}
+        *         otherwise
+        */
+       public boolean isPostReplyKnown(PostReply postReply) {
+               lock.readLock().lock();
+               try {
+                       return knownPostReplies.contains(postReply.getId());
+               } finally {
+                       lock.readLock().unlock();
+               }
        }
 
-       //
-       // POSTREPLYSTORE METHODS
-       //
+       @Override
+       public void setPostReplyKnown(PostReply postReply) {
+               lock.writeLock().lock();
+               try {
+                       knownPostReplies.add(postReply.getId());
+               } finally {
+                       lock.writeLock().unlock();
+               }
+       }
 
-       /** {@inheritDocs} */
        @Override
        public void storePostReply(PostReply postReply) {
                lock.writeLock().lock();
@@ -403,7 +403,6 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }
 
-       /** {@inheritDocs} */
        @Override
        public void storePostReplies(Sone sone, Collection<PostReply> postReplies) {
                checkNotNull(sone, "sone must not be null");
@@ -436,7 +435,6 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }
 
-       /** {@inheritDocs} */
        @Override
        public void removePostReply(PostReply postReply) {
                lock.writeLock().lock();
@@ -453,7 +451,6 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }
 
-       /** {@inheritDocs} */
        @Override
        public void removePostReplies(Sone sone) {
                checkNotNull(sone, "sone must not be null");
@@ -492,6 +489,38 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }
 
+       @Override
+       public void moveUp(Album album) {
+               lock.writeLock().lock();
+               try {
+                       List<String> albums = albumChildren.get(album.getParent().getId());
+                       int currentIndex = albums.indexOf(album.getId());
+                       if (currentIndex == 0) {
+                               return;
+                       }
+                       albums.remove(album.getId());
+                       albums.add(currentIndex - 1, album.getId());
+               } finally {
+                       lock.writeLock().unlock();
+               }
+       }
+
+       @Override
+       public void moveDown(Album album) {
+               lock.writeLock().lock();
+               try {
+                       List<String> albums = albumChildren.get(album.getParent().getId());
+                       int currentIndex = albums.indexOf(album.getId());
+                       if (currentIndex == (albums.size() - 1)) {
+                               return;
+                       }
+                       albums.remove(album.getId());
+                       albums.add(currentIndex + 1, album.getId());
+               } finally {
+                       lock.writeLock().unlock();
+               }
+       }
+
        //
        // ALBUMSTORE METHODS
        //
@@ -532,6 +561,48 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }
 
+       @Override
+       public List<Image> getImages(Album parent) {
+               lock.readLock().lock();
+               try {
+                       return from(albumImages.get(parent.getId())).transformAndConcat(getImage()).toList();
+               } finally {
+                       lock.readLock().unlock();
+               }
+       }
+
+       @Override
+       public void moveUp(Image image) {
+               lock.writeLock().lock();
+               try {
+                       List<String> images = albumImages.get(image.getAlbum().getId());
+                       int currentIndex = images.indexOf(image.getId());
+                       if (currentIndex == 0) {
+                               return;
+                       }
+                       images.remove(image.getId());
+                       images.add(currentIndex - 1, image.getId());
+               } finally {
+                       lock.writeLock().unlock();
+               }
+       }
+
+       @Override
+       public void moveDown(Image image) {
+               lock.writeLock().lock();
+               try {
+                       List<String> images = albumChildren.get(image.getAlbum().getId());
+                       int currentIndex = images.indexOf(image.getId());
+                       if (currentIndex == (images.size() - 1)) {
+                               return;
+                       }
+                       images.remove(image.getId());
+                       images.add(currentIndex + 1, image.getId());
+               } finally {
+                       lock.writeLock().unlock();
+               }
+       }
+
        //
        // IMAGESTORE METHODS
        //
@@ -599,104 +670,6 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }
 
-       /**
-        * Returns whether the given post reply is known.
-        *
-        * @param postReply
-        *              The post reply
-        * @return {@code true} if the given post reply is known, {@code false}
-        *         otherwise
-        */
-       boolean isPostReplyKnown(PostReply postReply) {
-               lock.readLock().lock();
-               try {
-                       return knownPostReplies.contains(postReply.getId());
-               } finally {
-                       lock.readLock().unlock();
-               }
-       }
-
-       /**
-        * Sets whether the given post reply is known.
-        *
-        * @param postReply
-        *              The post reply
-        * @param known
-        *              {@code true} if the post reply is known, {@code false} otherwise
-        */
-       void setPostReplyKnown(PostReply postReply, boolean known) {
-               lock.writeLock().lock();
-               try {
-                       if (known) {
-                               knownPostReplies.add(postReply.getId());
-                       } else {
-                               knownPostReplies.remove(postReply.getId());
-                       }
-               } finally {
-                       lock.writeLock().unlock();
-               }
-       }
-
-       void moveUp(Album album) {
-               lock.writeLock().lock();
-               try {
-                       List<String> albums = albumChildren.get(album.getParent().getId());
-                       int currentIndex = albums.indexOf(album.getId());
-                       if (currentIndex == 0) {
-                               return;
-                       }
-                       albums.remove(album.getId());
-                       albums.add(currentIndex - 1, album.getId());
-               } finally {
-                       lock.writeLock().unlock();
-               }
-       }
-
-       void moveDown(Album album) {
-               lock.writeLock().lock();
-               try {
-                       List<String> albums = albumChildren.get(album.getParent().getId());
-                       int currentIndex = albums.indexOf(album.getId());
-                       if (currentIndex == (albums.size() - 1)) {
-                               return;
-                       }
-                       albums.remove(album.getId());
-                       albums.add(currentIndex + 1, album.getId());
-               } finally {
-                       lock.writeLock().unlock();
-               }
-       }
-
-       void moveUp(Image image) {
-               lock.writeLock().lock();
-               try {
-                       List<String> images = albumImages.get(image.getAlbum().getId());
-                       int currentIndex = images.indexOf(image.getId());
-                       if (currentIndex == 0) {
-                               return;
-                       }
-                       images.remove(image.getId());
-                       images.add(currentIndex - 1, image.getId());
-               } finally {
-                       lock.writeLock().unlock();
-               }
-       }
-
-       void moveDown(Image image) {
-               lock.writeLock().lock();
-               try {
-                       List<String> images = albumChildren.get(image.getAlbum().getId());
-                       int currentIndex = images.indexOf(image.getId());
-                       if (currentIndex == (images.size() - 1)) {
-                               return;
-                       }
-                       images.remove(image.getId());
-                       images.add(currentIndex + 1, image.getId());
-               } finally {
-                       lock.writeLock().unlock();
-               }
-       }
-
        //
        // PRIVATE METHODS
        //
@@ -867,4 +840,13 @@ public class MemoryDatabase extends AbstractService implements Database {
                };
        }
 
+       private Function<String, Iterable<Image>> getImage() {
+               return new Function<String, Iterable<Image>>() {
+                       @Override
+                       public Iterable<Image> apply(String input) {
+                               return (input == null) ? Collections.<Image>emptyList() : getImage(input).asSet();
+                       }
+               };
+       }
+
 }