X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2Fimpl%2FAbstractAlbumBuilder.java;h=6ff935cdcfc1d64e99a34cac776e393cfee7288d;hb=e80be3f00b1d486fd3995d8f29ce3bf04da7b61d;hp=312c79550b51f71204eb673d09f623e5663205c5;hpb=5960d429777d56130ce044e7f38406be0f44495b;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/data/impl/AbstractAlbumBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/AbstractAlbumBuilder.java index 312c795..6ff935c 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/AbstractAlbumBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/AbstractAlbumBuilder.java @@ -17,10 +17,14 @@ package net.pterodactylus.sone.data.impl; -import static com.google.common.base.Preconditions.checkState; +import static com.google.common.base.Optional.absent; +import static com.google.common.base.Optional.fromNullable; +import static java.util.UUID.randomUUID; import net.pterodactylus.sone.database.AlbumBuilder; +import com.google.common.base.Optional; + /** * Abstract {@link AlbumBuilder} implementation. It stores the state of the new * album and performs validation, you only need to implement {@link #build()}. @@ -29,21 +33,11 @@ import net.pterodactylus.sone.database.AlbumBuilder; */ public abstract class AbstractAlbumBuilder implements AlbumBuilder { - /** Whether to create an album with a random ID. */ - protected boolean randomId; - - /** The ID of the album to create. */ - protected String id; - - @Override - public AlbumBuilder randomId() { - randomId = true; - return this; - } + private Optional id = absent(); @Override public AlbumBuilder withId(String id) { - this.id = id; + this.id = fromNullable(id); return this; } @@ -51,6 +45,10 @@ public abstract class AbstractAlbumBuilder implements AlbumBuilder { // PROTECTED METHODS // + protected String getId() { + return id.isPresent() ? id.get() : randomUUID().toString(); + } + /** * Validates the state of this post builder. * @@ -58,7 +56,6 @@ public abstract class AbstractAlbumBuilder implements AlbumBuilder { * if the state is not valid for building a new post */ protected void validate() throws IllegalStateException { - checkState((randomId && (id == null)) || (!randomId && (id != null)), "exactly one of random ID or custom ID must be set"); } }