Prevent NPEs when accessing an unset time.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / PostShell.java
index 36dba42..716aab9 100644 (file)
@@ -21,6 +21,10 @@ import java.util.Collections;
 import java.util.HashSet;
 import java.util.Set;
 import java.util.UUID;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import net.pterodactylus.util.logging.Logging;
 
 /**
  * {@link Shell} around a {@link Post} that has not yet been retrieved from
@@ -30,6 +34,18 @@ import java.util.UUID;
  */
 public class PostShell extends Post implements Shell<Post> {
 
+       /** The logger. */
+       private static final Logger logger = Logging.getLogger(PostShell.class);
+
+       /** The shell creator. */
+       public static final ShellCreator<Post> creator = new ShellCreator<Post>() {
+
+               @Override
+               public Shell<Post> createShell(String id) {
+                       return new PostShell().setId(id);
+               }
+       };
+
        /** The GUID of the post. */
        private UUID id;
 
@@ -73,8 +89,13 @@ public class PostShell extends Post implements Shell<Post> {
         *            The ID of the post
         * @return This post shell (for method chaining)
         */
-       public PostShell setId(UUID id) {
-               this.id = id;
+       public PostShell 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;
        }
 
@@ -107,7 +128,7 @@ public class PostShell extends Post implements Shell<Post> {
         */
        @Override
        public long getTime() {
-               return time;
+               return (time != null) ? time : 0;
        }
 
        /**
@@ -190,7 +211,7 @@ public class PostShell extends Post implements Shell<Post> {
         */
        @Override
        public boolean canUnshell() {
-               return (id != null) && (sone != null) && (time != null) && (text != null);
+               return (id != null) && (sone != null) && (!(sone instanceof Shell<?>)) && (time != null) && (text != null);
        }
 
        /**
@@ -204,7 +225,7 @@ public class PostShell extends Post implements Shell<Post> {
                                post.addReply(reply);
                        }
                }
-               return null;
+               return this;
        }
 
 }