X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2Fimpl%2FAbstractImageBuilder.java;h=13bf24b8625175df843d2f45f23580155f6cc5fe;hb=2e03e9dddbea4b81aacaf1aa316f5c3ccffd4bf9;hp=209ef0f3003052e9db38cbae5e7f2c4dd4afd8ce;hpb=fc2165a3e44887c18d6faca054cc4efa384ca797;p=Sone.git 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 209ef0f..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,13 +16,17 @@ */ package net.pterodactylus.sone.data.impl; -import static com.google.common.base.Preconditions.checkNotNull; +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.data.Album; -import net.pterodactylus.sone.data.Sone; 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()}. @@ -31,51 +35,21 @@ import net.pterodactylus.sone.database.ImageBuilder; */ public abstract class AbstractImageBuilder implements ImageBuilder { - protected final Album album; - - /** Whether to create an album with a random ID. */ - protected boolean randomId; - - /** The ID of the album to create. */ - protected String id; - protected Sone sone; - protected long creationTime; - protected boolean createdNow; + protected Optional id = absent(); + protected Optional creationTime = absent(); protected String key; protected int width; protected int height; - public AbstractImageBuilder(Album album) { - this.album = checkNotNull(album, "album must not be null"); - } - - @Override - public ImageBuilder randomId() { - randomId = true; - return this; - } - @Override public ImageBuilder withId(String id) { - this.id = id; - return this; - } - - @Override - public ImageBuilder by(Sone sone) { - this.sone = sone; + 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; } @@ -96,6 +70,14 @@ public abstract class AbstractImageBuilder implements ImageBuilder { // PROTECTED METHODS // + protected String getId() { + return id.isPresent() ? id.get() : randomUUID().toString(); + } + + protected long getCreationTime() { + return creationTime.isPresent() ? creationTime.get() : currentTimeMillis(); + } + /** * Validates the state of this image builder. * @@ -103,9 +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(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"); }