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;
/**
* {@link Shell} around a {@link Post} that has not yet been retrieved from
*/
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() {
- return new PostShell();
+ public Shell<Post> createShell(String id) {
+ return new PostShell().setId(id);
}
};
* 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;
}
*/
@Override
public long getTime() {
- return time;
+ return (time != null) ? time : 0;
}
/**
* @return All replies to this post
*/
@Override
- public Set<Reply> getReplies() {
- return Collections.unmodifiableSet(replies);
+ public List<Reply> getReplies() {
+ List<Reply> sortedReplies = new ArrayList<Reply>(replies);
+ Collections.sort(sortedReplies, new Comparator<Reply>() {
+
+ @Override
+ public int compare(Reply leftReply, Reply rightReply) {
+ return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, leftReply.getTime() - rightReply.getTime()));
+ }
+
+ });
+ return sortedReplies;
}
/**
*/
@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);
}
/**