/*
- * 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
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;
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public class Sone implements Fingerprintable {
+public class Sone implements Fingerprintable, Comparable<Sone> {
/** comparator that sorts Sones by their nice name. */
public static final Comparator<Sone> NICE_NAME_COMPARATOR = new Comparator<Sone>() {
};
+ /**
+ * Comparator that sorts Sones by last activity (least recent active first).
+ */
+ public static final Comparator<Sone> LAST_ACTIVITY_COMPARATOR = new Comparator<Sone>() {
+
+ @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<Sone> POST_COUNT_COMPARATOR = new Comparator<Sone>() {
+
+ /**
+ * {@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<Sone> EMPTY_SONE_FILTER = new Filter<Sone>() {
+
+ @Override
+ public boolean filterObject(Sone sone) {
+ return sone.getTime() != 0;
+ }
+ };
+
+ /** Filter that matches all {@link Core#isLocalSone(Sone) local Sones}. */
+ public static final Filter<Sone> LOCAL_SONE_FILTER = new Filter<Sone>() {
+
+ @Override
+ public boolean filterObject(Sone sone) {
+ return sone.getIdentity() instanceof OwnIdentity;
+ }
+
+ };
+
/** The logger. */
private static final Logger logger = Logging.getLogger(Sone.class);
/** The albums of this Sone. */
private final List<Album> albums = Collections.synchronizedList(new ArrayList<Album>());
+ /** Sone-specific options. */
+ private final Options options = new Options();
+
/**
* Creates a new Sone.
*
*/
public Sone setRequestUri(FreenetURI requestUri) {
if (this.requestUri == null) {
- this.requestUri = requestUri.setDocName("Sone").setMetaString(new String[0]);
+ this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
return this;
}
if (!this.requestUri.equalsKeypair(requestUri)) {
*/
public Sone setInsertUri(FreenetURI insertUri) {
if (this.insertUri == null) {
- this.insertUri = insertUri.setDocName("Sone").setMetaString(new String[0]);
+ this.insertUri = insertUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
return this;
}
if (!this.insertUri.equalsKeypair(insertUri)) {
*/
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;
* @return This Sone (for method chaining)
*/
public synchronized Sone setPosts(Collection<Post> posts) {
- this.posts.clear();
- this.posts.addAll(posts);
+ synchronized (this) {
+ this.posts.clear();
+ this.posts.addAll(posts);
+ }
return this;
}
}
/**
+ * Sets the albums of this Sone.
+ *
+ * @param albums
+ * The albums of this Sone
+ */
+ public synchronized void setAlbums(Collection<? extends Album> albums) {
+ Validation.begin().isNotNull("Albums", albums).check();
+ this.albums.clear();
+ for (Album album : albums) {
+ addAlbum(album);
+ }
+ }
+
+ /**
* Removes an album from this Sone.
*
* @param album
albums.remove(album);
}
+ /**
+ * Returns Sone-specific options.
+ *
+ * @return The options of this Sone
+ */
+ public Options getOptions() {
+ return options;
+ }
+
//
// FINGERPRINTABLE METHODS
//
@Override
public synchronized String getFingerprint() {
StringBuilder fingerprint = new StringBuilder();
- fingerprint.append("Profile(");
- if (profile.getFirstName() != null) {
- fingerprint.append("FirstName(").append(profile.getFirstName()).append(')');
- }
- if (profile.getMiddleName() != null) {
- fingerprint.append("MiddleName(").append(profile.getMiddleName()).append(')');
- }
- if (profile.getLastName() != null) {
- fingerprint.append("LastName(").append(profile.getLastName()).append(')');
- }
- if (profile.getBirthDay() != null) {
- fingerprint.append("BirthDay(").append(profile.getBirthDay()).append(')');
- }
- if (profile.getBirthMonth() != null) {
- fingerprint.append("BirthMonth(").append(profile.getBirthMonth()).append(')');
- }
- if (profile.getBirthYear() != null) {
- fingerprint.append("BirthYear(").append(profile.getBirthYear()).append(')');
- }
- fingerprint.append(")");
+ fingerprint.append(profile.getFingerprint());
fingerprint.append("Posts(");
for (Post post : getPosts()) {
}
fingerprint.append(")");
+ @SuppressWarnings("hiding")
List<Reply> replies = new ArrayList<Reply>(getReplies());
Collections.sort(replies, Reply.TIME_COMPARATOR);
fingerprint.append("Replies(");
}
fingerprint.append(')');
+ @SuppressWarnings("hiding")
List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
Collections.sort(likedPostIds);
fingerprint.append("LikedPosts(");
}
fingerprint.append(')');
+ @SuppressWarnings("hiding")
List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
Collections.sort(likedReplyIds);
fingerprint.append("LikedReplies(");
}
//
+ // 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<Album> flattenAlbums(Collection<? extends Album> albums) {
+ List<Album> flatAlbums = new ArrayList<Album>();
+ flatAlbums.addAll(albums);
+ int lastAlbumIndex = 0;
+ while (lastAlbumIndex < flatAlbums.size()) {
+ int previousAlbumCount = flatAlbums.size();
+ for (Album album : new ArrayList<Album>(flatAlbums.subList(lastAlbumIndex, flatAlbums.size()))) {
+ flatAlbums.addAll(album.getAlbums());
+ }
+ lastAlbumIndex = previousAlbumCount;
+ }
+ return flatAlbums;
+ }
+
+ //
+ // INTERFACE Comparable<Sone>
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int compareTo(Sone sone) {
+ return NICE_NAME_COMPARATOR.compare(this, sone);
+ }
+
+ //
// OBJECT METHODS
//