From: David ‘Bombe’ Roden Date: Sat, 12 Oct 2013 09:16:01 +0000 (+0200) Subject: Move required parameters for image into the image builder. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=e7905b3679a3ad0d488f29522a2d1e3ed7b84367;p=Sone.git Move required parameters for image into the image builder. --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index fac14a9..d20549f 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -1284,8 +1284,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, logger.log(Level.WARNING, "Invalid album image encountered, aborting load!"); return; } - Image image = getImage(imageId).modify().setSone(sone).setCreationTime(creationTime).setKey(key).setTitle(title).setDescription(description).setWidth(width).setHeight(height).update(); - album.addImage(image); + album.newImageBuilder().withId(imageId).by(sone).created(creationTime).at(key).sized(width, height).build().modify().setTitle(title).setDescription(description).update(); } /* load avatar. */ @@ -1639,8 +1638,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 = database.newImageBuilder().withId(temporaryImage.getId()).build().modify().setSone(sone).setCreationTime(System.currentTimeMillis()).update(); - album.addImage(image); + Image image = album.newImageBuilder().withId(temporaryImage.getId()).by(sone).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 53eef16..9e50886 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -488,10 +488,8 @@ 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 = core.getImage(imageId).modify().setSone(sone).setKey(imageKey).setCreationTime(creationTime).update(); + Image image = album.newImageBuilder().withId(imageId).by(sone).at(imageKey).created(creationTime).sized(imageWidth, imageHeight).build(); image = image.modify().setTitle(imageTitle).setDescription(imageDescription).update(); - image = image.modify().setWidth(imageWidth).setHeight(imageHeight).update(); - album.addImage(image); } } album.modify().setAlbumImage(albumImageId).update(); diff --git a/src/main/java/net/pterodactylus/sone/data/Album.java b/src/main/java/net/pterodactylus/sone/data/Album.java index 09a7da4..8864f13 100644 --- a/src/main/java/net/pterodactylus/sone/data/Album.java +++ b/src/main/java/net/pterodactylus/sone/data/Album.java @@ -26,6 +26,8 @@ import java.util.Comparator; import java.util.List; import javax.annotation.Nonnull; +import net.pterodactylus.sone.database.ImageBuilder; + import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.collect.FluentIterable; @@ -178,14 +180,6 @@ public interface Album extends Identified, Fingerprintable { List getImages(); /** - * Adds the given image to this album. - * - * @param image - * The image to add - */ - void addImage(Image image); - - /** * Removes the given image from this album. * * @param image @@ -276,6 +270,8 @@ public interface Album extends Identified, Fingerprintable { */ String getDescription(); + ImageBuilder newImageBuilder() throws IllegalStateException; + /** * Returns a modifier for this album. * diff --git a/src/main/java/net/pterodactylus/sone/data/AlbumImpl.java b/src/main/java/net/pterodactylus/sone/data/AlbumImpl.java index f489b0b..0f6ce81 100644 --- a/src/main/java/net/pterodactylus/sone/data/AlbumImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/AlbumImpl.java @@ -29,6 +29,9 @@ import java.util.List; import java.util.Map; import java.util.UUID; +import net.pterodactylus.sone.data.impl.ImageBuilderImpl; +import net.pterodactylus.sone.database.ImageBuilder; + import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.base.Predicates; @@ -172,24 +175,6 @@ public class AlbumImpl implements Album { } @Override - public void addImage(Image image) { - checkNotNull(image, "image must not be null"); - checkNotNull(image.getSone(), "image must have an owner"); - checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album"); - if (image.getAlbum() != null) { - image.getAlbum().removeImage(image); - } - image.setAlbum(this); - if (imageIds.isEmpty() && (albumImage == null)) { - albumImage = image.getId(); - } - if (!imageIds.contains(image.getId())) { - imageIds.add(image.getId()); - images.put(image.getId(), image); - } - } - - @Override public void removeImage(Image image) { checkNotNull(image, "image must not be null"); checkNotNull(image.getSone(), "image must have an owner"); @@ -281,6 +266,22 @@ public class AlbumImpl implements Album { } @Override + public ImageBuilder newImageBuilder() throws IllegalStateException { + return new ImageBuilderImpl() { + @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; + } + }; + } + + @Override public Modifier modify() throws IllegalStateException { // TODO: reenable check for local Sones return new Modifier() { diff --git a/src/main/java/net/pterodactylus/sone/data/Image.java b/src/main/java/net/pterodactylus/sone/data/Image.java index e3e1b4d..82c831d 100644 --- a/src/main/java/net/pterodactylus/sone/data/Image.java +++ b/src/main/java/net/pterodactylus/sone/data/Image.java @@ -46,16 +46,6 @@ public interface Image extends Identified, Fingerprintable { Album getAlbum(); /** - * Sets the album this image belongs to. The album of an image can only be - * set once, and it is usually called by {@link Album#addImage(Image)}. - * - * @param album - * The album this image belongs to - * @return This image - */ - Image setAlbum(Album album); - - /** * Returns the request key of this image. * * @return The request key of this image @@ -118,20 +108,12 @@ public interface Image extends Identified, Fingerprintable { interface Modifier { - Modifier setSone(Sone sone); - - Modifier setCreationTime(long creationTime); - Modifier setKey(String key); Modifier setTitle(String title); Modifier setDescription(String description); - Modifier setWidth(int width); - - Modifier setHeight(int height); - Image update() throws IllegalStateException; } diff --git a/src/main/java/net/pterodactylus/sone/data/ImageImpl.java b/src/main/java/net/pterodactylus/sone/data/ImageImpl.java index 447bb82..0014a13 100644 --- a/src/main/java/net/pterodactylus/sone/data/ImageImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/ImageImpl.java @@ -18,7 +18,6 @@ package net.pterodactylus.sone.data; import static com.google.common.base.Optional.absent; import static com.google.common.base.Optional.fromNullable; -import static com.google.common.base.Optional.of; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; @@ -39,7 +38,7 @@ public class ImageImpl implements Image { private final String id; /** The Sone the image belongs to. */ - private Sone sone; + private final Sone sone; /** The album this image belongs to. */ private Album album; @@ -48,13 +47,13 @@ public class ImageImpl implements Image { private String key; /** The creation time of the image. */ - private long creationTime; + private final long creationTime; /** The width of the image. */ - private int width; + private final int width; /** The height of the image. */ - private int height; + private final int height; /** The title of the image. */ private String title; @@ -63,9 +62,8 @@ public class ImageImpl implements Image { private String description; /** Creates a new image with a random ID. */ - public ImageImpl() { - this(UUID.randomUUID().toString()); - this.creationTime = System.currentTimeMillis(); + public ImageImpl(Sone sone, long creationTime, String key, int width, int height) { + this(UUID.randomUUID().toString(), sone, creationTime, key, width, height); } /** @@ -73,9 +71,15 @@ public class ImageImpl implements Image { * * @param id * The ID of the image + * @param creationTime */ - public ImageImpl(String id) { + public ImageImpl(String id, Sone sone, long creationTime, String key, int width, int height) { this.id = checkNotNull(id, "id must not be null"); + this.sone = sone; + this.creationTime = creationTime; + this.key = key; + this.width = width; + this.height = height; } // @@ -98,14 +102,6 @@ public class ImageImpl implements Image { } @Override - public Image setAlbum(Album album) { - checkNotNull(album, "album must not be null"); - checkNotNull(album.getSone().equals(getSone()), "album must belong to the same Sone as this image"); - this.album = album; - return this; - } - - @Override public String getKey() { return key; } @@ -143,32 +139,10 @@ public class ImageImpl implements Image { public Modifier modify() throws IllegalStateException { // TODO: reenable check for local images return new Modifier() { - private Optional sone = absent(); - - private Optional creationTime = absent(); - private Optional key = absent(); - private Optional title = absent(); - private Optional description = absent(); - private Optional width = absent(); - - private Optional height = absent(); - - @Override - public Modifier setSone(Sone sone) { - this.sone = fromNullable(sone); - return this; - } - - @Override - public Modifier setCreationTime(long creationTime) { - this.creationTime = of(creationTime); - return this; - } - @Override public Modifier setKey(String key) { this.key = fromNullable(key); @@ -188,31 +162,9 @@ public class ImageImpl implements Image { } @Override - public Modifier setWidth(int width) { - this.width = of(width); - return this; - } - - @Override - public Modifier setHeight(int height) { - this.height = of(height); - return this; - } - - @Override public Image update() throws IllegalStateException { - checkState(!sone.isPresent() || (ImageImpl.this.sone == null) || sone.get().equals(ImageImpl.this.sone), "can not change Sone once set"); - checkState(!creationTime.isPresent() || ((ImageImpl.this.creationTime == 0) || (ImageImpl.this.creationTime == creationTime.get())), "can not change creation time once set"); - checkState(!key.isPresent() || (ImageImpl.this.key == null) || key.get().equals(ImageImpl.this.key), "can not change key once set"); - checkState(!width.isPresent() || (ImageImpl.this.width == 0) || width.get().equals(ImageImpl.this.width), "can not change width once set"); - checkState(!height.isPresent() || (ImageImpl.this.height == 0) || height.get().equals(ImageImpl.this.height), "can not change height once set"); - - if (sone.isPresent()) { - ImageImpl.this.sone = sone.get(); - } - if (creationTime.isPresent()) { - ImageImpl.this.creationTime = creationTime.get(); - } + checkState(!key.isPresent() || (ImageImpl.this.key == null), "key can not be changed"); + if (key.isPresent()) { ImageImpl.this.key = key.get(); } @@ -222,12 +174,6 @@ public class ImageImpl implements Image { if (description.isPresent()) { ImageImpl.this.description = description.get(); } - if (width.isPresent()) { - ImageImpl.this.width = width.get(); - } - if (height.isPresent()) { - ImageImpl.this.height = height.get(); - } return ImageImpl.this; } 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 533ba39..1b9e3a8 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/AbstractImageBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/AbstractImageBuilder.java @@ -18,6 +18,7 @@ package net.pterodactylus.sone.data.impl; import static com.google.common.base.Preconditions.checkState; +import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.database.ImageBuilder; /** @@ -33,6 +34,12 @@ 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; + protected int width; + protected int height; @Override public ImageBuilder randomId() { @@ -46,6 +53,37 @@ public abstract class AbstractImageBuilder implements ImageBuilder { return this; } + @Override + public ImageBuilder by(Sone sone) { + this.sone = sone; + return this; + } + + @Override + public ImageBuilder created(long creationTime) { + this.creationTime = creationTime; + return this; + } + + @Override + public ImageBuilder createdNow() { + createdNow = true; + return this; + } + + @Override + public ImageBuilder at(String key) { + this.key = key; + return this; + } + + @Override + public ImageBuilder sized(int width, int height) { + this.width = width; + this.height = height; + return this; + } + // // PROTECTED METHODS // @@ -58,6 +96,9 @@ 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/ImageBuilderImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/ImageBuilderImpl.java index 870b5d7..83e1dbc 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/ImageBuilderImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/ImageBuilderImpl.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.data.impl; +import static java.util.UUID.randomUUID; + import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.ImageImpl; import net.pterodactylus.sone.database.ImageBuilder; @@ -31,7 +33,9 @@ public class ImageBuilderImpl extends AbstractImageBuilder { @Override public Image build() throws IllegalStateException { validate(); - return randomId ? new ImageImpl() : new ImageImpl(id); + String id = randomId ? randomUUID().toString() : this.id; + long creationTime = createdNow ? System.currentTimeMillis() : this.creationTime; + return new ImageImpl(id, sone, creationTime, key, width, height); } } diff --git a/src/main/java/net/pterodactylus/sone/database/ImageBuilder.java b/src/main/java/net/pterodactylus/sone/database/ImageBuilder.java index af405c7..d9c313b 100644 --- a/src/main/java/net/pterodactylus/sone/database/ImageBuilder.java +++ b/src/main/java/net/pterodactylus/sone/database/ImageBuilder.java @@ -17,6 +17,7 @@ package net.pterodactylus.sone.database; import net.pterodactylus.sone.data.Image; +import net.pterodactylus.sone.data.Sone; /** * Builder for {@link Image} objects. @@ -26,8 +27,12 @@ import net.pterodactylus.sone.data.Image; public interface ImageBuilder { ImageBuilder randomId(); - ImageBuilder withId(String id); + ImageBuilder by(Sone sone); + ImageBuilder created(long creationTime); + ImageBuilder createdNow(); + ImageBuilder at(String key); + ImageBuilder sized(int width, int height); Image build() throws IllegalStateException;