From 9e2baf6cc6a21e8b7e5a9f8bcfc70ebf9bfab106 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 15 Oct 2013 22:06:13 +0200 Subject: [PATCH] Remove randomId() and currentTime() methods from ImageBuilder. --- .../java/net/pterodactylus/sone/core/Core.java | 3 +- .../sone/data/impl/AbstractImageBuilder.java | 37 +++++++--------------- .../pterodactylus/sone/database/ImageBuilder.java | 2 -- 3 files changed, 13 insertions(+), 29 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index e9ebfda..8c9bc77 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -17,7 +17,6 @@ package net.pterodactylus.sone.core; -import static com.google.common.base.Optional.absent; import static com.google.common.base.Optional.of; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; @@ -1387,7 +1386,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()).createdNow().sized(temporaryImage.getWidth(), temporaryImage.getHeight()).build(); + Image image = album.newImageBuilder().withId(temporaryImage.getId()).sized(temporaryImage.getWidth(), temporaryImage.getHeight()).build(); imageInserter.insertImage(temporaryImage, image); return image; } 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 6ccd130..13bf24b 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/AbstractImageBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/AbstractImageBuilder.java @@ -16,11 +16,17 @@ */ package net.pterodactylus.sone.data.impl; +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.checkState; +import static java.lang.System.currentTimeMillis; import static java.util.UUID.randomUUID; import net.pterodactylus.sone.database.ImageBuilder; +import com.google.common.base.Optional; + /** * Abstract {@link ImageBuilder} implementation. It stores the state of the new * album and performs validation, you only need to implement {@link #build()}. @@ -29,38 +35,21 @@ import net.pterodactylus.sone.database.ImageBuilder; */ public abstract class AbstractImageBuilder implements ImageBuilder { - /** Whether to create an album with a random ID. */ - protected boolean randomId; - - /** The ID of the album to create. */ - protected String id; - protected long creationTime; - protected boolean createdNow; + protected Optional id = absent(); + protected Optional creationTime = absent(); protected String key; protected int width; protected int height; @Override - public ImageBuilder randomId() { - randomId = true; - return this; - } - - @Override public ImageBuilder withId(String id) { - this.id = id; + this.id = fromNullable(id); return this; } @Override public ImageBuilder created(long creationTime) { - this.creationTime = creationTime; - return this; - } - - @Override - public ImageBuilder createdNow() { - createdNow = true; + this.creationTime = of(creationTime); return this; } @@ -82,11 +71,11 @@ public abstract class AbstractImageBuilder implements ImageBuilder { // protected String getId() { - return randomId ? randomUUID().toString() : id; + return id.isPresent() ? id.get() : randomUUID().toString(); } protected long getCreationTime() { - return createdNow ? System.currentTimeMillis() : creationTime; + return creationTime.isPresent() ? creationTime.get() : currentTimeMillis(); } /** @@ -96,8 +85,6 @@ public abstract class AbstractImageBuilder implements ImageBuilder { * if the state is not valid for building a new image */ protected void validate() throws IllegalStateException { - checkState((randomId && (id == null)) || (!randomId && (id != null)), "exactly one of random ID or custom ID must be set"); - 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/database/ImageBuilder.java b/src/main/java/net/pterodactylus/sone/database/ImageBuilder.java index 1180734..97e72f1 100644 --- a/src/main/java/net/pterodactylus/sone/database/ImageBuilder.java +++ b/src/main/java/net/pterodactylus/sone/database/ImageBuilder.java @@ -25,10 +25,8 @@ import net.pterodactylus.sone.data.Image; */ public interface ImageBuilder { - ImageBuilder randomId(); ImageBuilder withId(String id); ImageBuilder created(long creationTime); - ImageBuilder createdNow(); ImageBuilder at(String key); ImageBuilder sized(int width, int height); -- 2.7.4