From: David ‘Bombe’ Roden Date: Wed, 16 Oct 2013 05:35:51 +0000 (+0200) Subject: Add “image created” callback to ImageBuilder. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=6b2adeb0141b562811abcddd24d97c907700beb4;p=Sone.git Add “image created” callback to ImageBuilder. --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 7f74e14..9a8b7f9 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -69,6 +69,7 @@ import net.pterodactylus.sone.data.Sone.SoneStatus; import net.pterodactylus.sone.data.TemporaryImage; import net.pterodactylus.sone.database.Database; import net.pterodactylus.sone.database.DatabaseException; +import net.pterodactylus.sone.database.ImageBuilder.ImageCreated; import net.pterodactylus.sone.database.PostBuilder; import net.pterodactylus.sone.database.PostBuilder.PostCreated; import net.pterodactylus.sone.database.PostProvider; @@ -1162,7 +1163,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, logger.log(Level.WARNING, "Invalid album image encountered, aborting load!"); return; } - album.newImageBuilder().withId(imageId).created(creationTime).at(key).sized(width, height).build().modify().setTitle(title).setDescription(description).update(); + album.newImageBuilder().withId(imageId).created(creationTime).at(key).sized(width, height).build(Optional.absent()).modify().setTitle(title).setDescription(description).update(); } /* load avatar. */ @@ -1340,7 +1341,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, checkNotNull(temporaryImage, "temporaryImage must not be null"); checkArgument(sone.isLocal(), "sone must be a local Sone"); checkArgument(sone.equals(album.getSone()), "album must belong to the given Sone"); - Image image = album.newImageBuilder().withId(temporaryImage.getId()).sized(temporaryImage.getWidth(), temporaryImage.getHeight()).build(); + Image image = album.newImageBuilder().withId(temporaryImage.getId()).sized(temporaryImage.getWidth(), temporaryImage.getHeight()).build(imageCreated()); imageInserter.insertImage(temporaryImage, image); return image; } @@ -1948,4 +1949,13 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, }); } + public Optional imageCreated() { + return Optional.of(new ImageCreated() { + @Override + public void imageCreated(Image image) { + /* nothing happens here yet. */ + } + }); + } + } diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java index a4e73c7..d52e580 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -37,6 +37,7 @@ import net.pterodactylus.sone.data.Profile; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.Sone.SoneStatus; import net.pterodactylus.sone.data.impl.DefaultSone; +import net.pterodactylus.sone.database.ImageBuilder.ImageCreated; import net.pterodactylus.sone.database.PostBuilder; import net.pterodactylus.sone.database.PostBuilder.PostCreated; import net.pterodactylus.sone.database.PostReplyBuilder; @@ -488,7 +489,7 @@ public class SoneDownloader extends AbstractService { logger.log(Level.WARNING, String.format("Downloaded Sone %s contains image %s with invalid dimensions (%s, %s)!", sone, imageId, imageWidthString, imageHeightString)); return null; } - Image image = album.newImageBuilder().withId(imageId).at(imageKey).created(creationTime).sized(imageWidth, imageHeight).build(); + Image image = album.newImageBuilder().withId(imageId).at(imageKey).created(creationTime).sized(imageWidth, imageHeight).build(Optional.absent()); image = image.modify().setTitle(imageTitle).setDescription(imageDescription).update(); } } 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 c1640f9..0006dc4 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java @@ -90,11 +90,14 @@ public class DefaultAlbum extends AbstractAlbum { public ImageBuilder newImageBuilder() throws IllegalStateException { return new AbstractImageBuilder() { @Override - public Image build() throws IllegalStateException { + public Image build(Optional imageCreated) throws IllegalStateException { validate(); - DefaultImage memoryImage = new DefaultImage(database, getId(), sone, DefaultAlbum.this.id, key, getCreationTime(), width, height); - database.storeImage(memoryImage); - return memoryImage; + DefaultImage image = new DefaultImage(database, getId(), sone, DefaultAlbum.this.id, key, getCreationTime(), width, height); + database.storeImage(image); + if (imageCreated.isPresent()) { + imageCreated.get().imageCreated(image); + } + return image; } }; } diff --git a/src/main/java/net/pterodactylus/sone/database/ImageBuilder.java b/src/main/java/net/pterodactylus/sone/database/ImageBuilder.java index 97e72f1..3d0f87c 100644 --- a/src/main/java/net/pterodactylus/sone/database/ImageBuilder.java +++ b/src/main/java/net/pterodactylus/sone/database/ImageBuilder.java @@ -18,6 +18,8 @@ package net.pterodactylus.sone.database; import net.pterodactylus.sone.data.Image; +import com.google.common.base.Optional; + /** * Builder for {@link Image} objects. * @@ -30,6 +32,12 @@ public interface ImageBuilder { ImageBuilder at(String key); ImageBuilder sized(int width, int height); - Image build() throws IllegalStateException; + Image build(Optional imageCreated) throws IllegalStateException; + + interface ImageCreated { + + void imageCreated(Image image); + + } }