X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2FSone.java;h=a60998297d5f64f69a4fe5496b2410a551d7eb56;hp=c2f9ae19585251698f0e2964afaa2700a0f4820d;hb=69870060d45d5cdca064e67779dfc5619001ae42;hpb=85aa9c556ef8ac726694f0077a4f8df0bc912e5f diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index c2f9ae1..a609982 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 @@ /* - * Sone - Sone.java - Copyright © 2010 David Roden + * Sone - Sone.java - Copyright © 2010–2013 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 @@ -17,6 +17,9 @@ 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; @@ -28,14 +31,14 @@ import java.util.concurrent.CopyOnWriteArraySet; 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 com.google.common.base.Predicate; + import freenet.keys.FreenetURI; /** @@ -142,29 +145,29 @@ public class Sone implements Fingerprintable, Comparable { }; /** Filter to remove Sones that have not been downloaded. */ - public static final Filter EMPTY_SONE_FILTER = new Filter() { + public static final Predicate EMPTY_SONE_FILTER = new Predicate() { @Override - public boolean filterObject(Sone sone) { + public boolean apply(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() { + /** Filter that matches all {@link Sone#isLocal() local Sones}. */ + public static final Predicate LOCAL_SONE_FILTER = new Predicate() { @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. */ - public static final Filter HAS_ALBUM_FILTER = new Filter() { + public static final Predicate HAS_ALBUM_FILTER = new Predicate() { @Override - public boolean filterObject(Sone sone) { + public boolean apply(Sone sone) { return !sone.getAlbums().isEmpty(); } }; @@ -175,6 +178,9 @@ public class Sone implements Fingerprintable, Comparable { /** 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; @@ -222,16 +228,20 @@ public class Sone implements Fingerprintable, Comparable { private final List albums = new CopyOnWriteArrayList(); /** 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 + * @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.local = local; } // @@ -284,6 +294,16 @@ public class Sone implements Fingerprintable, Comparable { } /** + * 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 @@ -305,7 +325,7 @@ public class Sone implements Fingerprintable, Comparable { return this; } if (!this.requestUri.equalsKeypair(requestUri)) { - logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { requestUri, this.requestUri }); + logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", requestUri, this.requestUri)); return this; } return this; @@ -333,7 +353,7 @@ public class Sone implements Fingerprintable, Comparable { return this; } if (!this.insertUri.equalsKeypair(insertUri)) { - logger.log(Level.WARNING, "Request URI %s tried to overwrite %s!", new Object[] { insertUri, this.insertUri }); + logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", insertUri, this.insertUri)); return this; } return this; @@ -358,7 +378,7 @@ public class Sone implements Fingerprintable, Comparable { */ public void setLatestEdition(long latestEdition) { if (!(latestEdition > this.latestEdition)) { - logger.log(Level.FINE, "New latest edition %d is not greater than current latest edition %d!", new Object[] { latestEdition, this.latestEdition }); + logger.log(Level.FINE, String.format("New latest edition %d is not greater than current latest edition %d!", latestEdition, this.latestEdition)); return; } this.latestEdition = latestEdition; @@ -404,8 +424,7 @@ public class Sone implements Fingerprintable, Comparable { * 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; } @@ -560,7 +579,7 @@ public class Sone implements Fingerprintable, Comparable { */ public void addPost(Post post) { if (post.getSone().equals(this) && posts.add(post)) { - logger.log(Level.FINEST, "Adding %s to “%s”.", new Object[] { post, getName() }); + logger.log(Level.FINEST, String.format("Adding %s to “%s”.", post, getName())); } } @@ -790,8 +809,11 @@ public class Sone implements Fingerprintable, Comparable { * 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 +823,7 @@ public class Sone implements Fingerprintable, Comparable { * The albums of this Sone */ public void setAlbums(Collection albums) { - Validation.begin().isNotNull("Albums", albums).check(); + checkNotNull(albums, "albums must not be null"); this.albums.clear(); for (Album album : albums) { addAlbum(album); @@ -815,7 +837,8 @@ public class Sone implements Fingerprintable, Comparable { * 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); } @@ -829,7 +852,9 @@ public class Sone implements Fingerprintable, Comparable { * null 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; @@ -849,7 +874,9 @@ public class Sone implements Fingerprintable, Comparable { * null 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; @@ -868,6 +895,17 @@ public class Sone implements Fingerprintable, Comparable { 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 //