Merge commit 'fcabe38e9b3abacc0d580bf0513600858aee2eca' into less-critical
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 24 Jan 2013 05:10:32 +0000 (06:10 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 24 Jan 2013 05:10:32 +0000 (06:10 +0100)
Conflicts:
src/main/java/net/pterodactylus/sone/core/Core.java

1  2 
src/main/java/net/pterodactylus/sone/core/Core.java

@@@ -59,6 -59,8 +59,8 @@@ import net.pterodactylus.sone.data.Post
  import net.pterodactylus.sone.data.PostBuilder;
  import net.pterodactylus.sone.data.PostBuilderFactory;
  import net.pterodactylus.sone.data.PostReply;
+ import net.pterodactylus.sone.data.PostReplyBuilder;
+ import net.pterodactylus.sone.data.PostReplyBuilderFactory;
  import net.pterodactylus.sone.data.Profile;
  import net.pterodactylus.sone.data.Profile.Field;
  import net.pterodactylus.sone.data.Reply;
@@@ -66,7 -68,6 +68,6 @@@ import net.pterodactylus.sone.data.Sone
  import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
  import net.pterodactylus.sone.data.Sone.SoneStatus;
  import net.pterodactylus.sone.data.TemporaryImage;
- import net.pterodactylus.sone.data.impl.PostReplyImpl;
  import net.pterodactylus.sone.fcp.FcpInterface;
  import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
  import net.pterodactylus.sone.freenet.wot.Identity;
@@@ -86,12 -87,9 +87,12 @@@ import net.pterodactylus.util.number.Nu
  import net.pterodactylus.util.service.AbstractService;
  import net.pterodactylus.util.thread.NamedThreadFactory;
  
 +import com.google.common.base.Function;
  import com.google.common.base.Predicate;
  import com.google.common.base.Predicates;
  import com.google.common.collect.Collections2;
 +import com.google.common.collect.FluentIterable;
 +import com.google.common.collect.Ordering;
  import com.google.common.eventbus.EventBus;
  import com.google.common.eventbus.Subscribe;
  import com.google.inject.Inject;
@@@ -103,7 -101,7 +104,7 @@@ import freenet.keys.FreenetURI
   *
   * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
   */
- public class Core extends AbstractService implements SoneProvider, PostProvider {
+ public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider {
  
        /** The logger. */
        private static final Logger logger = Logging.getLogger(Core.class);
        /** All known posts. */
        private final Set<String> knownPosts = new HashSet<String>();
  
+       /** The post reply builder factory. */
+       private final PostReplyBuilderFactory postReplyBuilderFactory;
        /** All replies. */
        private final Map<String, PostReply> replies = new HashMap<String, PostReply>();
  
         *            The event bus
         * @param postBuilderFactory
         *            The post builder
+        * @param postReplyBuilderFactory
+        *            The post reply builder factory
         */
        @Inject
-       public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, PostBuilderFactory postBuilderFactory) {
+       public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, PostBuilderFactory postBuilderFactory, PostReplyBuilderFactory postReplyBuilderFactory) {
                super("Sone Core");
                this.configuration = configuration;
                this.freenetInterface = freenetInterface;
                this.webOfTrustUpdater = webOfTrustUpdater;
                this.eventBus = eventBus;
                this.postBuilderFactory = postBuilderFactory;
+               this.postReplyBuilderFactory = postReplyBuilderFactory;
        }
  
        //
        }
  
        /**
-        * Returns the reply with the given ID. If there is no reply with the given
-        * ID yet, a new one is created, unless {@code create} is false in which
-        * case {@code null} is returned.
+        * Returns a post reply builder.
         *
-        * @param replyId
-        *            The ID of the reply to get
-        * @param create
-        *            {@code true} to always return a {@link Reply}, {@code false}
-        *            to return {@code null} if no reply can be found
-        * @return The reply, or {@code null} if there is no such reply
+        * @return A new post reply builder
         */
-       public PostReply getPostReply(String replyId, boolean create) {
+       public PostReplyBuilder postReplyBuilder() {
+               return postReplyBuilderFactory.newPostReplyBuilder();
+       }
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public PostReply getPostReply(String replyId) {
                synchronized (replies) {
-                       PostReply reply = replies.get(replyId);
-                       if (create && (reply == null)) {
-                               reply = new PostReplyImpl(replyId);
-                               replies.put(replyId, reply);
-                       }
-                       return reply;
+                       return replies.get(replyId);
                }
        }
  
        /**
-        * Returns all replies for the given post, order ascending by time.
-        *
-        * @param post
-        *            The post to get all replies for
-        * @return All replies for the given post
+        * {@inheritDoc}
         */
 -      public List<PostReply> getReplies(Post post) {
 -              Set<Sone> sones = getSones();
 -              List<PostReply> replies = new ArrayList<PostReply>();
 -              for (Sone sone : sones) {
 -                      for (PostReply reply : sone.getReplies()) {
 -                              if (reply.getPost().equals(post)) {
 -                                      replies.add(reply);
 -                              }
+       @Override
 +      public List<PostReply> getReplies(final Post post) {
 +              return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(FluentIterable.from(getSones()).transformAndConcat(new Function<Sone, Iterable<PostReply>>() {
 +
 +                      @Override
 +                      public Iterable<PostReply> apply(Sone sone) {
 +                              return sone.getReplies();
                        }
 -              }
 -              Collections.sort(replies, Reply.TIME_COMPARATOR);
 -              return replies;
 +              }).filter(new Predicate<PostReply>() {
 +
 +                      @Override
 +                      public boolean apply(PostReply reply) {
 +                              return post.equals(reply.getPost());
 +                      }
 +              }));
        }
  
        /**
                synchronized (sones) {
                        final Sone sone = getRemoteSone(identity.getId(), true).setIdentity(identity);
                        boolean newSone = sone.getRequestUri() == null;
-                       sone.setRequestUri(getSoneUri(identity.getRequestUri()));
+                       sone.setRequestUri(SoneUri.create(identity.getRequestUri()));
                        sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
                        if (newSone) {
                                synchronized (knownSones) {
                                logger.log(Level.WARNING, "Invalid post found, aborting load!");
                                return;
                        }
 -                      PostBuilder postBuilder = postBuilderFactory.newPostBuilder().withId(postId).from(sone).withTime(postTime).withText(postText);
 +                      PostBuilder postBuilder = postBuilder().withId(postId).from(sone).withTime(postTime).withText(postText);
                        if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
                                postBuilder.to(getSone(postRecipientId));
                        }
                                logger.log(Level.WARNING, "Invalid reply found, aborting load!");
                                return;
                        }
-                       replies.add(getPostReply(replyId, true).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
+                       PostReplyBuilder postReplyBuilder = postReplyBuilderFactory.newPostReplyBuilder();
+                       postReplyBuilder.withId(replyId).from(sone).to(postId).withTime(replyTime).withText(replyText);
+                       replies.add(postReplyBuilder.build());
                }
  
                /* load post likes. */
         * @return The created reply
         */
        public PostReply createReply(Sone sone, Post post, String text) {
-               return createReply(sone, post, System.currentTimeMillis(), text);
-       }
-       /**
-        * Creates a new reply.
-        *
-        * @param sone
-        *            The Sone that creates the reply
-        * @param post
-        *            The post that this reply refers to
-        * @param time
-        *            The time of the reply
-        * @param text
-        *            The text of the reply
-        * @return The created reply
-        */
-       public PostReply createReply(Sone sone, Post post, long time, 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;
                }
-               final PostReply reply = new PostReplyImpl(sone, post, System.currentTimeMillis(), text.trim());
+               PostReplyBuilder postReplyBuilder = postReplyBuilderFactory.newPostReplyBuilder();
+               postReplyBuilder.randomId().from(sone).to(post.getId()).currentTime().withText(text.trim());
+               final PostReply reply = postReplyBuilder.build();
                synchronized (replies) {
                        replies.put(reply.getId(), reply);
                }
        }
  
        /**
-        * Generate a Sone URI from the given URI and latest edition.
-        *
-        * @param uriString
-        *            The URI to derive the Sone URI from
-        * @return The derived URI
-        */
-       private static FreenetURI getSoneUri(String uriString) {
-               try {
-                       FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
-                       return uri;
-               } catch (MalformedURLException mue1) {
-                       logger.log(Level.WARNING, String.format("Could not create Sone URI from URI: %s", uriString), mue1);
-                       return null;
-               }
-       }
-       /**
         * Notifies the core that a new {@link OwnIdentity} was added.
         *
         * @param ownIdentityAddedEvent