From 8d5dcab8d96af52241aaf425440680806c5e20d3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 15 Oct 2013 22:49:14 +0200 Subject: [PATCH] Rename post and reply implementations; use builder to create replies. --- .../java/net/pterodactylus/sone/core/Core.java | 76 +++------ .../pterodactylus/sone/core/SoneDownloader.java | 6 +- .../java/net/pterodactylus/sone/data/Sone.java | 3 + .../sone/data/impl/AbstractPostReplyBuilder.java | 28 +--- .../sone/data/impl/AbstractReplyBuilder.java | 34 ++-- .../pterodactylus/sone/data/impl/DefaultPost.java | 182 +++++++++++++++++++++ .../sone/data/impl/DefaultPostBuilder.java | 4 +- .../sone/data/impl/DefaultPostReply.java | 72 ++++++++ .../sone/data/impl/DefaultPostReplyBuilder.java | 55 +++++++ .../data/impl/DefaultPostReplyBuilderFactory.java | 48 ------ .../pterodactylus/sone/data/impl/DefaultReply.java | 135 +++++++++++++++ .../pterodactylus/sone/data/impl/DefaultSone.java | 13 ++ .../net/pterodactylus/sone/data/impl/PostImpl.java | 182 --------------------- .../sone/data/impl/PostReplyBuilderImpl.java | 49 ------ .../sone/data/impl/PostReplyImpl.java | 72 -------- .../pterodactylus/sone/data/impl/ReplyImpl.java | 135 --------------- .../sone/database/PostReplyBuilder.java | 20 +-- .../sone/database/PostReplyDatabase.java | 2 +- .../pterodactylus/sone/database/ReplyBuilder.java | 10 -- .../sone/database/memory/MemoryDatabase.java | 10 -- .../pterodactylus/sone/fcp/CreateReplyCommand.java | 2 +- .../net/pterodactylus/sone/main/SonePlugin.java | 3 - .../pterodactylus/sone/web/CreateReplyPage.java | 2 +- .../sone/web/ajax/CreateReplyAjaxPage.java | 2 +- 24 files changed, 527 insertions(+), 618 deletions(-) create mode 100644 src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java create mode 100644 src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReply.java create mode 100644 src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReplyBuilder.java delete mode 100644 src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReplyBuilderFactory.java create mode 100644 src/main/java/net/pterodactylus/sone/data/impl/DefaultReply.java delete mode 100644 src/main/java/net/pterodactylus/sone/data/impl/PostImpl.java delete mode 100644 src/main/java/net/pterodactylus/sone/data/impl/PostReplyBuilderImpl.java delete mode 100644 src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java delete mode 100644 src/main/java/net/pterodactylus/sone/data/impl/ReplyImpl.java diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 48afb36..7f74e14 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -73,6 +73,7 @@ import net.pterodactylus.sone.database.PostBuilder; 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; @@ -466,15 +467,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, 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 getPostReply(String replyId) { @@ -1087,8 +1079,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, 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.absent())); } /* load post likes. */ @@ -1298,44 +1290,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } /** - * 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 @@ -1970,4 +1924,28 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, }); } + public Optional postReplyCreated() { + return Optional.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); + } + } + }); + } + } diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java index ed8ad98..a4e73c7 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -40,6 +40,7 @@ import net.pterodactylus.sone.data.impl.DefaultSone; 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; @@ -405,10 +406,9 @@ public class SoneDownloader extends AbstractService { 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.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)); diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index f1d9ffe..4ff9643 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -31,6 +31,7 @@ import net.pterodactylus.sone.core.Options; 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; @@ -553,4 +554,6 @@ public interface Sone extends Identified, Fingerprintable, PostBuilderFactory, C PostBuilder newPostBuilder(); + PostReplyBuilder newPostReplyBuilder(String postId) throws IllegalStateException; + } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/AbstractPostReplyBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/AbstractPostReplyBuilder.java index 05e5ccc..c8bca7c 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/AbstractPostReplyBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/AbstractPostReplyBuilder.java @@ -17,6 +17,7 @@ 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; @@ -32,32 +33,11 @@ import net.pterodactylus.sone.database.PostReplyBuilder; */ public abstract class AbstractPostReplyBuilder extends AbstractReplyBuilder 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"); } } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/AbstractReplyBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/AbstractReplyBuilder.java index 5c1b976..389eb85 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/AbstractReplyBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/AbstractReplyBuilder.java @@ -20,12 +20,14 @@ 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.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}. @@ -36,24 +38,13 @@ import com.google.common.base.Optional; */ public class AbstractReplyBuilder> implements ReplyBuilder { + protected final String senderId; protected Optional id = absent(); - - /** The sender of the reply. */ - protected String senderId; - protected Optional 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; } /** @@ -61,8 +52,8 @@ public class AbstractReplyBuilder> implements ReplyBui */ @Override @SuppressWarnings("unchecked") - public B from(String senderId) { - this.senderId = senderId; + public B withId(String id) { + this.id = fromNullable(id); return (B) this; } @@ -94,4 +85,15 @@ public class AbstractReplyBuilder> implements ReplyBui 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"); + } + } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java new file mode 100644 index 0000000..c4dcb62 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java @@ -0,0 +1,182 @@ +/* + * 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 . + */ + +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 David ‘Bombe’ Roden + */ +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 getRecipientId() { + return Optional.fromNullable(recipientId); + } + + /** + * {@inheritDoc} + */ + @Override + public Optional 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); + } + +} diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilder.java index e73e293..0d4c71c 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilder.java @@ -24,7 +24,7 @@ import net.pterodactylus.sone.database.PostBuilder; import com.google.common.base.Optional; /** - * {@link PostBuilder} implementation that creates {@link PostImpl} objects. + * {@link PostBuilder} implementation that creates {@link DefaultPost} objects. * * @author David ‘Bombe’ Roden */ @@ -43,7 +43,7 @@ public class DefaultPostBuilder extends AbstractPostBuilder { @Override public Post build(Optional 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); } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReply.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReply.java new file mode 100644 index 0000000..6c08106 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReply.java @@ -0,0 +1,72 @@ +/* + * 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 . + */ + +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 David ‘Bombe’ Roden + */ +public class DefaultPostReply extends DefaultReply 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 getPost() { + return database.getPost(postId); + } + +} diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReplyBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReplyBuilder.java new file mode 100644 index 0000000..8213faa --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReplyBuilder.java @@ -0,0 +1,55 @@ +/* + * 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 . + */ + +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 David ‘Bombe’ Roden + */ +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) { + validate(); + + DefaultPostReply postReply = new DefaultPostReply(database, getId(), senderId, getTime(), text, postId); + if (postReplyCreated.isPresent()) { + postReplyCreated.get().postReplyCreated(postReply); + } + return postReply; + } + +} diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReplyBuilderFactory.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReplyBuilderFactory.java deleted file mode 100644 index 2c303a1..0000000 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReplyBuilderFactory.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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 . - */ - -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 David ‘Bombe’ Roden - */ -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); - } - -} diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultReply.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultReply.java new file mode 100644 index 0000000..13bd5bc --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultReply.java @@ -0,0 +1,135 @@ +/* + * 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 . + */ + +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 + * The type of the reply + * @author David ‘Bombe’ Roden + */ +public abstract class DefaultReply> implements Reply { + + 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); + } + +} diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java index 6f77fb4..cc03d7a 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java @@ -39,6 +39,7 @@ import net.pterodactylus.sone.data.Sone; 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; @@ -689,6 +690,18 @@ public class DefaultSone implements Sone { }; } + @Override + public PostReplyBuilder newPostReplyBuilder(String postId) throws IllegalStateException { + return new DefaultPostReplyBuilder(database, getId(), postId) { + @Override + public PostReply build(Optional postReplyCreated) { + PostReply postReply = super.build(postReplyCreated); + database.storePostReply(postReply); + return postReply; + } + }; + } + // // FINGERPRINTABLE METHODS // diff --git a/src/main/java/net/pterodactylus/sone/data/impl/PostImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/PostImpl.java deleted file mode 100644 index f0f9d35..0000000 --- a/src/main/java/net/pterodactylus/sone/data/impl/PostImpl.java +++ /dev/null @@ -1,182 +0,0 @@ -/* - * 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 . - */ - -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 David ‘Bombe’ Roden - */ -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 getRecipientId() { - return Optional.fromNullable(recipientId); - } - - /** - * {@inheritDoc} - */ - @Override - public Optional 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); - } - -} diff --git a/src/main/java/net/pterodactylus/sone/data/impl/PostReplyBuilderImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/PostReplyBuilderImpl.java deleted file mode 100644 index 5c7f494..0000000 --- a/src/main/java/net/pterodactylus/sone/data/impl/PostReplyBuilderImpl.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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 . - */ - -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 David ‘Bombe’ Roden - */ -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); - } - -} diff --git a/src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java deleted file mode 100644 index c1a2423..0000000 --- a/src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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 . - */ - -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 David ‘Bombe’ Roden - */ -public class PostReplyImpl extends ReplyImpl 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 getPost() { - return database.getPost(postId); - } - -} diff --git a/src/main/java/net/pterodactylus/sone/data/impl/ReplyImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/ReplyImpl.java deleted file mode 100644 index fae5436..0000000 --- a/src/main/java/net/pterodactylus/sone/data/impl/ReplyImpl.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * 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 . - */ - -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 - * The type of the reply - * @author David ‘Bombe’ Roden - */ -public abstract class ReplyImpl> implements Reply { - - 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); - } - -} diff --git a/src/main/java/net/pterodactylus/sone/database/PostReplyBuilder.java b/src/main/java/net/pterodactylus/sone/database/PostReplyBuilder.java index c031443..2dc922a 100644 --- a/src/main/java/net/pterodactylus/sone/database/PostReplyBuilder.java +++ b/src/main/java/net/pterodactylus/sone/database/PostReplyBuilder.java @@ -19,6 +19,8 @@ package net.pterodactylus.sone.database; import net.pterodactylus.sone.data.PostReply; +import com.google.common.base.Optional; + /** * Builder for a {@link PostReply} object. * @@ -27,16 +29,6 @@ import net.pterodactylus.sone.data.PostReply; public interface PostReplyBuilder extends ReplyBuilder { /** - * 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. *

