X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2Fimpl%2FPostBuilderImpl.java;h=67138272482ef143584c93bbbb826852d283f80e;hb=4acdae8d247379dedcc5d41c8aded9503fcbaf72;hp=d407133bcdf86bdb58adaa7d0d80726f97a78004;hpb=da609f721e54691f27113e877a19637bd332abc3;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/data/impl/PostBuilderImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/PostBuilderImpl.java index d407133..6713827 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/PostBuilderImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/PostBuilderImpl.java @@ -22,8 +22,8 @@ import static com.google.common.base.Preconditions.checkState; import java.util.UUID; import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.PostBuilder; -import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.database.PostBuilder; +import net.pterodactylus.sone.database.SoneProvider; import org.apache.commons.lang.StringUtils; @@ -34,6 +34,9 @@ import org.apache.commons.lang.StringUtils; */ public class PostBuilderImpl implements PostBuilder { + /** The Sone provider for the created posts. */ + private final SoneProvider soneProvider; + /** Wether to create a post with a random ID. */ private boolean randomId; @@ -41,7 +44,7 @@ public class PostBuilderImpl implements PostBuilder { private String id; /** The sender of the post. */ - private Sone sender; + private String senderId; /** Whether to use the current time when creating the post. */ private boolean currentTime; @@ -53,7 +56,17 @@ public class PostBuilderImpl implements PostBuilder { private String text; /** The (optional) recipient of the post. */ - private Sone recipient; + private String recipientId; + + /** + * Creates a new post builder. + * + * @param soneProvider + * The Sone provider + */ + public PostBuilderImpl(SoneProvider soneProvider) { + this.soneProvider = soneProvider; + } /** * {@inheritDoc} @@ -62,11 +75,11 @@ public class PostBuilderImpl implements PostBuilder { public PostBuilder copyPost(Post post) { this.randomId = false; this.id = post.getId(); - this.sender = post.getSone(); + this.senderId = post.getSone().getId(); this.currentTime = false; this.time = post.getTime(); this.text = post.getText(); - this.recipient = post.getRecipient(); + this.recipientId = (post.getRecipient() != null) ? post.getRecipient().getId() : null; return this; } @@ -92,8 +105,8 @@ public class PostBuilderImpl implements PostBuilder { * {@inheritDoc} */ @Override - public PostBuilder from(Sone sender) { - this.sender = sender; + public PostBuilder from(String senderId) { + this.senderId = senderId; return this; } @@ -128,8 +141,8 @@ public class PostBuilderImpl implements PostBuilder { * {@inheritDoc} */ @Override - public PostBuilder to(Sone recipient) { - this.recipient = recipient; + public PostBuilder to(String recipientId) { + this.recipientId = recipientId; return this; } @@ -138,14 +151,12 @@ public class PostBuilderImpl implements PostBuilder { */ @Override public Post build() { - checkState(!randomId && (id == null), "neither random ID or custom ID set"); - checkState(randomId && (id != null), "both random ID and custom ID set"); - checkState(sender != null, "sender must not be null"); - checkState(!currentTime && (time == 0), "neither current time or custom time set"); - checkState(currentTime && (time != 0), "both current time and custom time set"); + checkState((randomId && (id == null)) || (!randomId && (id != null)), "exactly one of random ID or custom ID must be set"); + checkState(senderId != null, "sender must not be null"); + checkState((currentTime && (time == 0)) || (!currentTime && (time > 0)), "one of current time or custom time must be set"); checkState(!StringUtils.isBlank(text), "text must not be empty"); - checkState((recipient == null) || !recipient.equals(sender), "sender and recipient must not be the same"); - return new PostImpl(randomId ? UUID.randomUUID().toString() : id, sender, currentTime ? System.currentTimeMillis() : time, text).setRecipient(recipient); + checkState((recipientId == null) || !recipientId.equals(senderId), "sender and recipient must not be the same"); + return new PostImpl(soneProvider, randomId ? UUID.randomUUID().toString() : id, senderId, recipientId, currentTime ? System.currentTimeMillis() : time, text); } }