Move memory album and image to default implementation.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 15 Oct 2013 05:02:58 +0000 (07:02 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:26 +0000 (22:25 +0100)
14 files changed:
src/main/java/net/pterodactylus/sone/core/SoneDownloader.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbumBuilder.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultImageBuilder.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryAlbum.java [deleted file]
src/main/java/net/pterodactylus/sone/database/memory/MemoryImage.java [deleted file]
src/main/java/net/pterodactylus/sone/database/memory/MemoryImageBuilder.java [deleted file]
src/main/java/net/pterodactylus/sone/template/ParserFilter.java
src/main/java/net/pterodactylus/sone/text/SoneTextParser.java
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/test/java/net/pterodactylus/sone/data/impl/DefaultImageBuilderTest.java
src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java

index 34ca521..4be836b 100644 (file)
@@ -240,7 +240,7 @@ public class SoneDownloader extends AbstractService {
                        return null;
                }
 
-               Sone sone = new DefaultSone(originalSone.getId(), originalSone.isLocal()).setIdentity(originalSone.getIdentity());
+               Sone sone = new DefaultSone(core.getDatabase(), originalSone.getId(), originalSone.isLocal()).setIdentity(originalSone.getIdentity());
 
                SimpleXML soneXml;
                try {
index 7102bfe..c1640f9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - Album.java - Copyright © 2011–2013 David Roden
+ * Sone - MemoryAlbum.java - Copyright © 2013 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 package net.pterodactylus.sone.data.impl;
 
-import static com.google.common.base.Optional.absent;
-import static com.google.common.base.Optional.fromNullable;
 import static com.google.common.base.Preconditions.checkState;
 
-import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
-import java.util.UUID;
 
 import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.database.AlbumBuilder;
+import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.sone.database.ImageBuilder;
 
-import com.google.common.base.Function;
 import com.google.common.base.Optional;
-import com.google.common.base.Predicates;
-import com.google.common.collect.Collections2;
 
 /**
- * Dumb, store-everything-in-memory implementation of an {@link Album}.
+ * TODO
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
 public class DefaultAlbum extends AbstractAlbum {
 
-       /** The Sone this album belongs to. */
-       private Sone sone;
+       private final Database database;
+       private final Sone sone; /* TODO - only store sone ID. */
+       private final String parentId;
 
-       /** The parent album. */
-       private final DefaultAlbum parent;
-
-       /** Nested albums. */
-       private final List<Album> albums = new ArrayList<Album>();
-
-       /** The image IDs in order. */
-       final List<String> imageIds = new ArrayList<String>();
-
-       /** The images in this album. */
-       final Map<String, Image> images = new HashMap<String, Image>();
-
-       /** Creates a new album with a random ID. */
-       public DefaultAlbum(Sone sone, DefaultAlbum parent) {
-               this(UUID.randomUUID().toString(), sone, parent);
-       }
-
-       /**
-        * Creates a new album with the given ID.
-        *
-        * @param id
-        *              The ID of the album
-        */
-       public DefaultAlbum(String id, Sone sone, DefaultAlbum parent) {
+       protected DefaultAlbum(Database database, String id, Sone sone, String parentId) {
                super(id);
+               this.database = database;
                this.sone = sone;
-               this.parent = parent;
+               this.parentId = parentId;
        }
 
-       //
-       // ACCESSORS
-       //
-
        @Override
        public Sone getSone() {
                return sone;
@@ -88,94 +55,70 @@ public class DefaultAlbum extends AbstractAlbum {
 
        @Override
        public List<Album> getAlbums() {
-               return new ArrayList<Album>(albums);
+               return database.getAlbums(this);
        }
 
        @Override
        public List<Image> getImages() {
-               return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
-
-                       @Override
-                       @SuppressWarnings("synthetic-access")
-                       public Image apply(String imageId) {
-                               return images.get(imageId);
-                       }
-               }), Predicates.notNull()));
+               return database.getImages(this);
        }
 
        @Override
        public Optional<Image> getAlbumImage() {
-               if (albumImage == null) {
-                       return absent();
-               }
-               return fromNullable(fromNullable(images.get(albumImage)).or(images.values().iterator().next()));
+               return database.getImage(albumImage);
        }
 
        @Override
        public Album getParent() {
-               return parent;
+               return database.getAlbum(parentId).get();
        }
 
        @Override
-       public AlbumBuilder newAlbumBuilder() {
-               return new DefaultAlbumBuilder(sone, this) {
+       public AlbumBuilder newAlbumBuilder() throws IllegalStateException {
+               return new AbstractAlbumBuilder() {
                        @Override
                        public Album build() throws IllegalStateException {
-                               Album album = super.build();
-                               albums.add(album);
-                               return album;
+                               validate();
+                               DefaultAlbum memoryAlbum = new DefaultAlbum(database, getId(), sone, DefaultAlbum.this.id);
+                               database.storeAlbum(memoryAlbum);
+                               return memoryAlbum;
                        }
                };
        }
 
        @Override
        public ImageBuilder newImageBuilder() throws IllegalStateException {
-               return new DefaultImageBuilder(sone, this) {
+               return new AbstractImageBuilder() {
                        @Override
                        public Image build() throws IllegalStateException {
-                               Image image = super.build();
-                               if (images.isEmpty() && (albumImage == null)) {
-                                       albumImage = image.getId();
-                               }
-                               images.put(image.getId(), image);
-                               imageIds.add(image.getId());
-                               return image;
+                               validate();
+                               DefaultImage memoryImage = new DefaultImage(database, getId(), sone, DefaultAlbum.this.id, key, getCreationTime(), width, height);
+                               database.storeImage(memoryImage);
+                               return memoryImage;
                        }
                };
        }
 
        @Override
        public void moveUp() {
-               int oldIndex = parent.albums.indexOf(this);
-               parent.albums.remove(this);
-               parent.albums.add(Math.max(0, oldIndex - 1), this);
+               database.moveUp(this);
        }
 
        @Override
        public void moveDown() {
-               int oldIndex = parent.albums.indexOf(this);
-               parent.albums.remove(this);
-               parent.albums.add(Math.min(parent.albums.size(), oldIndex + 1), this);
+               database.moveDown(this);
        }
 
        @Override
        public void remove() throws IllegalStateException {
                checkState(!isRoot(), "can not remove root album");
-               removeAllAlbums();
-               removeAllImages();
-               parent.albums.remove(this);
-       }
-
-       private void removeAllImages() {
-               for (Image image : images.values()) {
-                       image.remove();
-               }
-       }
-
-       private void removeAllAlbums() {
-               for (Album album: albums) {
+               for (Album album : getAlbums()) {
                        album.remove();
                }
+               for (Image image : getImages()) {
+                       image.remove();
+               }
+               database.removeAlbum(this);
        }
 
 }
index 5148471..fd4b5a5 100644 (file)
@@ -20,26 +20,34 @@ package net.pterodactylus.sone.data.impl;
 import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.database.AlbumBuilder;
+import net.pterodactylus.sone.database.Database;
 
 /**
- * {@link AlbumBuilder} implementation that creates {@link DefaultAlbum} objects.
+ * {@link AlbumBuilder} implementation that creates {@link DefaultAlbum}
+ * objects.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
 public class DefaultAlbumBuilder extends AbstractAlbumBuilder {
 
+       private final Database database;
        private final Sone sone;
-       private final DefaultAlbum parent;
+       private final String parentId;
 
-       public DefaultAlbumBuilder(Sone sone, DefaultAlbum parent) {
+       public DefaultAlbumBuilder(Database database, Sone sone, String parentId) {
+               this.database = database;
                this.sone = sone;
-               this.parent = parent;
+               this.parentId = parentId;
        }
 
        @Override
        public Album build() throws IllegalStateException {
                validate();
-               return new DefaultAlbum(getId(), sone, parent);
+               DefaultAlbum album = new DefaultAlbum(database, getId(), sone, parentId);
+               if (parentId != null) {
+                       database.storeAlbum(album);
+               }
+               return album;
        }
 
 }
index 26f6355..445fa6b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - DefaultImage.java - Copyright © 2013 David Roden
+ * Sone - MemoryImage.java - Copyright © 2013 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 package net.pterodactylus.sone.data.impl;
 
-import static com.google.common.collect.FluentIterable.from;
-
 import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.database.Database;
 
 /**
- * Dumb, store-everything-in-memory implementation of an {@link Image}.
+ * {@link Image} implementation that uses a {@link Database}.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
 public class DefaultImage extends AbstractImage {
 
-       private final Sone sone;
-       private final DefaultAlbum album;
+       private final Database database;
+       private final Sone sone; /* TODO - store sone ID only. */
+       private final String albumId;
 
-       public DefaultImage(String id, Sone sone, DefaultAlbum album, String key, long creationTime, int width, int height) {
+       public DefaultImage(Database database, String id, Sone sone, String albumId, String key, long creationTime, int width, int height) {
                super(id, key, creationTime, width, height);
+               this.database = database;
                this.sone = sone;
-               this.album = album;
+               this.albumId = albumId;
        }
 
        @Override
@@ -46,32 +47,22 @@ public class DefaultImage extends AbstractImage {
 
        @Override
        public Album getAlbum() {
-               return album;
+               return database.getAlbum(albumId).get();
        }
 
        @Override
        public void moveUp() throws IllegalStateException {
-               int oldIndex = album.imageIds.indexOf(getId());
-               album.imageIds.remove(getId());
-               album.imageIds.add(Math.max(0, oldIndex - 1), getId());
+               database.moveUp(this);
        }
 
        @Override
        public void moveDown() throws IllegalStateException {
-               int oldIndex = album.imageIds.indexOf(getId());
-               album.imageIds.remove(getId());
-               album.imageIds.add(Math.min(album.imageIds.size(), oldIndex + 1), getId());
+               database.moveDown(this);
        }
 
        @Override
        public void remove() throws IllegalStateException {
-               synchronized (album) {
-                       album.images.remove(getId());
-                       album.imageIds.remove(getId());
-                       if (getId().equals(album.albumImage)) {
-                               album.albumImage = from(album.images.values()).transform(GET_ID).first().orNull();
-                       }
-               }
+               database.removeImage(this);
        }
 
 }
index 12f20c2..6df9a47 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - ImageBuilderImpl.java - Copyright © 2013 David Roden
+ * Sone - MemoryImageBuilder.java - Copyright © 2013 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 package net.pterodactylus.sone.data.impl;
 
-import static com.google.common.base.Preconditions.checkNotNull;
-
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.database.ImageBuilder;
+import net.pterodactylus.sone.database.memory.MemoryDatabase;
 
 /**
- * {@link ImageBuilder} implementation that creates {@link DefaultImage} objects.
+ * {@link ImageBuilder} implementation that creates {@link DefaultImage}s.
  *
- * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
 public class DefaultImageBuilder extends AbstractImageBuilder {
 
-       protected final Sone sone;
-       protected final DefaultAlbum album;
+       private final MemoryDatabase memoryDatabase;
+       private final Sone sone;
+       private final String albumId;
 
-       public DefaultImageBuilder(Sone sone, DefaultAlbum album) {
-               this.sone = checkNotNull(sone, "sone must not be null");
-               this.album = checkNotNull(album, "album must not be null");
+       public DefaultImageBuilder(MemoryDatabase memoryDatabase, Sone sone, String albumId) {
+               this.memoryDatabase = memoryDatabase;
+               this.sone = sone;
+               this.albumId = albumId;
        }
 
        @Override
        public Image build() throws IllegalStateException {
                validate();
-               return new DefaultImage(getId(), sone, album, key, getCreationTime(), width, height);
+               return new DefaultImage(memoryDatabase, getId(), sone, albumId, key, getCreationTime(), width, height);
        }
 
 }
index 61bd530..87ad6bf 100644 (file)
@@ -37,6 +37,7 @@ import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.database.AlbumBuilder;
+import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.util.logging.Logging;
 
@@ -55,6 +56,9 @@ public class DefaultSone implements Sone {
        /** The logger. */
        private static final Logger logger = Logging.getLogger(DefaultSone.class);
 
+       /** The database. */
+       private final Database database;
+
        /** The ID of this Sone. */
        private final String id;
 
@@ -105,7 +109,7 @@ public class DefaultSone implements Sone {
        private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
 
        /** The root album containing all albums. */
-       private final DefaultAlbum rootAlbum = new DefaultAlbum(this, null);
+       private final Album rootAlbum;
 
        /** Sone-specific options. */
        private Options options = new Options();
@@ -118,9 +122,11 @@ public class DefaultSone implements Sone {
         * @param local
         *              {@code true} if the Sone is a local Sone, {@code false} otherwise
         */
-       public DefaultSone(String id, boolean local) {
+       public DefaultSone(Database database, String id, boolean local) {
+               this.database = database;
                this.id = id;
                this.local = local;
+               rootAlbum = new DefaultAlbumBuilder(database, this, null).randomId().build();
        }
 
        //
@@ -667,7 +673,7 @@ public class DefaultSone implements Sone {
 
        @Override
        public AlbumBuilder newAlbumBuilder() {
-               return new DefaultAlbumBuilder(this, rootAlbum);
+               return new DefaultAlbumBuilder(database, this, rootAlbum.getId());
        }
 
        //
diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryAlbum.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryAlbum.java
deleted file mode 100644 (file)
index 38e781f..0000000
+++ /dev/null
@@ -1,126 +0,0 @@
-/*
- * Sone - MemoryAlbum.java - Copyright © 2013 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.database.memory;
-
-import static com.google.common.base.Preconditions.checkState;
-
-import java.util.List;
-
-import net.pterodactylus.sone.data.Album;
-import net.pterodactylus.sone.data.Image;
-import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.data.impl.AbstractAlbum;
-import net.pterodactylus.sone.data.impl.AbstractAlbumBuilder;
-import net.pterodactylus.sone.data.impl.AbstractImageBuilder;
-import net.pterodactylus.sone.database.AlbumBuilder;
-import net.pterodactylus.sone.database.ImageBuilder;
-
-import com.google.common.base.Optional;
-
-/**
- * TODO
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class MemoryAlbum extends AbstractAlbum {
-
-       private final MemoryDatabase memoryDatabase;
-       private final Sone sone; /* TODO - only store sone ID. */
-       private final String parentId;
-
-       protected MemoryAlbum(MemoryDatabase memoryDatabase, String id, Sone sone, String parentId) {
-               super(id);
-               this.memoryDatabase = memoryDatabase;
-               this.sone = sone;
-               this.parentId = parentId;
-       }
-
-       @Override
-       public Sone getSone() {
-               return sone;
-       }
-
-       @Override
-       public List<Album> getAlbums() {
-               return memoryDatabase.getAlbums(this);
-       }
-
-       @Override
-       public List<Image> getImages() {
-               return memoryDatabase.getImages(this);
-       }
-
-       @Override
-       public Optional<Image> getAlbumImage() {
-               return memoryDatabase.getImage(albumImage);
-       }
-
-       @Override
-       public Album getParent() {
-               return memoryDatabase.getAlbum(parentId).get();
-       }
-
-       @Override
-       public AlbumBuilder newAlbumBuilder() throws IllegalStateException {
-               return new AbstractAlbumBuilder() {
-                       @Override
-                       public Album build() throws IllegalStateException {
-                               validate();
-                               MemoryAlbum memoryAlbum = new MemoryAlbum(memoryDatabase, getId(), sone, MemoryAlbum.this.id);
-                               memoryDatabase.storeAlbum(memoryAlbum);
-                               return memoryAlbum;
-                       }
-               };
-       }
-
-       @Override
-       public ImageBuilder newImageBuilder() throws IllegalStateException {
-               return new AbstractImageBuilder() {
-                       @Override
-                       public Image build() throws IllegalStateException {
-                               validate();
-                               MemoryImage memoryImage = new MemoryImage(memoryDatabase, getId(), sone, MemoryAlbum.this.id, key, getCreationTime(), width, height);
-                               memoryDatabase.storeImage(memoryImage);
-                               return memoryImage;
-                       }
-               };
-       }
-
-       @Override
-       public void moveUp() {
-               memoryDatabase.moveUp(this);
-       }
-
-       @Override
-       public void moveDown() {
-               memoryDatabase.moveDown(this);
-       }
-
-       @Override
-       public void remove() throws IllegalStateException {
-               checkState(!isRoot(), "can not remove root album");
-               for (Album album : getAlbums()) {
-                       album.remove();
-               }
-               for (Image image : getImages()) {
-                       image.remove();
-               }
-               memoryDatabase.removeAlbum(this);
-       }
-
-}
diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryImage.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryImage.java
deleted file mode 100644 (file)
index c00759e..0000000
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
- * Sone - MemoryImage.java - Copyright © 2013 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.database.memory;
-
-import net.pterodactylus.sone.data.Album;
-import net.pterodactylus.sone.data.Image;
-import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.data.impl.AbstractImage;
-import net.pterodactylus.sone.database.Database;
-
-/**
- * In-memory implementation of an {@link Image}.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class MemoryImage extends AbstractImage {
-
-       private final Database database;
-       private final Sone sone; /* TODO - store sone ID only. */
-       private final String albumId;
-
-       public MemoryImage(Database database, String id, Sone sone, String albumId, String key, long creationTime, int width, int height) {
-               super(id, key, creationTime, width, height);
-               this.database = database;
-               this.sone = sone;
-               this.albumId = albumId;
-       }
-
-       @Override
-       public Sone getSone() {
-               return sone;
-       }
-
-       @Override
-       public Album getAlbum() {
-               return database.getAlbum(albumId).get();
-       }
-
-       @Override
-       public void moveUp() throws IllegalStateException {
-               database.moveUp(this);
-       }
-
-       @Override
-       public void moveDown() throws IllegalStateException {
-               database.moveDown(this);
-       }
-
-       @Override
-       public void remove() throws IllegalStateException {
-               database.removeImage(this);
-       }
-
-}
diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryImageBuilder.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryImageBuilder.java
deleted file mode 100644 (file)
index cf16416..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Sone - MemoryImageBuilder.java - Copyright © 2013 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.database.memory;
-
-import net.pterodactylus.sone.data.Image;
-import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.data.impl.AbstractImageBuilder;
-import net.pterodactylus.sone.database.ImageBuilder;
-
-/**
- * {@link ImageBuilder} implementation that creates {@link MemoryImage}s.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class MemoryImageBuilder extends AbstractImageBuilder {
-
-       private final MemoryDatabase memoryDatabase;
-       private final Sone sone;
-       private final String albumId;
-
-       public MemoryImageBuilder(MemoryDatabase memoryDatabase, Sone sone, String albumId) {
-               this.memoryDatabase = memoryDatabase;
-               this.sone = sone;
-               this.albumId = albumId;
-       }
-
-       @Override
-       public Image build() throws IllegalStateException {
-               validate();
-               return new MemoryImage(memoryDatabase, getId(), sone, albumId, key, getCreationTime(), width, height);
-       }
-
-}
index bd6903b..bc496a2 100644 (file)
@@ -248,7 +248,7 @@ public class ParserFilter implements Filter {
         *            The part to render
         */
        private void render(Writer writer, PostPart postPart) {
-               SoneTextParser parser = new SoneTextParser(core, core);
+               SoneTextParser parser = new SoneTextParser(core.getDatabase());
                SoneTextParserContext parserContext = new SoneTextParserContext(null, postPart.getPost().getSone());
                try {
                        Iterable<Part> parts = parser.parse(parserContext, new StringReader(postPart.getPost().getText()));
index 18afb39..16db9c1 100644 (file)
@@ -29,15 +29,14 @@ import java.util.regex.Pattern;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.impl.DefaultSone;
-import net.pterodactylus.sone.database.PostProvider;
-import net.pterodactylus.sone.database.SoneProvider;
+import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.util.io.Closer;
 import net.pterodactylus.util.logging.Logging;
 
-import com.google.common.base.Optional;
-
 import freenet.keys.FreenetURI;
 
+import com.google.common.base.Optional;
+
 /**
  * {@link Parser} implementation that can recognize Freenet URIs.
  *
@@ -106,23 +105,15 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
 
        }
 
-       /** The Sone provider. */
-       private final SoneProvider soneProvider;
-
-       /** The post provider. */
-       private final PostProvider postProvider;
+       private final Database database;
 
        /**
         * Creates a new freenet link parser.
         *
-        * @param soneProvider
-        *            The Sone provider
-        * @param postProvider
-        *            The post provider
+        * @param database
         */
-       public SoneTextParser(SoneProvider soneProvider, PostProvider postProvider) {
-               this.soneProvider = soneProvider;
-               this.postProvider = postProvider;
+       public SoneTextParser(Database database) {
+               this.database = database;
        }
 
        //
@@ -243,13 +234,13 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                        if (linkType == LinkType.SONE) {
                                                if (line.length() >= (7 + 43)) {
                                                        String soneId = line.substring(7, 50);
-                                                       Optional<Sone> sone = soneProvider.getSone(soneId);
+                                                       Optional<Sone> sone = database.getSone(soneId);
                                                        if (!sone.isPresent()) {
                                                                /*
                                                                 * don’t use create=true above, we don’t want
                                                                 * the empty shell.
                                                                 */
-                                                               sone = Optional.<Sone>of(new DefaultSone(soneId, false));
+                                                               sone = Optional.<Sone>of(new DefaultSone(database, soneId, false));
                                                        }
                                                        parts.add(new SonePart(sone.get()));
                                                        line = line.substring(50);
@@ -262,7 +253,7 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                        if (linkType == LinkType.POST) {
                                                if (line.length() >= (7 + 36)) {
                                                        String postId = line.substring(7, 43);
-                                                       Optional<Post> post = postProvider.getPost(postId);
+                                                       Optional<Post> post = database.getPost(postId);
                                                        if (post.isPresent()) {
                                                                parts.add(new PostPart(post.get()));
                                                        } else {
index f6645ca..ed39b71 100644 (file)
@@ -246,7 +246,7 @@ public class WebInterface {
        public WebInterface(SonePlugin sonePlugin) {
                this.sonePlugin = sonePlugin;
                formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
-               soneTextParser = new SoneTextParser(getCore(), getCore());
+               soneTextParser = new SoneTextParser(getCore().getDatabase());
 
                templateContextFactory = new TemplateContextFactory();
                templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
index 2f00735..21c89d8 100644 (file)
@@ -25,6 +25,7 @@ import static org.mockito.Mockito.mock;
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.database.ImageBuilder;
+import net.pterodactylus.sone.database.memory.MemoryDatabase;
 
 import org.hamcrest.CoreMatchers;
 import org.junit.Test;
@@ -44,7 +45,7 @@ public class DefaultImageBuilderTest {
 
        private final Sone sone = mock(Sone.class);
        private final DefaultAlbum album = mock(DefaultAlbum.class);
-       private final ImageBuilder imageBuilder = new DefaultImageBuilder(sone, album);
+       private final ImageBuilder imageBuilder = new DefaultImageBuilder(new MemoryDatabase(null), sone, null);
 
        @Test
        public void testImageCreationWithAllExplicitParameters() {
index 0dd1032..34767b5 100644 (file)
@@ -20,14 +20,10 @@ package net.pterodactylus.sone.text;
 import java.io.IOException;
 import java.io.StringReader;
 import java.util.Arrays;
-import java.util.Collection;
 
-import com.google.common.base.Optional;
+import net.pterodactylus.sone.database.memory.MemoryDatabase;
 
 import junit.framework.TestCase;
-import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.data.impl.DefaultSone;
-import net.pterodactylus.sone.database.SoneProvider;
 
 /**
  * JUnit test case for {@link SoneTextParser}.
@@ -48,7 +44,7 @@ public class SoneTextParserTest extends TestCase {
         */
        @SuppressWarnings("static-method")
        public void testPlainText() throws IOException {
-               SoneTextParser soneTextParser = new SoneTextParser(null, null);
+               SoneTextParser soneTextParser = new SoneTextParser(new MemoryDatabase(null));
                Iterable<Part> parts;
 
                /* check basic operation. */
@@ -75,7 +71,7 @@ public class SoneTextParserTest extends TestCase {
         */
        @SuppressWarnings("static-method")
        public void testKSKLinks() throws IOException {
-               SoneTextParser soneTextParser = new SoneTextParser(null, null);
+               SoneTextParser soneTextParser = new SoneTextParser(new MemoryDatabase(null));
                Iterable<Part> parts;
 
                /* check basic links. */
@@ -102,7 +98,7 @@ public class SoneTextParserTest extends TestCase {
         */
        @SuppressWarnings({ "synthetic-access", "static-method" })
        public void testEmptyLinesAndSoneLinks() throws IOException {
-               SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
+               SoneTextParser soneTextParser = new SoneTextParser(new MemoryDatabase(null));
                Iterable<Part> parts;
 
                /* check basic links. */
@@ -120,7 +116,7 @@ public class SoneTextParserTest extends TestCase {
         */
        @SuppressWarnings({ "synthetic-access", "static-method" })
        public void testEmpyHttpLinks() throws IOException {
-               SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
+               SoneTextParser soneTextParser = new SoneTextParser(new MemoryDatabase(null));
                Iterable<Part> parts;
 
                /* check empty http links. */
@@ -174,54 +170,4 @@ public class SoneTextParserTest extends TestCase {
                return text.toString();
        }
 
-       /**
-        * Mock Sone provider.
-        *
-        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
-        */
-       private static class TestSoneProvider implements SoneProvider {
-
-               /**
-                * {@inheritDoc}
-                */
-               @Override
-               public Optional<Sone> getSone(final String soneId) {
-                       return Optional.<Sone>of(new DefaultSone(soneId, false) {
-
-                               /**
-                                * {@inheritDoc}
-                                */
-                               @Override
-                               public String getName() {
-                                       return soneId;
-                               }
-                       });
-               }
-
-               /**
-                * {@inheritDocs}
-                */
-               @Override
-               public Collection<Sone> getSones() {
-                       return null;
-               }
-
-               /**
-                * {@inheritDocs}
-                */
-               @Override
-               public Collection<Sone> getLocalSones() {
-                       return null;
-               }
-
-               /**
-                * {@inheritDocs}
-                */
-               @Override
-               public Collection<Sone> getRemoteSones() {
-                       return null;
-               }
-
-       }
-
 }