projects
/
Sone.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Remove all elements from a notification.
[Sone.git]
/
src
/
main
/
java
/
net
/
pterodactylus
/
sone
/
data
/
Sone.java
diff --git
a/src/main/java/net/pterodactylus/sone/data/Sone.java
b/src/main/java/net/pterodactylus/sone/data/Sone.java
index
ac6e41a
..
a3e872b
100644
(file)
--- a/
src/main/java/net/pterodactylus/sone/data/Sone.java
+++ b/
src/main/java/net/pterodactylus/sone/data/Sone.java
@@
-1,5
+1,5
@@
/*
/*
- * Sone - Sone.java - Copyright © 2010–201
2
David Roden
+ * Sone - Sone.java - Copyright © 2010–201
3
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
*
* 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
@@
-17,6
+17,9
@@
package net.pterodactylus.sone.data;
package net.pterodactylus.sone.data;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@
-28,14
+31,16
@@
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.logging.Level;
import java.util.logging.Logger;
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.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.collection.filter.Filter;
import net.pterodactylus.util.logging.Logging;
import net.pterodactylus.util.logging.Logging;
-import net.pterodactylus.util.validation.Validation;
+
+import com.google.common.base.Predicate;
+import com.google.common.hash.Hasher;
+import com.google.common.hash.Hashing;
+
import freenet.keys.FreenetURI;
/**
import freenet.keys.FreenetURI;
/**
@@
-142,29
+147,29
@@
public class Sone implements Fingerprintable, Comparable<Sone> {
};
/** Filter to remove Sones that have not been downloaded. */
};
/** Filter to remove Sones that have not been downloaded. */
- public static final
Filter<Sone> EMPTY_SONE_FILTER = new Filter
<Sone>() {
+ public static final
Predicate<Sone> EMPTY_SONE_FILTER = new Predicate
<Sone>() {
@Override
@Override
- public boolean
filterObject
(Sone sone) {
+ public boolean
apply
(Sone sone) {
return sone.getTime() != 0;
}
};
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>() {
+ /** Filter that matches all {@link
Sone#isLocal(
) local Sones}. */
+ public static final
Predicate<Sone> LOCAL_SONE_FILTER = new Predicate
<Sone>() {
@Override
@Override
- public boolean
filterObject
(Sone sone) {
+ public boolean
apply
(Sone sone) {
return sone.getIdentity() instanceof OwnIdentity;
}
};
/** Filter that matches Sones that have at least one album. */
return sone.getIdentity() instanceof OwnIdentity;
}
};
/** Filter that matches Sones that have at least one album. */
- public static final
Filter<Sone> HAS_ALBUM_FILTER = new Filter
<Sone>() {
+ public static final
Predicate<Sone> HAS_ALBUM_FILTER = new Predicate
<Sone>() {
@Override
@Override
- public boolean
filterObject
(Sone sone) {
+ public boolean
apply
(Sone sone) {
return !sone.getAlbums().isEmpty();
}
};
return !sone.getAlbums().isEmpty();
}
};
@@
-175,6
+180,9
@@
public class Sone implements Fingerprintable, Comparable<Sone> {
/** The ID of this Sone. */
private final String id;
/** The ID of this Sone. */
private final String id;
+ /** Whether the Sone is local. */
+ private final boolean local;
+
/** The identity of this Sone. */
private Identity identity;
/** The identity of this Sone. */
private Identity identity;
@@
-222,16
+230,20
@@
public class Sone implements Fingerprintable, Comparable<Sone> {
private final List<Album> albums = new CopyOnWriteArrayList<Album>();
/** Sone-specific options. */
private final List<Album> albums = new CopyOnWriteArrayList<Album>();
/** Sone-specific options. */
- private
final
Options options = new Options();
+ private Options options = new Options();
/**
* Creates a new Sone.
*
* @param id
* The ID of the Sone
/**
* Creates a new Sone.
*
* @param id
* The ID of the Sone
+ * @param local
+ * {@code true} if the Sone is a local Sone, {@code false}
+ * otherwise
*/
*/
- public Sone(String id) {
+ public Sone(String id
, boolean local
) {
this.id = id;
this.id = id;
+ this.local = local;
}
//
}
//
@@
-284,6
+296,16
@@
public class Sone implements Fingerprintable, Comparable<Sone> {
}
/**
}
/**
+ * Returns whether this Sone is a local Sone.
+ *
+ * @return {@code true} if this Sone is a local Sone, {@code false}
+ * otherwise
+ */
+ public boolean isLocal() {
+ return local;
+ }
+
+ /**
* Returns the request URI of this Sone.
*
* @return The request URI of this Sone
* Returns the request URI of this Sone.
*
* @return The request URI of this Sone
@@
-404,8
+426,7
@@
public class Sone implements Fingerprintable, Comparable<Sone> {
* if {@code status} is {@code null}
*/
public Sone setStatus(SoneStatus status) {
* if {@code status} is {@code null}
*/
public Sone setStatus(SoneStatus status) {
- Validation.begin().isNotNull("Sone Status", status).check();
- this.status = status;
+ this.status = checkNotNull(status, "status must not be null");
return this;
}
return this;
}
@@
-790,8
+811,11
@@
public class Sone implements Fingerprintable, Comparable<Sone> {
* The album to add
*/
public void addAlbum(Album album) {
* The album to add
*/
public void addAlbum(Album album) {
- Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).check();
- albums.add(album);
+ checkNotNull(album, "album must not be null");
+ checkArgument(album.getSone().equals(this), "album must belong to this Sone");
+ if (!albums.contains(album)) {
+ albums.add(album);
+ }
}
/**
}
/**
@@
-801,7
+825,7
@@
public class Sone implements Fingerprintable, Comparable<Sone> {
* The albums of this Sone
*/
public void setAlbums(Collection<? extends Album> albums) {
* The albums of this Sone
*/
public void setAlbums(Collection<? extends Album> albums) {
-
Validation.begin().isNotNull("Albums", albums).check(
);
+
checkNotNull(albums, "albums must not be null"
);
this.albums.clear();
for (Album album : albums) {
addAlbum(album);
this.albums.clear();
for (Album album : albums) {
addAlbum(album);
@@
-815,7
+839,8
@@
public class Sone implements Fingerprintable, Comparable<Sone> {
* The album to remove
*/
public void removeAlbum(Album album) {
* The album to remove
*/
public void removeAlbum(Album album) {
- Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).check();
+ checkNotNull(album, "album must not be null");
+ checkArgument(album.getSone().equals(this), "album must belong to this Sone");
albums.remove(album);
}
albums.remove(album);
}
@@
-829,7
+854,9
@@
public class Sone implements Fingerprintable, Comparable<Sone> {
* <code>null</code> if the album did not change its place
*/
public Album moveAlbumUp(Album album) {
* <code>null</code> if the album did not change its place
*/
public Album moveAlbumUp(Album album) {
- Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).isNull("Album Parent", album.getParent()).check();
+ checkNotNull(album, "album must not be null");
+ checkArgument(album.getSone().equals(this), "album must belong to this Sone");
+ checkArgument(album.getParent() == null, "album must not have a parent");
int oldIndex = albums.indexOf(album);
if (oldIndex <= 0) {
return null;
int oldIndex = albums.indexOf(album);
if (oldIndex <= 0) {
return null;
@@
-849,7
+876,9
@@
public class Sone implements Fingerprintable, Comparable<Sone> {
* <code>null</code> if the album did not change its place
*/
public Album moveAlbumDown(Album album) {
* <code>null</code> if the album did not change its place
*/
public Album moveAlbumDown(Album album) {
- Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).isNull("Album Parent", album.getParent()).check();
+ checkNotNull(album, "album must not be null");
+ checkArgument(album.getSone().equals(this), "album must belong to this Sone");
+ checkArgument(album.getParent() == null, "album must not have a parent");
int oldIndex = albums.indexOf(album);
if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
return null;
int oldIndex = albums.indexOf(album);
if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
return null;
@@
-868,6
+897,17
@@
public class Sone implements Fingerprintable, Comparable<Sone> {
return options;
}
return options;
}
+ /**
+ * Sets the options of this Sone.
+ *
+ * @param options
+ * The options of this Sone
+ */
+ /* TODO - remove this method again, maybe add an option provider */
+ public void setOptions(Options options) {
+ this.options = options;
+ }
+
//
// FINGERPRINTABLE METHODS
//
//
// FINGERPRINTABLE METHODS
//
@@
-877,46
+917,46
@@
public class Sone implements Fingerprintable, Comparable<Sone> {
*/
@Override
public synchronized String getFingerprint() {
*/
@Override
public synchronized String getFingerprint() {
-
StringBuilder fingerprint = new StringBuild
er();
-
fingerprint.append
(profile.getFingerprint());
+
Hasher hash = Hashing.sha256().newHash
er();
+
hash.putString
(profile.getFingerprint());
-
fingerprint.append
("Posts(");
+
hash.putString
("Posts(");
for (Post post : getPosts()) {
for (Post post : getPosts()) {
-
fingerprint.append("Post(").append(post.getId()).append(')'
);
+
hash.putString("Post(").putString(post.getId()).putString(")"
);
}
}
-
fingerprint.append
(")");
+
hash.putString
(")");
List<PostReply> replies = new ArrayList<PostReply>(getReplies());
Collections.sort(replies, Reply.TIME_COMPARATOR);
List<PostReply> replies = new ArrayList<PostReply>(getReplies());
Collections.sort(replies, Reply.TIME_COMPARATOR);
-
fingerprint.append
("Replies(");
+
hash.putString
("Replies(");
for (PostReply reply : replies) {
for (PostReply reply : replies) {
-
fingerprint.append("Reply(").append(reply.getId()).append(')'
);
+
hash.putString("Reply(").putString(reply.getId()).putString(")"
);
}
}
-
fingerprint.append(')'
);
+
hash.putString(")"
);
List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
Collections.sort(likedPostIds);
List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
Collections.sort(likedPostIds);
-
fingerprint.append
("LikedPosts(");
+
hash.putString
("LikedPosts(");
for (String likedPostId : likedPostIds) {
for (String likedPostId : likedPostIds) {
-
fingerprint.append("Post(").append(likedPostId).append(')'
);
+
hash.putString("Post(").putString(likedPostId).putString(")"
);
}
}
-
fingerprint.append(')'
);
+
hash.putString(")"
);
List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
Collections.sort(likedReplyIds);
List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
Collections.sort(likedReplyIds);
-
fingerprint.append
("LikedReplies(");
+
hash.putString
("LikedReplies(");
for (String likedReplyId : likedReplyIds) {
for (String likedReplyId : likedReplyIds) {
-
fingerprint.append("Reply(").append(likedReplyId).append(')'
);
+
hash.putString("Reply(").putString(likedReplyId).putString(")"
);
}
}
-
fingerprint.append(')'
);
+
hash.putString(")"
);
-
fingerprint.append
("Albums(");
+
hash.putString
("Albums(");
for (Album album : albums) {
for (Album album : albums) {
-
fingerprint.append
(album.getFingerprint());
+
hash.putString
(album.getFingerprint());
}
}
-
fingerprint.append(')'
);
+
hash.putString(")"
);
- return
fingerprint
.toString();
+ return
hash.hash()
.toString();
}
//
}
//