Image builders always need to know their Sones.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 12 Oct 2013 13:27:57 +0000 (15:27 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:17:43 +0000 (22:17 +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/AbstractImageBuilder.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultImageBuilder.java
src/main/java/net/pterodactylus/sone/database/ImageBuilder.java
src/test/java/net/pterodactylus/sone/data/impl/DefaultImageBuilderTest.java

index fd3b1d1..b8b8f1a 100644 (file)
@@ -1252,7 +1252,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                logger.log(Level.WARNING, "Invalid album image encountered, aborting load!");
                                return;
                        }
-                       album.newImageBuilder().withId(imageId).by(sone).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().modify().setTitle(title).setDescription(description).update();
                }
 
                /* load avatar. */
@@ -1606,7 +1606,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()).by(sone).createdNow().sized(temporaryImage.getWidth(), temporaryImage.getHeight()).build();
+               Image image = album.newImageBuilder().withId(temporaryImage.getId()).createdNow().sized(temporaryImage.getWidth(), temporaryImage.getHeight()).build();
                database.storeImage(image);
                imageInserter.insertImage(temporaryImage, image);
                return image;
index db302b0..5f10a48 100644 (file)
@@ -488,7 +488,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).by(sone).at(imageKey).created(creationTime).sized(imageWidth, imageHeight).build();
+                                               Image image = album.newImageBuilder().withId(imageId).at(imageKey).created(creationTime).sized(imageWidth, imageHeight).build();
                                                image = image.modify().setTitle(imageTitle).setDescription(imageDescription).update();
                                        }
                                }
index 1692bee..6ccd130 100644 (file)
@@ -19,7 +19,6 @@ package net.pterodactylus.sone.data.impl;
 import static com.google.common.base.Preconditions.checkState;
 import static java.util.UUID.randomUUID;
 
-import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.database.ImageBuilder;
 
 /**
@@ -35,7 +34,6 @@ public abstract class AbstractImageBuilder implements ImageBuilder {
 
        /** The ID of the album to create. */
        protected String id;
-       protected Sone sone;
        protected long creationTime;
        protected boolean createdNow;
        protected String key;
@@ -55,12 +53,6 @@ public abstract class AbstractImageBuilder implements ImageBuilder {
        }
 
        @Override
