X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2FSoneShell.java;h=9063d31827f18d79199b2281c900f2347aab996d;hb=78cd820b392069def4640d45497e4097ef031d53;hp=8b2f727c4f0b133d17f59d09b3d42e61e0c33726;hpb=97865d6020fd9ad90961bdf66941d35d792e6c8f;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/data/SoneShell.java b/src/main/java/net/pterodactylus/sone/data/SoneShell.java index 8b2f727..9063d31 100644 --- a/src/main/java/net/pterodactylus/sone/data/SoneShell.java +++ b/src/main/java/net/pterodactylus/sone/data/SoneShell.java @@ -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 { + /** The logger. */ + private static final Logger logger = Logging.getLogger(SoneShell.class); + + /** The shell creator. */ + public static final ShellCreator creator = new ShellCreator() { + + @Override + public Shell 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 { * 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; } @@ -157,7 +178,7 @@ public class SoneShell extends Sone implements Shell { * @return The friend Sones of this Sone */ @Override - public Set getFriendSones() { + public Set getFriends() { return Collections.unmodifiableSet(friendSones); } @@ -170,7 +191,7 @@ public class SoneShell extends Sone implements Shell { * {@code false} otherwise */ @Override - public boolean hasFriendSone(Sone friendSone) { + public boolean hasFriend(Sone friendSone) { return friendSones.contains(friendSone); } @@ -182,7 +203,7 @@ public class SoneShell extends Sone implements Shell { * @return This Sone (for method chaining) */ @Override - public Sone addFriendSone(Sone friendSone) { + public Sone addFriend(Sone friendSone) { friendSones.add(friendSone); return this; } @@ -195,19 +216,28 @@ public class SoneShell extends Sone implements Shell { * @return This Sone (for method chaining) */ @Override - public Sone removeFriendSone(Sone friendSone) { + public Sone removeFriend(Sone friendSone) { friendSones.remove(friendSone); return this; } /** - * 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 getPosts() { - return Collections.unmodifiableList(posts); + List sortedPosts = new ArrayList(posts); + Collections.sort(sortedPosts, new Comparator() { + + @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; } /** @@ -287,7 +317,7 @@ public class SoneShell extends Sone implements Shell { Sone sone = new Sone(id, name, requestUri); sone.setProfile(profile); for (Sone friendSone : friendSones) { - sone.addFriendSone(friendSone); + sone.addFriend(friendSone); } for (Post post : posts) { sone.addPost(post); @@ -297,7 +327,7 @@ public class SoneShell extends Sone implements Shell { } return sone; } - return null; + return this; } }