X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2FSone.java;h=e36a3063f2d1ba37e7bea1e24f3edfe903079a28;hp=e124387a9f3a37eadfdd6f582e406270fc4b5e5f;hb=8cbc1e2bc1a94d2ceb6a635389ed4791a08a4889;hpb=38cb6c5ec82298ee351d0eb15ddd8331db273af2 diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index e124387..e36a306 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -84,6 +84,18 @@ public class Sone implements Fingerprintable, Comparable { } }; + /** Comparator that sorts Sones by number of images (descending). */ + public static final Comparator IMAGE_COUNT_COMPARATOR = new Comparator() { + + /** + * {@inheritDoc} + */ + @Override + public int compare(Sone leftSone, Sone rightSone) { + return rightSone.getAllImages().size() - leftSone.getAllImages().size(); + } + }; + /** Filter to remove Sones that have not been downloaded. */ public static final Filter EMPTY_SONE_FILTER = new Filter() { @@ -103,6 +115,15 @@ public class Sone implements Fingerprintable, Comparable { }; + /** Filter that matches Sones that have at least one album. */ + public static final Filter HAS_ALBUM_FILTER = new Filter() { + + @Override + public boolean filterObject(Sone sone) { + return !sone.getAlbums().isEmpty(); + } + }; + /** The logger. */ private static final Logger logger = Logging.getLogger(Sone.class); @@ -138,7 +159,7 @@ public class Sone implements Fingerprintable, Comparable { private final Set posts = Collections.synchronizedSet(new HashSet()); /** All replies. */ - private final Set replies = Collections.synchronizedSet(new HashSet()); + private final Set replies = Collections.synchronizedSet(new HashSet()); /** The IDs of all liked posts. */ private final Set likedPostIds = Collections.synchronizedSet(new HashSet()); @@ -152,6 +173,9 @@ public class Sone implements Fingerprintable, Comparable { /** Sone-specific options. */ private final Options options = new Options(); + /** The avatar of this Sone. */ + private volatile String avatar; + /** * Creates a new Sone. * @@ -368,19 +392,6 @@ public class Sone implements Fingerprintable, Comparable { } /** - * Sets all friends of this Sone at once. - * - * @param friends - * The new (and only) friends of this Sone - * @return This Sone (for method chaining) - */ - public Sone setFriends(Collection friends) { - friendSones.clear(); - friendSones.addAll(friends); - return this; - } - - /** * Returns whether this Sone has the given Sone as a friend Sone. * * @param friendSoneId @@ -477,7 +488,7 @@ public class Sone implements Fingerprintable, Comparable { * * @return All replies this Sone made */ - public synchronized Set getReplies() { + public synchronized Set getReplies() { return Collections.unmodifiableSet(replies); } @@ -488,7 +499,7 @@ public class Sone implements Fingerprintable, Comparable { * The new (and only) replies of this Sone * @return This Sone (for method chaining) */ - public synchronized Sone setReplies(Collection replies) { + public synchronized Sone setReplies(Collection replies) { this.replies.clear(); this.replies.addAll(replies); return this; @@ -501,7 +512,7 @@ public class Sone implements Fingerprintable, Comparable { * @param reply * The reply to add */ - public synchronized void addReply(Reply reply) { + public synchronized void addReply(PostReply reply) { if (reply.getSone().equals(this)) { replies.add(reply); } @@ -513,7 +524,7 @@ public class Sone implements Fingerprintable, Comparable { * @param reply * The reply to remove */ - public synchronized void removeReply(Reply reply) { + public synchronized void removeReply(PostReply reply) { if (reply.getSone().equals(this)) { replies.remove(reply); } @@ -645,6 +656,41 @@ public class Sone implements Fingerprintable, Comparable { } /** + * Returns a flattened list of all albums of this Sone. The resulting list + * contains parent albums before child albums so that the resulting list can + * be parsed in a single pass. + * + * @return The flattened albums + */ + public List getAllAlbums() { + List flatAlbums = new ArrayList(); + flatAlbums.addAll(albums); + int lastAlbumIndex = 0; + while (lastAlbumIndex < flatAlbums.size()) { + int previousAlbumCount = flatAlbums.size(); + for (Album album : new ArrayList(flatAlbums.subList(lastAlbumIndex, flatAlbums.size()))) { + flatAlbums.addAll(album.getAlbums()); + } + lastAlbumIndex = previousAlbumCount; + } + return flatAlbums; + } + + /** + * Returns all images of a Sone. Images of a album are inserted into this + * list before images of all child albums. + * + * @return The list of all images + */ + public List getAllImages() { + List allImages = new ArrayList(); + for (Album album : getAllAlbums()) { + allImages.addAll(album.getImages()); + } + return allImages; + } + + /** * Adds an album to this Sone. * * @param album @@ -721,6 +767,34 @@ public class Sone implements Fingerprintable, Comparable { } /** + * Returns the ID of the currently selected avatar image. + * + * @return The ID of the currently selected avatar image, or {@code null} if + * no avatar is selected. + */ + public String getAvatar() { + return avatar; + } + + /** + * Sets the avatar image. + * + * @param avatar + * The new avatar image, or {@code null} to not select an avatar + * image. + * @return This Sone + */ + public Sone setAvatar(Image avatar) { + if (avatar == null) { + this.avatar = null; + return this; + } + Validation.begin().isEqual("Image Owner", avatar.getSone(), this).check(); + this.avatar = avatar.getId(); + return this; + } + + /** * Returns Sone-specific options. * * @return The options of this Sone @@ -747,10 +821,10 @@ public class Sone implements Fingerprintable, Comparable { } fingerprint.append(")"); - List replies = new ArrayList(getReplies()); + List replies = new ArrayList(getReplies()); Collections.sort(replies, Reply.TIME_COMPARATOR); fingerprint.append("Replies("); - for (Reply reply : replies) { + for (PostReply reply : replies) { fingerprint.append("Reply(").append(reply.getId()).append(')'); } fingerprint.append(')'); @@ -777,34 +851,9 @@ public class Sone implements Fingerprintable, Comparable { } fingerprint.append(')'); - return fingerprint.toString(); - } - - // - // STATIC METHODS - // + fingerprint.append("Avatar(").append(avatar).append(')'); - /** - * Flattens the given top-level albums so that the resulting list contains - * parent albums before child albums and the resulting list can be parsed in - * a single pass. - * - * @param albums - * The albums to flatten - * @return The flattened albums - */ - public static List flattenAlbums(Collection albums) { - List flatAlbums = new ArrayList(); - flatAlbums.addAll(albums); - int lastAlbumIndex = 0; - while (lastAlbumIndex < flatAlbums.size()) { - int previousAlbumCount = flatAlbums.size(); - for (Album album : new ArrayList(flatAlbums.subList(lastAlbumIndex, flatAlbums.size()))) { - flatAlbums.addAll(album.getAlbums()); - } - lastAlbumIndex = previousAlbumCount; - } - return flatAlbums; + return fingerprint.toString(); } //