* The following conditions must be met in order for the configuration to be @@ -56,6 +48,12 @@ public interface PostReplyBuilder extends ReplyBuilder { * @throws IllegalStateException * if this builder’s configuration is not valid */ - public PostReply build() throws IllegalStateException; + public PostReply build(Optional postReplyCreated) throws IllegalStateException; + + interface PostReplyCreated { + + void postReplyCreated(PostReply postReply); + + } } diff --git a/src/main/java/net/pterodactylus/sone/database/PostReplyDatabase.java b/src/main/java/net/pterodactylus/sone/database/PostReplyDatabase.java index c9a809f..c1e5391 100644 --- a/src/main/java/net/pterodactylus/sone/database/PostReplyDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/PostReplyDatabase.java @@ -23,7 +23,7 @@ package net.pterodactylus.sone.database; * * @author David ‘Bombe’ Roden */ -public interface PostReplyDatabase extends PostReplyProvider, PostReplyBuilderFactory, PostReplyStore { +public interface PostReplyDatabase extends PostReplyProvider, PostReplyStore { /* nothing here. */ diff --git a/src/main/java/net/pterodactylus/sone/database/ReplyBuilder.java b/src/main/java/net/pterodactylus/sone/database/ReplyBuilder.java index a04a09c..8582c83 100644 --- a/src/main/java/net/pterodactylus/sone/database/ReplyBuilder.java +++ b/src/main/java/net/pterodactylus/sone/database/ReplyBuilder.java @@ -41,16 +41,6 @@ public interface ReplyBuilder> { 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. * diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java index d1ceee9..00443e4 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -362,16 +362,6 @@ public class MemoryDatabase extends AbstractService implements Database { } // - // POSTREPLYBUILDERFACTORY METHODS - // - - /** {@inheritDocs} */ - @Override - public PostReplyBuilder newPostReplyBuilder() { - return new MemoryPostReplyBuilder(this, this); - } - - // // POSTREPLYSTORE METHODS // diff --git a/src/main/java/net/pterodactylus/sone/fcp/CreateReplyCommand.java b/src/main/java/net/pterodactylus/sone/fcp/CreateReplyCommand.java index fe4bee8..2de9bef 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/CreateReplyCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/CreateReplyCommand.java @@ -53,7 +53,7 @@ public class CreateReplyCommand extends AbstractSoneCommand { 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()); } diff --git a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java index 4813028..8e24e3c 100644 --- a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java +++ b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java @@ -26,9 +26,7 @@ import net.pterodactylus.sone.core.Core; 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; @@ -226,7 +224,6 @@ public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, Fr 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() { diff --git a/src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java b/src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java index 84231aa..ef83c71 100644 --- a/src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java +++ b/src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java @@ -73,7 +73,7 @@ public class CreateReplyPage extends SoneTemplatePage { 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); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java index ab5bca3..441caaa 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java @@ -66,7 +66,7 @@ public class CreateReplyAjaxPage extends JsonPage { 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()); } -- 2.7.4