From 7948a66da729b30be545d553e7a8234b5957b165 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 19 Oct 2013 16:14:10 +0200 Subject: [PATCH] Remove setIdentity() from Sone. --- .../java/net/pterodactylus/sone/core/Core.java | 8 ++--- .../net/pterodactylus/sone/core/SoneParser.java | 2 +- .../java/net/pterodactylus/sone/data/Sone.java | 12 ------- .../sone/data/impl/AbstractSoneBuilder.java | 37 ++++++++++++++++++++++ .../pterodactylus/sone/data/impl/DefaultSone.java | 17 ++-------- .../sone/data/impl/DefaultSoneBuilder.java | 32 +++++++++++++++++++ .../net/pterodactylus/sone/database/Database.java | 2 +- .../sone/database/IdentityDatabase.java | 16 ++++++++++ .../pterodactylus/sone/database/SoneBuilder.java | 12 +++++-- .../sone/database/memory/MemoryDatabase.java | 13 ++++++++ 10 files changed, 117 insertions(+), 34 deletions(-) create mode 100644 src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java create mode 100644 src/main/java/net/pterodactylus/sone/data/impl/DefaultSoneBuilder.java create mode 100644 src/main/java/net/pterodactylus/sone/database/IdentityDatabase.java diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 3a02f15..63bf181 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -17,6 +17,7 @@ package net.pterodactylus.sone.core; +import static com.google.common.base.Optional.absent; import static com.google.common.base.Optional.of; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; @@ -74,6 +75,7 @@ import net.pterodactylus.sone.database.PostBuilder; import net.pterodactylus.sone.database.PostBuilder.PostCreated; import net.pterodactylus.sone.database.PostReplyBuilder; import net.pterodactylus.sone.database.PostReplyBuilder.PostReplyCreated; +import net.pterodactylus.sone.database.SoneBuilder.SoneCreated; import net.pterodactylus.sone.database.SoneProvider; import net.pterodactylus.sone.fcp.FcpInterface; import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired; @@ -557,7 +559,7 @@ public class Core extends AbstractService implements SoneProvider { synchronized (sones) { final Sone sone; try { - sone = database.newSoneBuilder().by(ownIdentity).local().build().setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri())); + sone = database.newSoneBuilder().by(ownIdentity.getId()).local().build(Optional.absent()).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri())); } catch (MalformedURLException mue1) { logger.log(Level.SEVERE, String.format("Could not convert the Identity’s URIs to Freenet URIs: %s, %s", ownIdentity.getInsertUri(), ownIdentity.getRequestUri()), mue1); return null; @@ -614,11 +616,10 @@ public class Core extends AbstractService implements SoneProvider { return null; } synchronized (sones) { - final Sone sone = database.newSoneBuilder().by(identity).build(); + final Sone sone = database.newSoneBuilder().by(identity.getId()).build(Optional.absent()); if (sone.isLocal()) { return sone; } - sone.setIdentity(identity); boolean newSone = sone.getRequestUri() == null; sone.setRequestUri(SoneUri.create(identity.getRequestUri())); sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0)); @@ -1728,7 +1729,6 @@ public class Core extends AbstractService implements SoneProvider { @SuppressWarnings("synthetic-access") public void run() { Optional sone = getRemoteSone(identity.getId()); - sone.get().setIdentity(identity); sone.get().setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), sone.get().getLatestEdition())); soneDownloader.addSone(sone.get()); soneDownloader.fetchSone(sone.get()); diff --git a/src/main/java/net/pterodactylus/sone/core/SoneParser.java b/src/main/java/net/pterodactylus/sone/core/SoneParser.java index 87d659f..d4e8078 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneParser.java @@ -90,7 +90,7 @@ public class SoneParser { return null; } - Sone sone = new DefaultSone(new MemoryDatabase(null), originalSone.getId(), originalSone.isLocal()).setIdentity(originalSone.getIdentity()); + Sone sone = new DefaultSone(new MemoryDatabase(null), originalSone.getId(), originalSone.isLocal()); SimpleXML soneXml; try { diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index e9ba3cf..7973001 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -178,18 +178,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable { Identity getIdentity(); /** - * Sets the identity of this Sone. The {@link Identity#getId() ID} of the - * identity has to match this Sone’s {@link #getId()}. - * - * @param identity - * The identity of this Sone - * @return This Sone (for method chaining) - * @throws IllegalArgumentException - * if the ID of the identity does not match this Sone’s ID - */ - Sone setIdentity(Identity identity) throws IllegalArgumentException; - - /** * Returns the name of this Sone. * * @return The name of this Sone diff --git a/src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java new file mode 100644 index 0000000..140fbd5 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java @@ -0,0 +1,37 @@ +package net.pterodactylus.sone.data.impl; + +import static com.google.common.base.Preconditions.checkState; + +import net.pterodactylus.sone.database.SoneBuilder; +import net.pterodactylus.sone.freenet.wot.Identity; + +import com.google.common.base.Preconditions; +import com.google.inject.internal.util.$Preconditions; + +/** + * Abstract {@link SoneBuilder} implementation. + * + * @author David ‘Bombe’ Roden + */ +public abstract class AbstractSoneBuilder implements SoneBuilder { + + protected String id; + protected boolean local; + + @Override + public SoneBuilder by(String id) { + this.id = id; + return this; + } + + @Override + public SoneBuilder local() { + local = true; + return this; + } + + protected void validate() throws IllegalStateException { + checkState(id != null, "id must not be null"); + } + +} diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java index 59964f9..ee3052d 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java @@ -68,9 +68,6 @@ public class DefaultSone implements Sone { /** Whether the Sone is local. */ private final boolean local; - /** The identity of this Sone. */ - private Identity identity; - /** The URI under which the Sone is stored in Freenet. */ private volatile FreenetURI requestUri; @@ -141,19 +138,11 @@ public class DefaultSone implements Sone { } public Identity getIdentity() { - return identity; - } - - public DefaultSone setIdentity(Identity identity) throws IllegalArgumentException { - if (!identity.getId().equals(id)) { - throw new IllegalArgumentException("Identity’s ID does not match Sone’s ID!"); - } - this.identity = identity; - return this; + return database.getIdentity(id).get(); } public String getName() { - return (identity != null) ? identity.getNickname() : null; + return getIdentity().getNickname(); } public boolean isLocal() { @@ -488,7 +477,7 @@ public class DefaultSone implements Sone { @Override public String toString() { - return getClass().getName() + "[identity=" + identity + ",requestUri=" + requestUri + ",insertUri(" + String.valueOf(insertUri).length() + "),friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]"; + return getClass().getName() + "[id=" + id + ",requestUri=" + requestUri + ",insertUri(" + String.valueOf(insertUri).length() + "),friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]"; } } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultSoneBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultSoneBuilder.java new file mode 100644 index 0000000..37a83fd --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultSoneBuilder.java @@ -0,0 +1,32 @@ +package net.pterodactylus.sone.data.impl; + +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.database.Database; +import net.pterodactylus.sone.database.SoneBuilder; + +import com.google.common.base.Optional; + +/** + * {@link SoneBuilder} implementation that can create {@link DefaultSone}s. + * + * @author David ‘Bombe’ Roden + */ +public class DefaultSoneBuilder extends AbstractSoneBuilder { + + private final Database database; + + public DefaultSoneBuilder(Database database) { + this.database = database; + } + + @Override + public Sone build(Optional soneCreated) throws IllegalStateException { + validate(); + Sone sone = new DefaultSone(database, id, local); + if (soneCreated.isPresent()) { + soneCreated.get().soneCreated(sone); + } + return sone; + } + +} diff --git a/src/main/java/net/pterodactylus/sone/database/Database.java b/src/main/java/net/pterodactylus/sone/database/Database.java index b43b2c4..c6ebc03 100644 --- a/src/main/java/net/pterodactylus/sone/database/Database.java +++ b/src/main/java/net/pterodactylus/sone/database/Database.java @@ -26,7 +26,7 @@ import com.google.common.util.concurrent.Service; * * @author David ‘Bombe’ Roden */ -public interface Database extends Service, SoneDatabase, PostDatabase, PostReplyDatabase, AlbumDatabase, ImageDatabase { +public interface Database extends Service, IdentityDatabase, SoneDatabase, PostDatabase, PostReplyDatabase, AlbumDatabase, ImageDatabase { /** * Saves the database. diff --git a/src/main/java/net/pterodactylus/sone/database/IdentityDatabase.java b/src/main/java/net/pterodactylus/sone/database/IdentityDatabase.java new file mode 100644 index 0000000..ac4c98b --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/database/IdentityDatabase.java @@ -0,0 +1,16 @@ +package net.pterodactylus.sone.database; + +import net.pterodactylus.sone.freenet.wot.Identity; + +import com.google.common.base.Optional; + +/** + * Database for handling {@link Identity}s. + * + * @author David ’Bombe‘ Roden + */ +public interface IdentityDatabase { + + Optional getIdentity(String identityId); + +} diff --git a/src/main/java/net/pterodactylus/sone/database/SoneBuilder.java b/src/main/java/net/pterodactylus/sone/database/SoneBuilder.java index 2aca8b9..d0b38af 100644 --- a/src/main/java/net/pterodactylus/sone/database/SoneBuilder.java +++ b/src/main/java/net/pterodactylus/sone/database/SoneBuilder.java @@ -20,6 +20,8 @@ package net.pterodactylus.sone.database; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.freenet.wot.Identity; +import com.google.common.base.Optional; + /** * Builder for {@link Sone} objects. * @@ -27,9 +29,15 @@ import net.pterodactylus.sone.freenet.wot.Identity; */ public interface SoneBuilder { - SoneBuilder by(Identity identity); + SoneBuilder by(String id); SoneBuilder local(); - Sone build() throws IllegalStateException; + Sone build(Optional soneCreated) throws IllegalStateException; + + interface SoneCreated { + + void soneCreated(Sone createdSone); + + } } diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java index 8cff1be..d7cef49 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -48,6 +48,7 @@ import net.pterodactylus.sone.database.Database; import net.pterodactylus.sone.database.DatabaseException; import net.pterodactylus.sone.database.PostDatabase; import net.pterodactylus.sone.database.SoneBuilder; +import net.pterodactylus.sone.freenet.wot.Identity; import net.pterodactylus.util.config.Configuration; import net.pterodactylus.util.config.ConfigurationException; @@ -55,6 +56,7 @@ import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ListMultimap; +import com.google.common.collect.Maps; import com.google.common.collect.SortedSetMultimap; import com.google.common.collect.TreeMultimap; import com.google.common.util.concurrent.AbstractService; @@ -73,6 +75,7 @@ public class MemoryDatabase extends AbstractService implements Database { /** The configuration. */ private final Configuration configuration; + private final Map identities = Maps.newHashMap(); private final Map sones = new HashMap(); /** All posts by their ID. */ @@ -154,6 +157,16 @@ public class MemoryDatabase extends AbstractService implements Database { } @Override + public Optional getIdentity(String identityId) { + lock.readLock().lock(); + try { + return fromNullable(identities.get(identityId)); + } finally { + lock.readLock().unlock(); + } + } + + @Override public Optional getSone(String soneId) { lock.readLock().lock(); try { -- 2.7.4