From bdedce9c2a3bf37b0c98ea7d96a4c08432013011 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 15 Oct 2013 07:02:58 +0200 Subject: [PATCH] Move memory album and image to default implementation. --- .../pterodactylus/sone/core/SoneDownloader.java | 2 +- .../pterodactylus/sone/data/impl/DefaultAlbum.java | 119 +++++-------------- .../sone/data/impl/DefaultAlbumBuilder.java | 18 ++- .../pterodactylus/sone/data/impl/DefaultImage.java | 35 +++--- .../sone/data/impl/DefaultImageBuilder.java | 23 ++-- .../pterodactylus/sone/data/impl/DefaultSone.java | 12 +- .../sone/database/memory/MemoryAlbum.java | 126 --------------------- .../sone/database/memory/MemoryImage.java | 69 ----------- .../sone/database/memory/MemoryImageBuilder.java | 48 -------- .../pterodactylus/sone/template/ParserFilter.java | 2 +- .../pterodactylus/sone/text/SoneTextParser.java | 29 ++--- .../net/pterodactylus/sone/web/WebInterface.java | 2 +- .../sone/data/impl/DefaultImageBuilderTest.java | 3 +- .../sone/text/SoneTextParserTest.java | 64 +---------- 14 files changed, 98 insertions(+), 454 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/database/memory/MemoryAlbum.java delete mode 100644 src/main/java/net/pterodactylus/sone/database/memory/MemoryImage.java delete mode 100644 src/main/java/net/pterodactylus/sone/database/memory/MemoryImageBuilder.java diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java index 34ca521..4be836b 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -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 { diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java index 7102bfe..c1640f9 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java @@ -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 @@ -17,70 +17,37 @@ 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 David ‘Bombe’ Roden */ 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 albums = new ArrayList(); - - /** The image IDs in order. */ - final List imageIds = new ArrayList(); - - /** The images in this album. */ - final Map images = new HashMap(); - - /** 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 getAlbums() { - return new ArrayList(albums); + return database.getAlbums(this); } @Override public List getImages() { - return new ArrayList(Collections2.filter(Collections2.transform(imageIds, new Function() { - - @Override - @SuppressWarnings("synthetic-access") - public Image apply(String imageId) { - return images.get(imageId); - } - }), Predicates.notNull())); + return database.getImages(this); } @Override public Optional 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); } } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbumBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbumBuilder.java index 5148471..fd4b5a5 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbumBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbumBuilder.java @@ -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 David ‘Bombe’ Roden */ 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; } } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java index 26f6355..445fa6b 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java @@ -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 @@ -17,26 +17,27 @@ 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 David ‘Bombe’ Roden */ 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); } } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultImageBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultImageBuilder.java index 12f20c2..6df9a47 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultImageBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultImageBuilder.java @@ -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 @@ -17,31 +17,32 @@ 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 David Roden + * @author David ‘Bombe’ Roden */ 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); } } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java index 61bd530..87ad6bf 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java @@ -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 likedReplyIds = new CopyOnWriteArraySet(); /** 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 index 38e781f..0000000 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryAlbum.java +++ /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 . - */ - -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 David ‘Bombe’ Roden - */ -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 getAlbums() { - return memoryDatabase.getAlbums(this); - } - - @Override - public List getImages() { - return memoryDatabase.getImages(this); - } - - @Override - public Optional 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 index c00759e..0000000 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryImage.java +++ /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 . - */ - -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 David ‘Bombe’ Roden - */ -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 index cf16416..0000000 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryImageBuilder.java +++ /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 . - */ - -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 David ‘Bombe’ Roden - */ -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); - } - -} diff --git a/src/main/java/net/pterodactylus/sone/template/ParserFilter.java b/src/main/java/net/pterodactylus/sone/template/ParserFilter.java index bd6903b..bc496a2 100644 --- a/src/main/java/net/pterodactylus/sone/template/ParserFilter.java +++ b/src/main/java/net/pterodactylus/sone/template/ParserFilter.java @@ -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 parts = parser.parse(parserContext, new StringReader(postPart.getPost().getText())); diff --git a/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java b/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java index 18afb39..16db9c1 100644 --- a/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java +++ b/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java @@ -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 { } - /** 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 { if (linkType == LinkType.SONE) { if (line.length() >= (7 + 43)) { String soneId = line.substring(7, 50); - Optional sone = soneProvider.getSone(soneId); + Optional sone = database.getSone(soneId); if (!sone.isPresent()) { /* * don’t use create=true above, we don’t want * the empty shell. */ - sone = Optional.of(new DefaultSone(soneId, false)); + sone = Optional.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 { if (linkType == LinkType.POST) { if (line.length() >= (7 + 36)) { String postId = line.substring(7, 43); - Optional post = postProvider.getPost(postId); + Optional post = database.getPost(postId); if (post.isPresent()) { parts.add(new PostPart(post.get())); } else { diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index f6645ca..ed39b71 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -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()); diff --git a/src/test/java/net/pterodactylus/sone/data/impl/DefaultImageBuilderTest.java b/src/test/java/net/pterodactylus/sone/data/impl/DefaultImageBuilderTest.java index 2f00735..21c89d8 100644 --- a/src/test/java/net/pterodactylus/sone/data/impl/DefaultImageBuilderTest.java +++ b/src/test/java/net/pterodactylus/sone/data/impl/DefaultImageBuilderTest.java @@ -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() { diff --git a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java index 0dd1032..34767b5 100644 --- a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java +++ b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java @@ -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 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 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 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 parts; /* check empty http links. */ @@ -174,54 +170,4 @@ public class SoneTextParserTest extends TestCase { return text.toString(); } - /** - * Mock Sone provider. - * - * @author David ‘Bombe’ Roden - */ - private static class TestSoneProvider implements SoneProvider { - - /** - * {@inheritDoc} - */ - @Override - public Optional getSone(final String soneId) { - return Optional.of(new DefaultSone(soneId, false) { - - /** - * {@inheritDoc} - */ - @Override - public String getName() { - return soneId; - } - }); - } - - /** - * {@inheritDocs} - */ - @Override - public Collection getSones() { - return null; - } - - /** - * {@inheritDocs} - */ - @Override - public Collection getLocalSones() { - return null; - } - - /** - * {@inheritDocs} - */ - @Override - public Collection getRemoteSones() { - return null; - } - - } - } -- 2.7.4