synchronized (knownPosts) {
for (Post post : sone.getPosts()) {
PostBuilder postBuilder = postBuilderFactory.newPostBuilder();
- postBuilder.copyPost(post).from(storedSone);
+ postBuilder.copyPost(post).from(storedSone.getId());
Post newPost = postBuilder.build().setKnown(knownPosts.contains(post.getId()));
if (!storedPosts.contains(newPost)) {
if (newPost.getTime() < getSoneFollowingTime(sone)) {
logger.log(Level.WARNING, "Invalid post found, aborting load!");
return;
}
- PostBuilder postBuilder = postBuilderFactory.newPostBuilder().withId(postId).from(sone).withTime(postTime).withText(postText);
+ PostBuilder postBuilder = postBuilderFactory.newPostBuilder().withId(postId).from(sone.getId()).withTime(postTime).withText(postText);
if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
- postBuilder.to(getSone(postRecipientId));
+ postBuilder.to(postRecipientId);
}
posts.add(postBuilder.build());
}
return null;
}
PostBuilder postBuilder = postBuilderFactory.newPostBuilder();
- postBuilder.from(sone).randomId().withTime(time).withText(text.trim());
+ postBuilder.from(sone.getId()).randomId().withTime(time).withText(text.trim());
if (recipient != null) {
- postBuilder.to(recipient);
+ postBuilder.to(recipient.getId());
}
final Post post = postBuilder.build();
synchronized (posts) {
try {
PostBuilder postBuilder = core.postBuilder();
/* TODO - parse time correctly. */
- postBuilder.withId(postId).from(sone).withTime(Long.parseLong(postTime)).withText(postText);
+ postBuilder.withId(postId).from(sone.getId()).withTime(Long.parseLong(postTime)).withText(postText);
if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
- postBuilder.to(core.getSone(postRecipientId));
+ postBuilder.to(postRecipientId);
}
posts.add(postBuilder.build());
} catch (NumberFormatException nfe1) {
/**
* Configures this builder to use the given Sone as sender of the new post.
*
- * @param sender
- * The sender of the post
+ * @param senderId
+ * The ID of the sender of the post
* @return This post builder
*/
- public PostBuilder from(Sone sender);
+ public PostBuilder from(String senderId);
/**
* Configures this builder to use a random ID for the new post. If this
* Configures the builder to use the given {@link Sone} as recipient for the
* post.
*
- * @param recipient
- * The recipient of the post
+ * @param recipientId
+ * The ID of the recipient of the post
* @return This post builder
*/
- public PostBuilder to(Sone recipient);
+ public PostBuilder to(String recipientId);
/**
* Verifies this builder’s configuration and creates a new post.
* <ul>
* <li>Exactly one of {@link #randomId()} or {@link #withId(String)} must
* have been called.</li>
- * <li>The {@link #from(Sone) sender} must not be {@code null}.</li>
+ * <li>The {@link #from(String) sender} must not be {@code null}.</li>
* <li>Exactly one of {@link #currentTime()} or {@link #withTime(long)} must
* have been called.</li>
* <li>The {@link #withText(String) text} must not be {@code null} and must
* contain something other than whitespace.</li>
- * <li>The {@link #to(Sone) recipient} must either not have been set, or it
- * must have been set to a {@link Sone} other than {@link #from(Sone) the
- * sender}.</li>
+ * <li>The {@link #to(String) recipient} must either not have been set, or
+ * it must have been set to a {@link Sone} other than {@link #from(String)
+ * the sender}.</li>
* </ul>
*
* @return A new post
package net.pterodactylus.sone.data.impl;
+import net.pterodactylus.sone.core.SoneProvider;
import net.pterodactylus.sone.data.PostBuilder;
import net.pterodactylus.sone.data.PostBuilderFactory;
*/
public class DefaultPostBuilderFactory implements PostBuilderFactory {
+ /** The Sone provider. */
+ private final SoneProvider soneProvider;
+
+ /**
+ * Creates a new default post builder factory.
+ *
+ * @param soneProvider
+ * The Sone provider
+ */
+ public DefaultPostBuilderFactory(SoneProvider soneProvider) {
+ this.soneProvider = soneProvider;
+ }
+
/**
* {@inheritDoc}
*/
@Override
public PostBuilder newPostBuilder() {
- return new PostBuilderImpl();
+ return new PostBuilderImpl(soneProvider);
}
}
import java.util.UUID;
+import net.pterodactylus.sone.core.SoneProvider;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.PostBuilder;
-import net.pterodactylus.sone.data.Sone;
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;
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;
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}
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;
}
* {@inheritDoc}
*/
@Override
- public PostBuilder from(Sone sender) {
- this.sender = sender;
+ public PostBuilder from(String senderId) {
+ this.senderId = senderId;
return this;
}
* {@inheritDoc}
*/
@Override
- public PostBuilder to(Sone recipient) {
- this.recipient = recipient;
+ public PostBuilder to(String recipientId) {
+ this.recipientId = recipientId;
return this;
}
@Override
public Post build() {
checkState((randomId && (id == null)) || (!randomId && (id != null)), "exactly one of random ID or custom ID must be set");
- checkState(sender != null, "sender must not be null");
+ 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, recipient, currentTime ? System.currentTimeMillis() : time, text);
+ 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);
}
}
import java.util.UUID;
+import net.pterodactylus.sone.core.SoneProvider;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Sone;
*/
public class PostImpl implements Post {
+ /** The Sone provider. */
+ private final SoneProvider soneProvider;
+
/** The GUID of the post. */
private final UUID id;
- /** The Sone this post belongs to. */
- private final Sone sone;
+ /** The ID of the owning Sone. */
+ private final String soneId;
- /** The Sone of the recipient. */
- private final Sone recipient;
+ /** The ID of the recipient Sone. */
+ private final String recipientId;
/** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
private final long time;
/**
* Creates a new post.
*
+ * @param soneProvider
+ * The Sone provider
* @param id
* The ID of the post
- * @param sone
- * The Sone this post belongs to
- * @param recipient
- * The recipient of the post
+ * @param soneId
+ * The ID of the Sone this post belongs to
+ * @param recipientId
+ * The ID of the recipient of the post
* @param time
* The time of the post (in milliseconds since Jan 1, 1970 UTC)
* @param text
* The text of the post
*/
- public PostImpl(String id, Sone sone, Sone recipient, long time, String text) {
+ public PostImpl(SoneProvider soneProvider, String id, String soneId, String recipientId, long time, String text) {
+ this.soneProvider = soneProvider;
this.id = UUID.fromString(id);
- this.sone = sone;
- this.recipient = recipient;
+ this.soneId = soneId;
+ this.recipientId = recipientId;
this.time = time;
this.text = text;
}
*/
@Override
public Sone getSone() {
- return sone;
+ return soneProvider.getSone(soneId, false);
}
/**
*/
@Override
public Sone getRecipient() {
- return recipient;
+ return soneProvider.getSone(recipientId, false);
}
/**
*/
@Override
public String toString() {
- return getClass().getName() + "[id=" + id + ",sone=" + sone + ",time=" + time + ",text=" + text + "]";
+ return String.format("%s[id=%s,sone=%s,recipient=%s,time=%d,text=%s]", getClass().getName(), id, soneId, recipientId, time, text);
}
}