X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2FSone.java;h=b8db269712c4ee3eac5bf165097b5a304c9cc45f;hp=f183b390636c8c4984e59a3c5d37ac2b4412aabb;hb=0df5e91852f737d760c5a9f54c5667309fbadcc2;hpb=07ca98b45465f7a442e695fbfb6f59100260f39b diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index f183b39..b8db269 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -1,5 +1,5 @@ /* - * FreenetSone - Sone.java - Copyright © 2010 David Roden + * Sone - Sone.java - Copyright © 2010 David Roden * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,9 +27,14 @@ import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; +import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.core.Options; import net.pterodactylus.sone.freenet.wot.Identity; +import net.pterodactylus.sone.freenet.wot.OwnIdentity; import net.pterodactylus.sone.template.SoneAccessor; +import net.pterodactylus.util.filter.Filter; import net.pterodactylus.util.logging.Logging; +import net.pterodactylus.util.validation.Validation; import freenet.keys.FreenetURI; /** @@ -40,7 +45,7 @@ import freenet.keys.FreenetURI; * * @author David ‘Bombe’ Roden */ -public class Sone implements Fingerprintable { +public class Sone implements Fingerprintable, Comparable { /** comparator that sorts Sones by their nice name. */ public static final Comparator NICE_NAME_COMPARATOR = new Comparator() { @@ -56,6 +61,48 @@ public class Sone implements Fingerprintable { }; + /** + * Comparator that sorts Sones by last activity (least recent active first). + */ + public static final Comparator LAST_ACTIVITY_COMPARATOR = new Comparator() { + + @Override + public int compare(Sone firstSone, Sone secondSone) { + return (int) Math.min(Integer.MAX_VALUE, Math.max(Integer.MIN_VALUE, secondSone.getTime() - firstSone.getTime())); + } + }; + + /** Comparator that sorts Sones by numbers of posts (descending). */ + public static final Comparator POST_COUNT_COMPARATOR = new Comparator() { + + /** + * {@inheritDoc} + */ + @Override + public int compare(Sone leftSone, Sone rightSone) { + return (leftSone.getPosts().size() != rightSone.getPosts().size()) ? (rightSone.getPosts().size() - leftSone.getPosts().size()) : (rightSone.getReplies().size() - leftSone.getReplies().size()); + } + }; + + /** Filter to remove Sones that have not been downloaded. */ + public static final Filter EMPTY_SONE_FILTER = new Filter() { + + @Override + public boolean filterObject(Sone sone) { + return sone.getTime() != 0; + } + }; + + /** Filter that matches all {@link Core#isLocalSone(Sone) local Sones}. */ + public static final Filter LOCAL_SONE_FILTER = new Filter() { + + @Override + public boolean filterObject(Sone sone) { + return sone.getIdentity() instanceof OwnIdentity; + } + + }; + /** The logger. */ private static final Logger logger = Logging.getLogger(Sone.class); @@ -99,6 +146,12 @@ public class Sone implements Fingerprintable { /** The IDs of all liked replies. */ private final Set likedReplyIds = Collections.synchronizedSet(new HashSet()); + /** The albums of this Sone. */ + private final List albums = Collections.synchronizedList(new ArrayList()); + + /** Sone-specific options. */ + private final Options options = new Options(); + /** * Creates a new Sone. * @@ -233,7 +286,7 @@ public class Sone implements Fingerprintable { */ public void setLatestEdition(long latestEdition) { if (!(latestEdition > this.latestEdition)) { - logger.log(Level.INFO, "New latest edition %d is not greater than current latest edition %d!", new Object[] { latestEdition, this.latestEdition }); + logger.log(Level.FINE, "New latest edition %d is not greater than current latest edition %d!", new Object[] { latestEdition, this.latestEdition }); return; } this.latestEdition = latestEdition; @@ -387,8 +440,10 @@ public class Sone implements Fingerprintable { * @return This Sone (for method chaining) */ public synchronized Sone setPosts(Collection posts) { - this.posts.clear(); - this.posts.addAll(posts); + synchronized (this) { + this.posts.clear(); + this.posts.addAll(posts); + } return this; } @@ -580,6 +635,60 @@ public class Sone implements Fingerprintable { return this; } + /** + * Returns the albums of this Sone. + * + * @return The albums of this Sone + */ + public List getAlbums() { + return Collections.unmodifiableList(albums); + } + + /** + * Adds an album to this Sone. + * + * @param album + * The album to add + */ + public synchronized void addAlbum(Album album) { + Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).check(); + albums.add(album); + } + + /** + * Sets the albums of this Sone. + * + * @param albums + * The albums of this Sone + */ + public synchronized void setAlbums(Collection albums) { + Validation.begin().isNotNull("Albums", albums).check(); + this.albums.clear(); + for (Album album : albums) { + addAlbum(album); + } + } + + /** + * Removes an album from this Sone. + * + * @param album + * The album to remove + */ + public synchronized void removeAlbum(Album album) { + Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).check(); + albums.remove(album); + } + + /** + * Returns Sone-specific options. + * + * @return The options of this Sone + */ + public Options getOptions() { + return options; + } + // // FINGERPRINTABLE METHODS // @@ -598,6 +707,7 @@ public class Sone implements Fingerprintable { } fingerprint.append(")"); + @SuppressWarnings("hiding") List replies = new ArrayList(getReplies()); Collections.sort(replies, Reply.TIME_COMPARATOR); fingerprint.append("Replies("); @@ -606,6 +716,7 @@ public class Sone implements Fingerprintable { } fingerprint.append(')'); + @SuppressWarnings("hiding") List likedPostIds = new ArrayList(getLikedPostIds()); Collections.sort(likedPostIds); fingerprint.append("LikedPosts("); @@ -614,6 +725,7 @@ public class Sone implements Fingerprintable { } fingerprint.append(')'); + @SuppressWarnings("hiding") List likedReplyIds = new ArrayList(getLikedReplyIds()); Collections.sort(likedReplyIds); fingerprint.append("LikedReplies("); @@ -622,10 +734,55 @@ public class Sone implements Fingerprintable { } fingerprint.append(')'); + fingerprint.append("Albums("); + for (Album album : albums) { + fingerprint.append(album.getFingerprint()); + } + fingerprint.append(')'); + return fingerprint.toString(); } // + // STATIC METHODS + // + + /** + * 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; + } + + // + // INTERFACE Comparable + // + + /** + * {@inheritDoc} + */ + @Override + public int compareTo(Sone sone) { + return NICE_NAME_COMPARATOR.compare(this, sone); + } + + // // OBJECT METHODS //