From 17bc7628a552608caa3bc2057428cdc80535444b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 26 Nov 2014 06:35:21 +0100 Subject: [PATCH] Use database to return friends of a Sone. --- .../java/net/pterodactylus/sone/data/impl/SoneImpl.java | 17 ++++++++++------- .../sone/database/memory/MemoryDatabase.java | 2 +- .../sone/database/memory/MemorySoneBuilder.java | 9 ++++++++- .../net/pterodactylus/sone/core/SoneParserTest.java | 2 +- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java index 4c9bcae..2fdd40c 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java @@ -40,6 +40,7 @@ import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.SoneOptions; import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions; +import net.pterodactylus.sone.database.Database; import net.pterodactylus.sone.freenet.wot.Identity; import net.pterodactylus.sone.freenet.wot.OwnIdentity; @@ -60,6 +61,9 @@ public class SoneImpl implements Sone { /** The logger. */ private static final Logger logger = getLogger("Sone.Data"); + /** The database. */ + private final Database database; + /** The ID of this Sone. */ private final String id; @@ -87,9 +91,6 @@ public class SoneImpl implements Sone { /** Whether this Sone is known. */ private volatile boolean known; - /** All friend Sones. */ - private final Set friendSones = new CopyOnWriteArraySet(); - /** All posts. */ private final Set posts = new CopyOnWriteArraySet(); @@ -111,12 +112,14 @@ public class SoneImpl implements Sone { /** * Creates a new Sone. * + * @param database The database * @param identity * The identity of the Sone * @param local * {@code true} if the Sone is a local Sone, {@code false} otherwise */ - public SoneImpl(Identity identity, boolean local) { + public SoneImpl(Database database, Identity identity, boolean local) { + this.database = database; this.id = identity.getId(); this.identity = identity; this.local = local; @@ -340,7 +343,7 @@ public class SoneImpl implements Sone { * @return The friend Sones of this Sone */ public Collection getFriends() { - return new ArrayList(friendSones); + return database.getFriends(this); } /** @@ -352,7 +355,7 @@ public class SoneImpl implements Sone { * false} otherwise */ public boolean hasFriend(String friendSoneId) { - return friendSones.contains(friendSoneId); + return database.isFriend(this, friendSoneId); } /** @@ -685,7 +688,7 @@ public class SoneImpl implements Sone { /** {@inheritDoc} */ @Override public String toString() { - return getClass().getName() + "[identity=" + identity + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]"; + return getClass().getName() + "[identity=" + identity + ",posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]"; } } 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 127b90c..b830ba5 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -181,7 +181,7 @@ public class MemoryDatabase extends AbstractService implements Database { @Override public SoneBuilder newSoneBuilder() { - return new MemorySoneBuilder(); + return new MemorySoneBuilder(this); } @Override diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneBuilder.java b/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneBuilder.java index c537bcb..49531a1 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneBuilder.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneBuilder.java @@ -3,6 +3,7 @@ package net.pterodactylus.sone.database.memory; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.impl.SoneImpl; import net.pterodactylus.sone.data.impl.AbstractSoneBuilder; +import net.pterodactylus.sone.database.Database; /** * Memory-based {@link AbstractSoneBuilder} implementation. @@ -11,10 +12,16 @@ import net.pterodactylus.sone.data.impl.AbstractSoneBuilder; */ public class MemorySoneBuilder extends AbstractSoneBuilder { + private final Database database; + + public MemorySoneBuilder(Database database) { + this.database = database; + } + @Override public Sone build() throws IllegalStateException { validate(); - return new SoneImpl(identity, local); + return new SoneImpl(database, identity, local); } } diff --git a/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java b/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java index b6560f4..dc19195 100644 --- a/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java +++ b/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java @@ -114,7 +114,7 @@ public class SoneParserTest { when(core.soneBuilder()).thenAnswer(new Answer() { @Override public SoneBuilder answer(InvocationOnMock invocation) { - return new MemorySoneBuilder(); + return new MemorySoneBuilder(null); } }); } -- 2.7.4