Add “image created” callback to ImageBuilder.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 16 Oct 2013 05:35:51 +0000 (07:35 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:27 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/core/Core.java
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/database/ImageBuilder.java

index 7f74e14..9a8b7f9 100644 (file)
@@ -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.<ImageCreated>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> imageCreated() {
+               return Optional.<ImageCreated>of(new ImageCreated() {
+                       @Override
+                       public void imageCreated(Image image) {
+                               /* nothing happens here yet. */
+                       }
+               });
+       }
+
 }
index a4e73c7..d52e580 100644 (file)
@@ -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.<ImageCreated>absent());
                                                image = image.modify().setTitle(imageTitle).setDescription(imageDescription).update();
                                        }
                                }
index c1640f9..0006dc4 100644 (file)
@@ -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> 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;
                        }
                };
        }
index 97e72f1..3d0f87c 100644 (file)
@@ -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> imageCreated) throws IllegalStateException;
+
+       interface ImageCreated {
+
+               void imageCreated(Image image);
+
+       }
 
 }