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.checkNotNull;
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.Post;
import net.pterodactylus.sone.database.Database;
import net.pterodactylus.sone.database.PostBuilder;
protected final Database database;
protected final String senderId;
- /** Wether to create a post with a random ID. */
- protected boolean randomId;
-
/** The ID of the post. */
- protected String id;
-
- /** Whether to use the current time when creating the post. */
- protected boolean currentTime;
+ protected Optional<String> id = absent();
/** The time of the post. */
- protected long time;
+ protected Optional<Long> time = absent();
/** The text of the post. */
protected String text;
* {@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 currentTime() {
- currentTime = true;
+ this.id = fromNullable(id);
return this;
}
*/
@Override
public PostBuilder withTime(long time) {
- this.time = time;
+ this.time = of(time);
return this;
}
// PROTECTED METHODS
//
+ protected String getId() {
+ return id.isPresent() ? id.get() : randomUUID().toString();
+ }
+
+ protected long getTime() {
+ return time.isPresent() ? time.get() : currentTimeMillis();
+ }
+
/**
* Validates the state of this post builder.
*
* 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");
- 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(!recipientId.isPresent() || !senderId.equals(recipientId.get()), "sender and recipient must not be the same");
}
package net.pterodactylus.sone.data.impl;
-import java.util.UUID;
-
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.database.Database;
import net.pterodactylus.sone.database.PostBuilder;
@Override
public Post build(Optional<PostCreated> postCreated) {
validate();
- PostImpl post = new PostImpl(database, randomId ? UUID.randomUUID().toString() : id, senderId, recipientId.orNull(), currentTime ? System.currentTimeMillis() : time, text);
+ PostImpl post = new PostImpl(database, getId(), senderId, recipientId.orNull(), getTime(), text);
if (postCreated.isPresent()) {
postCreated.get().postCreated(post);
}
public interface PostBuilder {
/**
- * Configures this builder to use a random ID for the new post. If this
- * method is used, {@link #withId(String)} must not be used.
- *
- * @return This post builder
- */
- public PostBuilder randomId();
-
- /**
* Configures this builder to use the given ID as ID for the new post. If
* this method is used, {@link #randomId()} must not be used.
*
public PostBuilder withId(String id);
/**
- * Configures this builder to use the current time when creating the post.
- * If this method is used, {@link #withTime(long)} must not be used.
- *
- * @return This post builder
- */
- public PostBuilder currentTime();
-
- /**
* Configures the builder to use the given time as time for the new post. If
* this method is used, {@link #currentTime()} must not be used.
*
if (sone.equals(recipient)) {
return new ErrorResponse("Sone and Recipient must not be the same.");
}
- Post post = sone.newPostBuilder().randomId().currentTime().to(fromNullable(recipient).transform(GET_ID)).withText(text).build(of(getCore().postCreated()));
+ Post post = sone.newPostBuilder().to(fromNullable(recipient).transform(GET_ID)).withText(text).build(of(getCore().postCreated()));
return new Response("PostCreated", new SimpleFieldSetBuilder().put("Post", post.getId()).get());
}
}
Optional<Sone> recipient = webInterface.getCore().getSone(recipientId);
text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text);
- sender.get().newPostBuilder().randomId().currentTime().to(recipient.transform(Identified.GET_ID)).withText(text).build(of(webInterface.getCore().postCreated()));
+ sender.get().newPostBuilder().to(recipient.transform(Identified.GET_ID)).withText(text).build(of(webInterface.getCore().postCreated()));
throw new RedirectException(returnPage);
}
templateContext.set("errorTextEmpty", true);
return createErrorJsonObject("text-required");
}
text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text);
- Post newPost = sender.get().newPostBuilder().randomId().currentTime().to(recipient.transform(GET_ID)).withText(text).build(of(webInterface.getCore().postCreated()));
+ Post newPost = sender.get().newPostBuilder().to(recipient.transform(GET_ID)).withText(text).build(of(webInterface.getCore().postCreated()));
return createSuccessJsonObject().put("postId", newPost.getId()).put("sone", sender.get().getId()).put("recipient", newPost.getRecipientId().orNull());
}