import net.pterodactylus.sone.database.PostBuilder.PostCreated;
import net.pterodactylus.sone.database.PostProvider;
import net.pterodactylus.sone.database.PostReplyBuilder;
+import net.pterodactylus.sone.database.PostReplyBuilder.PostReplyCreated;
import net.pterodactylus.sone.database.PostReplyProvider;
import net.pterodactylus.sone.database.SoneProvider;
import net.pterodactylus.sone.fcp.FcpInterface;
return database.getDirectedPosts(recipientId);
}
- /**
- * Returns a post reply builder.
- *
- * @return A new post reply builder
- */
- public PostReplyBuilder postReplyBuilder() {
- return database.newPostReplyBuilder();
- }
-
/** {@inheritDoc} */
@Override
public Optional<PostReply> getPostReply(String replyId) {
logger.log(Level.WARNING, "Invalid reply found, aborting load!");
return;
}
- PostReplyBuilder postReplyBuilder = postReplyBuilder().withId(replyId).from(sone.getId()).to(postId).withTime(replyTime).withText(replyText);
- replies.add(postReplyBuilder.build());
+ PostReplyBuilder postReplyBuilder = sone.newPostReplyBuilder(postId).withId(replyId).withTime(replyTime).withText(replyText);
+ replies.add(postReplyBuilder.build(Optional.<PostReplyCreated>absent()));
}
/* load post likes. */
}
/**
- * Creates a new reply.
- *
- * @param sone
- * The Sone that creates the reply
- * @param post
- * The post that this reply refers to
- * @param text
- * The text of the reply
- * @return The created reply
- */
- public PostReply createReply(Sone sone, Post post, String text) {
- checkNotNull(text, "text must not be null");
- checkArgument(text.trim().length() > 0, "text must not be empty");
- if (!sone.isLocal()) {
- logger.log(Level.FINE, String.format("Tried to create reply for non-local Sone: %s", sone));
- return null;
- }
- PostReplyBuilder postReplyBuilder = postReplyBuilder();
- postReplyBuilder.from(sone.getId()).to(post.getId()).withText(text.trim());
- final PostReply reply = postReplyBuilder.build();
- database.storePostReply(reply);
- eventBus.post(new NewPostReplyFoundEvent(reply));
- sone.addReply(reply);
- touchConfiguration();
- localElementTicker.schedule(new Runnable() {
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void run() {
- markReplyKnown(reply);
- }
- }, 10, TimeUnit.SECONDS);
- return reply;
- }
-
- /**
* Deletes the given reply.
*
* @param reply
});
}
+ public Optional<PostReplyCreated> postReplyCreated() {
+ return Optional.<PostReplyCreated>of(new PostReplyCreated() {
+ @Override
+ public void postReplyCreated(final PostReply postReply) {
+ if (postReply.isKnown()) {
+ return;
+ }
+ eventBus.post(new NewPostReplyFoundEvent(postReply));
+ if (postReply.getSone().isLocal()) {
+ localElementTicker.schedule(new Runnable() {
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void run() {
+ markReplyKnown(postReply);
+ }
+ }, 10, TimeUnit.SECONDS);
+ }
+ }
+ });
+ }
+
}
import net.pterodactylus.sone.database.PostBuilder;
import net.pterodactylus.sone.database.PostBuilder.PostCreated;
import net.pterodactylus.sone.database.PostReplyBuilder;
+import net.pterodactylus.sone.database.PostReplyBuilder.PostReplyCreated;
import net.pterodactylus.util.io.Closer;
import net.pterodactylus.util.logging.Logging;
import net.pterodactylus.util.number.Numbers;
return null;
}
try {
- PostReplyBuilder postReplyBuilder = core.postReplyBuilder();
/* TODO - parse time correctly. */
- postReplyBuilder.withId(replyId).from(sone.getId()).to(replyPostId).withTime(Long.parseLong(replyTime)).withText(replyText);
- replies.add(postReplyBuilder.build());
+ PostReplyBuilder postReplyBuilder = sone.newPostReplyBuilder(replyPostId).withId(replyId).withTime(Long.parseLong(replyTime)).withText(replyText);
+ replies.add(postReplyBuilder.build(Optional.<PostReplyCreated>absent()));
} catch (NumberFormatException nfe1) {
/* TODO - mark Sone as bad. */
logger.log(Level.WARNING, String.format("Downloaded reply for Sone %s with invalid time: %s", sone, replyTime));
import net.pterodactylus.sone.database.AlbumBuilder;
import net.pterodactylus.sone.database.PostBuilder;
import net.pterodactylus.sone.database.PostBuilderFactory;
+import net.pterodactylus.sone.database.PostReplyBuilder;
import net.pterodactylus.sone.freenet.wot.Identity;
import net.pterodactylus.sone.freenet.wot.OwnIdentity;
import net.pterodactylus.sone.template.SoneAccessor;
PostBuilder newPostBuilder();
+ PostReplyBuilder newPostReplyBuilder(String postId) throws IllegalStateException;
+
}
package net.pterodactylus.sone.data.impl;
+import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import org.apache.commons.lang.StringUtils;
*/
public abstract class AbstractPostReplyBuilder extends AbstractReplyBuilder<PostReplyBuilder> implements PostReplyBuilder {
- /** The ID of the post the created reply refers to. */
protected String postId;
- /**
- * {@inheritDoc}
- */
- @Override
- public PostReplyBuilder to(String postId) {
- this.postId = postId;
- return this;
- }
-
- //
- // PROTECTED METHODS
- //
-
- /**
- * Validates the state of this post reply builder.
- *
- * @throws IllegalStateException
- * if the state is not valid for building a new post reply
- */
- protected void validate() throws IllegalStateException {
- checkState(senderId != null, "sender must not be null");
- checkState(!StringUtils.isBlank(text), "text must not be empty");
- checkState(postId != null, "post must not be null");
+ protected AbstractPostReplyBuilder(String senderId, String postId) {
+ super(senderId);
+ this.postId = checkNotNull(postId, "post ID must not be null");
}
}
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.checkState;
import static java.lang.System.currentTimeMillis;
import static java.util.UUID.randomUUID;
import net.pterodactylus.sone.database.ReplyBuilder;
import com.google.common.base.Optional;
+import org.apache.commons.lang.StringUtils;
/**
* Abstract implementation of a {@link ReplyBuilder}.
*/
public class AbstractReplyBuilder<B extends ReplyBuilder<B>> implements ReplyBuilder<B> {
+ protected final String senderId;
protected Optional<String> id = absent();
-
- /** The sender of the reply. */
- protected String senderId;
-
protected Optional<Long> time = absent();
-
- /** The text of the reply. */
protected String text;
- /**
- * {@inheritDoc}
- */
- @Override
- @SuppressWarnings("unchecked")
- public B withId(String id) {
- this.id = fromNullable(id);
- return (B) this;
+ protected AbstractReplyBuilder(String senderId) {
+ this.senderId = senderId;
}
/**
*/
@Override
@SuppressWarnings("unchecked")
- public B from(String senderId) {
- this.senderId = senderId;
+ public B withId(String id) {
+ this.id = fromNullable(id);
return (B) this;
}
return time.isPresent() ? time.get() : currentTimeMillis();
}
+ /**
+ * Validates the state of this post reply builder.
+ *
+ * @throws IllegalStateException
+ * if the state is not valid for building a new post reply
+ */
+ protected void validate() throws IllegalStateException {
+ checkState(senderId != null, "sender must not be null");
+ checkState(!StringUtils.isBlank(text), "text must not be empty");
+ }
+
}
--- /dev/null
+/*
+ * Sone - PostImpl.java - Copyright © 2010–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 <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.data.impl;
+
+import java.util.UUID;
+
+import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.database.Database;
+
+import com.google.common.base.Optional;
+
+/**
+ * A post is a short message that a user writes in his Sone to let other users
+ * know what is going on.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class DefaultPost implements Post {
+
+ private final Database database;
+
+ /** The GUID of the post. */
+ private final UUID id;
+
+ /** The ID of the owning Sone. */
+ private final String soneId;
+
+ /** 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;
+
+ /** The text of the post. */
+ private final String text;
+
+ /** Whether the post is known. */
+ private volatile boolean known;
+
+ /**
+ * Creates a new post.
+ *
+ * @param database
+ * The database
+ * @param id
+ * The ID 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 DefaultPost(Database database, String id, String soneId, String recipientId, long time, String text) {
+ this.database = database;
+ this.id = UUID.fromString(id);
+ this.soneId = soneId;
+ this.recipientId = recipientId;
+ this.time = time;
+ this.text = text;
+ }
+
+ //
+ // ACCESSORS
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String getId() {
+ return id.toString();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Sone getSone() {
+ return database.getSone(soneId).get();
+ }
+
+ /**
+ * {@inheritDocs}
+ */
+ @Override
+ public Optional<String> getRecipientId() {
+ return Optional.fromNullable(recipientId);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Optional<Sone> getRecipient() {
+ return database.getSone(recipientId);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public long getTime() {
+ return time;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String getText() {
+ return text;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean isKnown() {
+ return known;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public DefaultPost setKnown(boolean known) {
+ this.known = known;
+ return this;
+ }
+
+ //
+ // OBJECT METHODS
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode() {
+ return id.hashCode();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean equals(Object object) {
+ if (!(object instanceof DefaultPost)) {
+ return false;
+ }
+ DefaultPost post = (DefaultPost) object;
+ return post.id.equals(id);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String toString() {
+ return String.format("%s[id=%s,sone=%s,recipient=%s,time=%d,text=%s]", getClass().getName(), id, soneId, recipientId, time, text);
+ }
+
+}
import com.google.common.base.Optional;
/**
- * {@link PostBuilder} implementation that creates {@link PostImpl} objects.
+ * {@link PostBuilder} implementation that creates {@link DefaultPost} objects.
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
@Override
public Post build(Optional<PostCreated> postCreated) {
validate();
- PostImpl post = new PostImpl(database, getId(), senderId, recipientId.orNull(), getTime(), text);
+ DefaultPost post = new DefaultPost(database, getId(), senderId, recipientId.orNull(), getTime(), text);
if (postCreated.isPresent()) {
postCreated.get().postCreated(post);
}
--- /dev/null
+/*
+ * Sone - PostReplyImpl.java - Copyright © 2010–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 <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.data.impl;
+
+import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.PostReply;
+import net.pterodactylus.sone.database.Database;
+
+import com.google.common.base.Optional;
+
+/**
+ * Simple {@link PostReply} implementation.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class DefaultPostReply extends DefaultReply<PostReply> implements PostReply {
+
+ /** The Post this reply refers to. */
+ private final String postId;
+
+ /**
+ * Creates a new reply.
+ *
+ * @param database
+ * The database
+ * @param id
+ * The ID of the reply
+ * @param soneId
+ * The ID of the Sone of the reply
+ * @param time
+ * The time of the reply
+ * @param text
+ * The text of the reply
+ * @param postId
+ */
+ public DefaultPostReply(Database database, String id, String soneId, long time, String text, String postId) {
+ super(database, id, soneId, time, text);
+ this.postId = postId;
+ }
+
+ //
+ // ACCESSORS
+ //
+
+ /** {@inheritDocs} */
+ @Override
+ public String getPostId() {
+ return postId;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public Optional<Post> getPost() {
+ return database.getPost(postId);
+ }
+
+}
--- /dev/null
+/*
+ * Sone - PostReplyBuilderImpl.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 <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.data.impl;
+
+import net.pterodactylus.sone.data.PostReply;
+import net.pterodactylus.sone.database.Database;
+import net.pterodactylus.sone.database.PostReplyBuilder;
+
+import com.google.common.base.Optional;
+
+/**
+ * {@link PostReplyBuilder} implementation that creates {@link DefaultPostReply}
+ * objects.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class DefaultPostReplyBuilder extends AbstractPostReplyBuilder {
+
+ private final Database database;
+
+ public DefaultPostReplyBuilder(Database database, String senderId, String postId) {
+ super(senderId, postId);
+ this.database = database;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public PostReply build(Optional<PostReplyCreated> postReplyCreated) {
+ validate();
+
+ DefaultPostReply postReply = new DefaultPostReply(database, getId(), senderId, getTime(), text, postId);
+ if (postReplyCreated.isPresent()) {
+ postReplyCreated.get().postReplyCreated(postReply);
+ }
+ return postReply;
+ }
+
+}
+++ /dev/null
-/*
- * Sone - DefaultPostReplyBuilderFactory.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 <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.data.impl;
-
-import net.pterodactylus.sone.database.Database;
-import net.pterodactylus.sone.database.PostReplyBuilder;
-import net.pterodactylus.sone.database.PostReplyBuilderFactory;
-
-import com.google.inject.Inject;
-
-/**
- * {@link PostReplyBuilderFactory} that creates {@link PostReplyBuilderImpl}s.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class DefaultPostReplyBuilderFactory implements PostReplyBuilderFactory {
-
- private final Database database;
-
- @Inject
- public DefaultPostReplyBuilderFactory(Database database) {
- this.database = database;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public PostReplyBuilder newPostReplyBuilder() {
- return new PostReplyBuilderImpl(database);
- }
-
-}
--- /dev/null
+/*
+ * Sone - ReplyImpl.java - Copyright © 2011–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 <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.data.impl;
+
+import net.pterodactylus.sone.data.Reply;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.database.Database;
+
+/**
+ * Abstract base class for all replies.
+ *
+ * @param <T>
+ * The type of the reply
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public abstract class DefaultReply<T extends Reply<T>> implements Reply<T> {
+
+ protected final Database database;
+
+ /** The ID of the reply. */
+ private final String id;
+
+ /** The Sone that created this reply. */
+ private final String soneId;
+
+ /** The time of the reply. */
+ private final long time;
+
+ /** The text of the reply. */
+ private final String text;
+
+ /** Whether the reply is known. */
+ private volatile boolean known;
+
+ /**
+ * Creates a new reply.
+ *
+ * @param database
+ * The database
+ * @param id
+ * The ID of the reply
+ * @param soneId
+ * The ID of the Sone of the reply
+ * @param time
+ * The time of the reply
+ * @param text
+ */
+ protected DefaultReply(Database database, String id, String soneId, long time, String text) {
+ this.database = database;
+ this.id = id;
+ this.soneId = soneId;
+ this.time = time;
+ this.text = text;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public String getId() {
+ return id;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public Sone getSone() {
+ return database.getSone(soneId).get();
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public long getTime() {
+ return time;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public String getText() {
+ return text;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public boolean isKnown() {
+ return known;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ @SuppressWarnings("unchecked")
+ public T setKnown(boolean known) {
+ this.known = known;
+ return (T) this;
+ }
+
+ //
+ // OBJECT METHODS
+ //
+
+ /** {@inheritDoc} */
+ @Override
+ public int hashCode() {
+ return id.hashCode();
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public boolean equals(Object object) {
+ if (!(object instanceof Reply<?>)) {
+ return false;
+ }
+ Reply<?> reply = (Reply<?>) object;
+ return reply.getId().equals(id);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public String toString() {
+ return String.format("%s[id=%s,sone=%s,time=%d,text=%s]", getClass().getName(), id, soneId, time, text);
+ }
+
+}
import net.pterodactylus.sone.database.AlbumBuilder;
import net.pterodactylus.sone.database.Database;
import net.pterodactylus.sone.database.PostBuilder;
+import net.pterodactylus.sone.database.PostReplyBuilder;
import net.pterodactylus.sone.freenet.wot.Identity;
import net.pterodactylus.util.logging.Logging;
};
}
+ @Override
+ public PostReplyBuilder newPostReplyBuilder(String postId) throws IllegalStateException {
+ return new DefaultPostReplyBuilder(database, getId(), postId) {
+ @Override
+ public PostReply build(Optional<PostReplyCreated> postReplyCreated) {
+ PostReply postReply = super.build(postReplyCreated);
+ database.storePostReply(postReply);
+ return postReply;
+ }
+ };
+ }
+
//
// FINGERPRINTABLE METHODS
//
+++ /dev/null
-/*
- * Sone - PostImpl.java - Copyright © 2010–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 <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.data.impl;
-
-import java.util.UUID;
-
-import net.pterodactylus.sone.data.Post;
-import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.database.Database;
-
-import com.google.common.base.Optional;
-
-/**
- * A post is a short message that a user writes in his Sone to let other users
- * know what is going on.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class PostImpl implements Post {
-
- private final Database database;
-
- /** The GUID of the post. */
- private final UUID id;
-
- /** The ID of the owning Sone. */
- private final String soneId;
-
- /** 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;
-
- /** The text of the post. */
- private final String text;
-
- /** Whether the post is known. */
- private volatile boolean known;
-
- /**
- * Creates a new post.
- *
- * @param database
- * The database
- * @param id
- * The ID 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(Database database, String id, String soneId, String recipientId, long time, String text) {
- this.database = database;
- this.id = UUID.fromString(id);
- this.soneId = soneId;
- this.recipientId = recipientId;
- this.time = time;
- this.text = text;
- }
-
- //
- // ACCESSORS
- //
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String getId() {
- return id.toString();
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Sone getSone() {
- return database.getSone(soneId).get();
- }
-
- /**
- * {@inheritDocs}
- */
- @Override
- public Optional<String> getRecipientId() {
- return Optional.fromNullable(recipientId);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Optional<Sone> getRecipient() {
- return database.getSone(recipientId);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public long getTime() {
- return time;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String getText() {
- return text;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean isKnown() {
- return known;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public PostImpl setKnown(boolean known) {
- this.known = known;
- return this;
- }
-
- //
- // OBJECT METHODS
- //
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int hashCode() {
- return id.hashCode();
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean equals(Object object) {
- if (!(object instanceof PostImpl)) {
- return false;
- }
- PostImpl post = (PostImpl) object;
- return post.id.equals(id);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String toString() {
- return String.format("%s[id=%s,sone=%s,recipient=%s,time=%d,text=%s]", getClass().getName(), id, soneId, recipientId, time, text);
- }
-
-}
+++ /dev/null
-/*
- * Sone - PostReplyBuilderImpl.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 <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.data.impl;
-
-import net.pterodactylus.sone.data.PostReply;
-import net.pterodactylus.sone.database.Database;
-import net.pterodactylus.sone.database.PostReplyBuilder;
-
-/**
- * {@link PostReplyBuilder} implementation that creates {@link PostReplyImpl}
- * objects.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class PostReplyBuilderImpl extends AbstractPostReplyBuilder {
-
- private final Database database;
-
- public PostReplyBuilderImpl(Database database) {
- this.database = database;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public PostReply build() {
- validate();
-
- /* create new post reply. */
- return new PostReplyImpl(database, getId(), senderId, getTime(), text, postId);
- }
-
-}
+++ /dev/null
-/*
- * Sone - PostReplyImpl.java - Copyright © 2010–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 <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.data.impl;
-
-import net.pterodactylus.sone.data.Post;
-import net.pterodactylus.sone.data.PostReply;
-import net.pterodactylus.sone.database.Database;
-
-import com.google.common.base.Optional;
-
-/**
- * Simple {@link PostReply} implementation.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class PostReplyImpl extends ReplyImpl<PostReply> implements PostReply {
-
- /** The Post this reply refers to. */
- private final String postId;
-
- /**
- * Creates a new reply.
- *
- * @param database
- * The database
- * @param id
- * The ID of the reply
- * @param soneId
- * The ID of the Sone of the reply
- * @param time
- * The time of the reply
- * @param text
- * The text of the reply
- * @param postId
- */
- public PostReplyImpl(Database database, String id, String soneId, long time, String text, String postId) {
- super(database, id, soneId, time, text);
- this.postId = postId;
- }
-
- //
- // ACCESSORS
- //
-
- /** {@inheritDocs} */
- @Override
- public String getPostId() {
- return postId;
- }
-
- /** {@inheritDoc} */
- @Override
- public Optional<Post> getPost() {
- return database.getPost(postId);
- }
-
-}
+++ /dev/null
-/*
- * Sone - ReplyImpl.java - Copyright © 2011–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 <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.data.impl;
-
-import net.pterodactylus.sone.data.Reply;
-import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.database.Database;
-
-/**
- * Abstract base class for all replies.
- *
- * @param <T>
- * The type of the reply
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public abstract class ReplyImpl<T extends Reply<T>> implements Reply<T> {
-
- protected final Database database;
-
- /** The ID of the reply. */
- private final String id;
-
- /** The Sone that created this reply. */
- private final String soneId;
-
- /** The time of the reply. */
- private final long time;
-
- /** The text of the reply. */
- private final String text;
-
- /** Whether the reply is known. */
- private volatile boolean known;
-
- /**
- * Creates a new reply.
- *
- * @param database
- * The database
- * @param id
- * The ID of the reply
- * @param soneId
- * The ID of the Sone of the reply
- * @param time
- * The time of the reply
- * @param text
- */
- protected ReplyImpl(Database database, String id, String soneId, long time, String text) {
- this.database = database;
- this.id = id;
- this.soneId = soneId;
- this.time = time;
- this.text = text;
- }
-
- /** {@inheritDoc} */
- @Override
- public String getId() {
- return id;
- }
-
- /** {@inheritDoc} */
- @Override
- public Sone getSone() {
- return database.getSone(soneId).get();
- }
-
- /** {@inheritDoc} */
- @Override
- public long getTime() {
- return time;
- }
-
- /** {@inheritDoc} */
- @Override
- public String getText() {
- return text;
- }
-
- /** {@inheritDoc} */
- @Override
- public boolean isKnown() {
- return known;
- }
-
- /** {@inheritDoc} */
- @Override
- @SuppressWarnings("unchecked")
- public T setKnown(boolean known) {
- this.known = known;
- return (T) this;
- }
-
- //
- // OBJECT METHODS
- //
-
- /** {@inheritDoc} */
- @Override
- public int hashCode() {
- return id.hashCode();
- }
-
- /** {@inheritDoc} */
- @Override
- public boolean equals(Object object) {
- if (!(object instanceof Reply<?>)) {
- return false;
- }
- Reply<?> reply = (Reply<?>) object;
- return reply.getId().equals(id);
- }
-
- /** {@inheritDoc} */
- @Override
- public String toString() {
- return String.format("%s[id=%s,sone=%s,time=%d,text=%s]", getClass().getName(), id, soneId, time, text);
- }
-
-}
import net.pterodactylus.sone.data.PostReply;
+import com.google.common.base.Optional;
+
/**
* Builder for a {@link PostReply} object.
*
public interface PostReplyBuilder extends ReplyBuilder<PostReplyBuilder> {
/**
- * Configures this builder to set the given post as post the created reply
- * refers to.
- *
- * @param postId
- * The ID of the post the reply refers to
- * @return This builder
- */
- public PostReplyBuilder to(String postId);
-
- /**
* Verifies the configuration of this builder and creates a new post reply.
* <p>
* The following conditions must be met in order for the configuration to be
* @throws IllegalStateException
* if this builder’s configuration is not valid
*/
- public PostReply build() throws IllegalStateException;
+ public PostReply build(Optional<PostReplyCreated> postReplyCreated) throws IllegalStateException;
+
+ interface PostReplyCreated {
+
+ void postReplyCreated(PostReply postReply);
+
+ }
}
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public interface PostReplyDatabase extends PostReplyProvider, PostReplyBuilderFactory, PostReplyStore {
+public interface PostReplyDatabase extends PostReplyProvider, PostReplyStore {
/* nothing here. */
public B withId(String id);
/**
- * Configures this builder to use the ID of the given {@link Sone} as sender
- * of the reply.
- *
- * @param senderId
- * The ID of the sender of the reply
- * @return This builder
- */
- public B from(String senderId);
-
- /**
* Configures this builder to use the given time when creating the reply. If
* this method is used, {@link #currentTime()} must not be used.
*
}
//
- // POSTREPLYBUILDERFACTORY METHODS
- //
-
- /** {@inheritDocs} */
- @Override
- public PostReplyBuilder newPostReplyBuilder() {
- return new MemoryPostReplyBuilder(this, this);
- }
-
- //
// POSTREPLYSTORE METHODS
//
Sone sone = getSone(parameters, "Sone", true);
Post post = getPost(parameters, "Post");
String text = getString(parameters, "Text");
- PostReply reply = getCore().createReply(sone, post, text);
+ PostReply reply = sone.newPostReplyBuilder(post.getId()).withText(text).build(getCore().postReplyCreated());
return new Response("ReplyCreated", new SimpleFieldSetBuilder().put("Reply", reply.getId()).get());
}
import net.pterodactylus.sone.core.FreenetInterface;
import net.pterodactylus.sone.core.WebOfTrustUpdater;
import net.pterodactylus.sone.database.Database;
-import net.pterodactylus.sone.database.PostBuilderFactory;
import net.pterodactylus.sone.database.PostProvider;
-import net.pterodactylus.sone.database.PostReplyBuilderFactory;
import net.pterodactylus.sone.database.SoneProvider;
import net.pterodactylus.sone.database.memory.MemoryDatabase;
import net.pterodactylus.sone.fcp.FcpInterface;
bind(SonePlugin.class).toInstance(SonePlugin.this);
bind(FcpInterface.class).in(Singleton.class);
bind(Database.class).to(MemoryDatabase.class);
- bind(PostReplyBuilderFactory.class).to(MemoryDatabase.class);
bind(SoneProvider.class).to(Core.class).in(Singleton.class);
bind(PostProvider.class).to(MemoryDatabase.class);
bindListener(Matchers.any(), new TypeListener() {
sender = of(getCurrentSone(request.getToadletContext()));
}
text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text);
- webInterface.getCore().createReply(sender.get(), post.get(), text);
+ sender.get().newPostReplyBuilder(post.get().getId()).withText(text).build(webInterface.getCore().postReplyCreated());
throw new RedirectException(returnPage);
}
templateContext.set("errorTextEmpty", true);
return createErrorJsonObject("invalid-post-id");
}
text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text);
- PostReply reply = webInterface.getCore().createReply(sender.get(), post.get(), text);
+ PostReply reply = sender.get().newPostReplyBuilder(post.get().getId()).withText(text).build(webInterface.getCore().postReplyCreated());
return createSuccessJsonObject().put("reply", reply.getId()).put("sone", sender.get().getId());
}