-       public ImageBuilder by(Sone sone) {
-               this.sone = sone;
-               return this;
-       }
-
-       @Override
        public ImageBuilder created(long creationTime) {
                this.creationTime = creationTime;
                return this;
@@ -105,7 +97,6 @@ public abstract class AbstractImageBuilder implements ImageBuilder {
         */
        protected void validate() throws IllegalStateException {
                checkState((randomId && (id == null)) || (!randomId && (id != null)), "exactly one of random ID or custom ID must be set");
-               checkState(sone != null, "sone must not be null");
                checkState((createdNow && (creationTime == 0)) || (!createdNow && (creationTime > 0)), "exactly one of created now or creation time must be set");
                checkState((width > 0) && (height > 0), "width and height must be set");
        }
index 70fac0d..7805bf4 100644 (file)
@@ -269,7 +269,7 @@ public class DefaultAlbum implements Album {
 
        @Override
        public ImageBuilder newImageBuilder() throws IllegalStateException {
-               return new DefaultImageBuilder(this) {
+               return new DefaultImageBuilder(sone, this) {
                        @Override
                        public Image build() throws IllegalStateException {
                                Image image = super.build();
index 29cab0c..b53095f 100644 (file)
 
 package net.pterodactylus.sone.data.impl;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+
 import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Image;
+import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.database.ImageBuilder;
 
 /**
@@ -28,10 +31,12 @@ import net.pterodactylus.sone.database.ImageBuilder;
  */
 public class DefaultImageBuilder extends AbstractImageBuilder {
 
+       protected final Sone sone;
        protected final Album album;
 
-       public DefaultImageBuilder(Album album) {
-               this.album = album;
+       public DefaultImageBuilder(Sone sone, Album album) {
+               this.sone = checkNotNull(sone, "sone must not be null");
+               this.album = checkNotNull(album, "album must not be null");
        }
 
        @Override
index d9c313b..1180734 100644 (file)
@@ -17,7 +17,6 @@
 package net.pterodactylus.sone.database;
 
 import net.pterodactylus.sone.data.Image;
-import net.pterodactylus.sone.data.Sone;
 
 /**
  * Builder for {@link Image} objects.
@@ -28,7 +27,6 @@ public interface ImageBuilder {
 
        ImageBuilder randomId();
        ImageBuilder withId(String id);
-       ImageBuilder by(Sone sone);
        ImageBuilder created(long creationTime);
        ImageBuilder createdNow();
        ImageBuilder at(String key);
index 38b50fa..c136818 100644 (file)
@@ -45,11 +45,11 @@ public class DefaultImageBuilderTest {
 
        private final Sone sone = mock(Sone.class);
        private final Album album = mock(Album.class);
-       private final ImageBuilder imageBuilder = new DefaultImageBuilder(album);
+       private final ImageBuilder imageBuilder = new DefaultImageBuilder(sone, album);
 
        @Test
        public void testImageCreationWithAllExplicitParameters() {
-               Image image = imageBuilder.withId(ID).by(sone).created(CREATION_TIME).at(KEY).sized(WIDTH, HEIGHT).build();
+               Image image = imageBuilder.withId(ID).created(CREATION_TIME).at(KEY).sized(WIDTH, HEIGHT).build();
                assertThat(image, CoreMatchers.notNullValue());
                assertThat(image.getId(), is(ID));
                assertThat(image.getSone(), is(sone));
@@ -62,7 +62,7 @@ public class DefaultImageBuilderTest {
        @Test
        public void testImageCreationWithRandomId() {
                Sone sone = mock(Sone.class);
-               Image image = imageBuilder.randomId().by(sone).created(CREATION_TIME).at(KEY).sized(WIDTH, HEIGHT).build();
+               Image image = imageBuilder.randomId().created(CREATION_TIME).at(KEY).sized(WIDTH, HEIGHT).build();
                assertThat(image, CoreMatchers.notNullValue());
                assertThat(image.getId(), notNullValue());
                assertThat(image.getSone(), is(sone));
@@ -74,7 +74,7 @@ public class DefaultImageBuilderTest {
 
        @Test
        public void testImageCreationWithCurrentTime() {
-               Image image = imageBuilder.withId(ID).by(sone).createdNow().at(KEY).sized(WIDTH, HEIGHT).build();
+               Image image = imageBuilder.withId(ID).createdNow().at(KEY).sized(WIDTH, HEIGHT).build();
                assertThat(image, CoreMatchers.notNullValue());
                assertThat(image.getId(), is(ID));
                assertThat(image.getSone(), is(sone));
@@ -86,32 +86,27 @@ public class DefaultImageBuilderTest {
 
        @Test(expected = IllegalStateException.class)
        public void testThatImageCreationWithoutAnIdFails() {
-               imageBuilder.by(sone).created(CREATION_TIME).at(KEY).sized(WIDTH, HEIGHT).build();
-       }
-
-       @Test(expected = IllegalStateException.class)
-       public void testThatImageCreationWithoutASoneFails() {
-               imageBuilder.withId(ID).created(CREATION_TIME).at(KEY).sized(WIDTH, HEIGHT).build();
+               imageBuilder.created(CREATION_TIME).at(KEY).sized(WIDTH, HEIGHT).build();
        }
 
        @Test(expected = IllegalStateException.class)
        public void testThatImageCreationWithoutATimeFails() {
-               imageBuilder.withId(ID).by(sone).at(KEY).sized(WIDTH, HEIGHT).build();
+               imageBuilder.withId(ID).at(KEY).sized(WIDTH, HEIGHT).build();
        }
 
        @Test(expected = IllegalStateException.class)
        public void testThatImageCreationWithoutASizeFails() {
-               imageBuilder.withId(ID).by(sone).createdNow().at(KEY).build();
+               imageBuilder.withId(ID).createdNow().at(KEY).build();
        }
 
        @Test(expected = IllegalStateException.class)
        public void testThatImageCreationWithoutInvalidWidthFails() {
-               imageBuilder.withId(ID).by(sone).createdNow().at(KEY).sized(0, 1).build();
+               imageBuilder.withId(ID).createdNow().at(KEY).sized(0, 1).build();
        }
 
        @Test(expected = IllegalStateException.class)
        public void testThatImageCreationWithoutInvalidHeightFails() {
-               imageBuilder.withId(ID).by(sone).createdNow().at(KEY).sized(1, 0).build();
+               imageBuilder.withId(ID).createdNow().at(KEY).sized(1, 0).build();
        }
 
 }