Extract nice name formatting into its own method.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / SoneShell.java
index 50b1189..16afea1 100644 (file)
@@ -19,11 +19,15 @@ package net.pterodactylus.sone.data;
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 import java.util.UUID;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
+import net.pterodactylus.util.logging.Logging;
 import freenet.keys.FreenetURI;
 
 /**
@@ -33,6 +37,18 @@ import freenet.keys.FreenetURI;
  */
 public class SoneShell extends Sone implements Shell<Sone> {
 
+       /** The logger. */
+       private static final Logger logger = Logging.getLogger(SoneShell.class);
+
+       /** The shell creator. */
+       public static final ShellCreator<Sone> creator = new ShellCreator<Sone>() {
+
+               @Override
+               public Shell<Sone> createShell(String id) {
+                       return new SoneShell().setId(id);
+               }
+       };
+
        /** A GUID for this Sone. */
        private UUID id;
 
@@ -82,8 +98,13 @@ public class SoneShell extends Sone implements Shell<Sone> {
         *            The ID of the Sone
         * @return This Sone shell (for method chaining)
         */
-       public SoneShell setId(UUID id) {
-               this.id = id;
+       public SoneShell setId(String id) {
+               try {
+                       this.id = UUID.fromString(id);
+               } catch (IllegalArgumentException iae1) {
+                       logger.log(Level.WARNING, "Invalid ID: “" + id + "”.", iae1);
+                       this.id = UUID.randomUUID();
+               }
                return this;
        }
 
@@ -201,13 +222,22 @@ public class SoneShell extends Sone implements Shell<Sone> {
        }
 
        /**
-        * Returns the list of posts of this Sone.
+        * Returns the list of posts of this Sone, sorted by time, newest first.
         *
         * @return All posts of this Sone
         */
        @Override
        public List<Post> getPosts() {
-               return Collections.unmodifiableList(posts);
+               List<Post> sortedPosts = new ArrayList<Post>(posts);
+               Collections.sort(sortedPosts, new Comparator<Post>() {
+
+                       @Override
+                       public int compare(Post leftPost, Post rightPost) {
+                               return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
+                       }
+
+               });
+               return sortedPosts;
        }
 
        /**