From: David ‘Bombe’ Roden Date: Sat, 12 Oct 2013 13:27:57 +0000 (+0200) Subject: Image builders always need to know their Sones. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=4e61d9d4c916ff7d314baa2923087203aec3842d;p=Sone.git Image builders always need to know their Sones. --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index fd3b1d1..b8b8f1a 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -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; diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java index db302b0..5f10a48 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -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(); } } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/AbstractImageBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/AbstractImageBuilder.java index 1692bee..6ccd130 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/AbstractImageBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/AbstractImageBuilder.java @@ -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"); } 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 70fac0d..7805bf4 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java @@ -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(); 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 29cab0c..b53095f 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultImageBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultImageBuilder.java @@ -17,8 +17,11 @@ 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 diff --git a/src/main/java/net/pterodactylus/sone/database/ImageBuilder.java b/src/main/java/net/pterodactylus/sone/database/ImageBuilder.java index d9c313b..1180734 100644 --- a/src/main/java/net/pterodactylus/sone/database/ImageBuilder.java +++ b/src/main/java/net/pterodactylus/sone/database/ImageBuilder.java @@ -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); 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 38b50fa..c136818 100644 --- a/src/test/java/net/pterodactylus/sone/data/impl/DefaultImageBuilderTest.java +++ b/src/test/java/net/pterodactylus/sone/data/impl/DefaultImageBuilderTest.java @@ -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(); } }