X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2Fimpl%2FPostBuilderImpl.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2Fimpl%2FPostBuilderImpl.java;h=d407133bcdf86bdb58adaa7d0d80726f97a78004;hb=da609f721e54691f27113e877a19637bd332abc3;hp=0000000000000000000000000000000000000000;hpb=14b1aa4bf4b88e80f9cce4ec14d0950727df4a5f;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 new file mode 100644 index 0000000..d407133 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/data/impl/PostBuilderImpl.java @@ -0,0 +1,151 @@ +/* + * Sone - PostBuilderImpl.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.data.impl; + +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 org.apache.commons.lang.StringUtils; + +/** + * {@link PostBuilder} implementation that creates {@link PostImpl} objects. + * + * @author David ‘Bombe’ Roden + */ +public class PostBuilderImpl implements PostBuilder { + + /** Wether to create a post with a random ID. */ + private boolean randomId; + + /** The ID of the post. */ + private String id; + + /** The sender of the post. */ + private Sone sender; + + /** Whether to use the current time when creating the post. */ + private boolean currentTime; + + /** The time of the post. */ + private long time; + + /** The text of the post. */ + private String text; + + /** The (optional) recipient of the post. */ + private Sone recipient; + + /** + * {@inheritDoc} + */ + @Override + public PostBuilder copyPost(Post post) { + this.randomId = false; + this.id = post.getId(); + this.sender = post.getSone(); + this.currentTime = false; + this.time = post.getTime(); + this.text = post.getText(); + this.recipient = post.getRecipient(); + return this; + } + + /** + * {@inheritDoc} + */ + @Override + public PostBuilder randomId() { + randomId = true; + return this; + } + + /** + * {@inheritDoc} + */ + @Override + public PostBuilder withId(String id) { + this.id = id; + return this; + } + + /** + * {@inheritDoc} + */ + @Override + public PostBuilder from(Sone sender) { + this.sender = sender; + return this; + } + + /** + * {@inheritDoc} + */ + @Override + public PostBuilder currentTime() { + currentTime = true; + return this; + } + + /** + * {@inheritDoc} + */ + @Override + public PostBuilder withTime(long time) { + this.time = time; + return this; + } + + /** + * {@inheritDoc} + */ + @Override + public PostBuilder withText(String text) { + this.text = text; + return this; + } + + /** + * {@inheritDoc} + */ + @Override + public PostBuilder to(Sone recipient) { + this.recipient = recipient; + return this; + } + + /** + * {@inheritDoc} + */ + @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(!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); + } + +}