X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=140d80ce354353c38e9cfd0078640648ea4a8846;hb=00a434a23c9ea1e57c63d8a3c0fc4b09277af431;hp=b25d8e3e1f6ea76fa58683b6b9684ef92b379e3c;hpb=73ca2937b9fe41f637e22d721f54a8cefb75b580;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java
index b25d8e3..140d80c 100644
--- a/src/main/java/net/pterodactylus/sone/core/Core.java
+++ b/src/main/java/net/pterodactylus/sone/core/Core.java
@@ -1,5 +1,5 @@
/*
- * Sone - Core.java - Copyright © 2010â2012 David Roden
+ * Sone - Core.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,9 +17,17 @@
package net.pterodactylus.sone.core;
-import java.net.MalformedURLException;
+import static com.google.common.base.Optional.fromNullable;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.primitives.Longs.tryParse;
+import static java.lang.String.format;
+import static java.util.logging.Level.WARNING;
+import static java.util.logging.Logger.getLogger;
+
import java.util.ArrayList;
-import java.util.Collections;
+import java.util.Collection;
+import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -28,12 +36,30 @@ import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
-import net.pterodactylus.sone.core.Options.DefaultOption;
-import net.pterodactylus.sone.core.Options.Option;
-import net.pterodactylus.sone.core.Options.OptionWatcher;
+import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidAlbumFound;
+import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidImageFound;
+import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidParentAlbumFound;
+import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostFound;
+import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostReplyFound;
+import net.pterodactylus.sone.core.SoneChangeDetector.PostProcessor;
+import net.pterodactylus.sone.core.SoneChangeDetector.PostReplyProcessor;
+import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
+import net.pterodactylus.sone.core.event.MarkPostKnownEvent;
+import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent;
+import net.pterodactylus.sone.core.event.MarkSoneKnownEvent;
+import net.pterodactylus.sone.core.event.NewPostFoundEvent;
+import net.pterodactylus.sone.core.event.NewPostReplyFoundEvent;
+import net.pterodactylus.sone.core.event.NewSoneFoundEvent;
+import net.pterodactylus.sone.core.event.PostRemovedEvent;
+import net.pterodactylus.sone.core.event.PostReplyRemovedEvent;
+import net.pterodactylus.sone.core.event.SoneLockedEvent;
+import net.pterodactylus.sone.core.event.SoneRemovedEvent;
+import net.pterodactylus.sone.core.event.SoneUnlockedEvent;
import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.Client;
import net.pterodactylus.sone.data.Image;
@@ -46,52 +72,66 @@ import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
import net.pterodactylus.sone.data.Sone.SoneStatus;
import net.pterodactylus.sone.data.TemporaryImage;
-import net.pterodactylus.sone.data.impl.PostImpl;
-import net.pterodactylus.sone.fcp.FcpInterface;
-import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
+import net.pterodactylus.sone.database.AlbumBuilder;
+import net.pterodactylus.sone.database.AlbumProvider;
+import net.pterodactylus.sone.database.Database;
+import net.pterodactylus.sone.database.DatabaseException;
+import net.pterodactylus.sone.database.ImageBuilder;
+import net.pterodactylus.sone.database.PostBuilder;
+import net.pterodactylus.sone.database.PostProvider;
+import net.pterodactylus.sone.database.PostReplyBuilder;
+import net.pterodactylus.sone.database.PostReplyProvider;
+import net.pterodactylus.sone.database.SoneBuilder;
+import net.pterodactylus.sone.database.SoneProvider;
import net.pterodactylus.sone.freenet.wot.Identity;
-import net.pterodactylus.sone.freenet.wot.IdentityListener;
import net.pterodactylus.sone.freenet.wot.IdentityManager;
import net.pterodactylus.sone.freenet.wot.OwnIdentity;
+import net.pterodactylus.sone.freenet.wot.event.IdentityAddedEvent;
+import net.pterodactylus.sone.freenet.wot.event.IdentityRemovedEvent;
+import net.pterodactylus.sone.freenet.wot.event.IdentityUpdatedEvent;
+import net.pterodactylus.sone.freenet.wot.event.OwnIdentityAddedEvent;
+import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent;
import net.pterodactylus.sone.main.SonePlugin;
import net.pterodactylus.util.config.Configuration;
import net.pterodactylus.util.config.ConfigurationException;
-import net.pterodactylus.util.logging.Logging;
-import net.pterodactylus.util.number.Numbers;
import net.pterodactylus.util.service.AbstractService;
import net.pterodactylus.util.thread.NamedThreadFactory;
-import net.pterodactylus.util.thread.Ticker;
-import net.pterodactylus.util.validation.EqualityValidator;
-import net.pterodactylus.util.validation.IntegerRangeValidator;
-import net.pterodactylus.util.validation.OrValidator;
-import net.pterodactylus.util.validation.Validation;
-import net.pterodactylus.util.version.Version;
-import freenet.keys.FreenetURI;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Function;
+import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Multimaps;
+import com.google.common.eventbus.EventBus;
+import com.google.common.eventbus.Subscribe;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
/**
* The Sone core.
*
* @author David âBombeâ Roden
*/
-public class Core extends AbstractService implements IdentityListener, UpdateListener, SoneProvider, PostProvider, SoneInsertListener, ImageInsertListener {
+@Singleton
+public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider, AlbumProvider {
/** The logger. */
- private static final Logger logger = Logging.getLogger(Core.class);
+ private static final Logger logger = getLogger(Core.class.getName());
/** The start time. */
private final long startupTime = System.currentTimeMillis();
- /** The options. */
- private final Options options = new Options();
-
/** The preferences. */
- private final Preferences preferences = new Preferences(options);
+ private final Preferences preferences;
- /** The core listener manager. */
- private final CoreListenerManager coreListenerManager = new CoreListenerManager(this);
+ /** The event bus. */
+ private final EventBus eventBus;
/** The configuration. */
- private Configuration configuration;
+ private final Configuration configuration;
/** Whether weâre currently saving the configuration. */
private boolean storingConfiguration = false;
@@ -117,65 +157,37 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
/** The trust updater. */
private final WebOfTrustUpdater webOfTrustUpdater;
- /** The FCP interface. */
- private volatile FcpInterface fcpInterface;
+ private final Set compatibilityModes = EnumSet.noneOf(CompatibilityMode.class);
/** The times Sones were followed. */
- private final Map soneFollowingTimes = new HashMap();
+ private final Map soneFollowingTimes = new HashMap();
/** Locked local Sones. */
/* synchronize on itself. */
private final Set lockedSones = new HashSet();
/** Sone inserters. */
- /* synchronize access on this on localSones. */
+ /* synchronize access on this on sones. */
private final Map soneInserters = new HashMap();
/** Sone rescuers. */
- /* synchronize access on this on localSones. */
+ /* synchronize access on this on sones. */
private final Map soneRescuers = new HashMap();
- /** All local Sones. */
- /* synchronize access on this on itself. */
- private final Map localSones = new HashMap();
-
- /** All remote Sones. */
- /* synchronize access on this on itself. */
- private final Map remoteSones = new HashMap();
-
/** All known Sones. */
private final Set knownSones = new HashSet();
- /** All posts. */
- private final Map posts = new HashMap();
-
- /** All known posts. */
- private final Set knownPosts = new HashSet();
-
- /** All replies. */
- private final Map replies = new HashMap();
-
- /** All known replies. */
- private final Set knownReplies = new HashSet();
-
- /** All bookmarked posts. */
- /* synchronize access on itself. */
- private final Set bookmarkedPosts = new HashSet();
+ /** The post database. */
+ private final Database database;
/** Trusted identities, sorted by own identities. */
- private final Map> trustedIdentities = Collections.synchronizedMap(new HashMap>());
-
- /** All known albums. */
- private final Map albums = new HashMap();
-
- /** All known images. */
- private final Map images = new HashMap();
+ private final Multimap trustedIdentities = Multimaps.synchronizedSetMultimap(HashMultimap.create());
/** All temporary images. */
private final Map temporaryImages = new HashMap();
/** Ticker for threads that mark own elements as known. */
- private final Ticker localElementTicker = new Ticker();
+ private final ScheduledExecutorService localElementTicker = Executors.newScheduledThreadPool(1);
/** The time the configuration was last touched. */
private volatile long lastConfigurationUpdate;
@@ -191,40 +203,39 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
* The identity manager
* @param webOfTrustUpdater
* The WebOfTrust updater
+ * @param eventBus
+ * The event bus
+ * @param database
+ * The database
*/
- public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, WebOfTrustUpdater webOfTrustUpdater) {
+ @Inject
+ public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) {
super("Sone Core");
this.configuration = configuration;
this.freenetInterface = freenetInterface;
this.identityManager = identityManager;
- this.soneDownloader = new SoneDownloader(this, freenetInterface);
- this.imageInserter = new ImageInserter(this, freenetInterface);
- this.updateChecker = new UpdateChecker(freenetInterface);
+ this.soneDownloader = new SoneDownloaderImpl(this, freenetInterface);
+ this.imageInserter = new ImageInserter(freenetInterface, freenetInterface.new InsertTokenSupplier());
+ this.updateChecker = new UpdateChecker(eventBus, freenetInterface);
this.webOfTrustUpdater = webOfTrustUpdater;
+ this.eventBus = eventBus;
+ this.database = database;
+ preferences = new Preferences(eventBus);
}
- //
- // LISTENER MANAGEMENT
- //
-
- /**
- * Adds a new core listener.
- *
- * @param coreListener
- * The listener to add
- */
- public void addCoreListener(CoreListener coreListener) {
- coreListenerManager.addListener(coreListener);
- }
-
- /**
- * Removes a core listener.
- *
- * @param coreListener
- * The listener to remove
- */
- public void removeCoreListener(CoreListener coreListener) {
- coreListenerManager.removeListener(coreListener);
+ @VisibleForTesting
+ protected Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, SoneDownloader soneDownloader, ImageInserter imageInserter, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) {
+ super("Sone Core");
+ this.configuration = configuration;
+ this.freenetInterface = freenetInterface;
+ this.identityManager = identityManager;
+ this.soneDownloader = soneDownloader;
+ this.imageInserter = imageInserter;
+ this.updateChecker = updateChecker;
+ this.webOfTrustUpdater = webOfTrustUpdater;
+ this.eventBus = eventBus;
+ this.database = database;
+ preferences = new Preferences(eventBus);
}
//
@@ -241,18 +252,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
}
/**
- * Sets the configuration to use. This will automatically save the current
- * configuration to the given configuration.
- *
- * @param configuration
- * The new configuration to use
- */
- public void setConfiguration(Configuration configuration) {
- this.configuration = configuration;
- touchConfiguration();
- }
-
- /**
* Returns the options used by the core.
*
* @return The options of the core
@@ -279,14 +278,16 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
return updateChecker;
}
- /**
- * Sets the FCP interface to use.
- *
- * @param fcpInterface
- * The FCP interface to use
- */
- public void setFcpInterface(FcpInterface fcpInterface) {
- this.fcpInterface = fcpInterface;
+ public boolean isCompatibilityMode(CompatibilityMode compatibilityMode) {
+ return compatibilityModes.contains(compatibilityMode);
+ }
+
+ public void setCompatibilityMode(CompatibilityMode compatibilityMode) {
+ compatibilityModes.add(compatibilityMode);
+ }
+
+ public void clearCompatibilityMod(CompatibilityMode compatibilityMode) {
+ compatibilityModes.remove(compatibilityMode);
}
/**
@@ -297,8 +298,9 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
* @return The Sone rescuer for the given Sone
*/
public SoneRescuer getSoneRescuer(Sone sone) {
- Validation.begin().isNotNull("Sone", sone).check().is("Local Sone", isLocalSone(sone)).check();
- synchronized (localSones) {
+ checkNotNull(sone, "sone must not be null");
+ checkArgument(sone.isLocal(), "sone must be local");
+ synchronized (soneRescuers) {
SoneRescuer soneRescuer = soneRescuers.get(sone);
if (soneRescuer == null) {
soneRescuer = new SoneRescuer(this, soneDownloader, sone);
@@ -322,29 +324,21 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
}
}
- /**
- * Returns all Sones, remote and local.
- *
- * @return All Sones
- */
- public Set getSones() {
- Set allSones = new HashSet();
- allSones.addAll(getLocalSones());
- allSones.addAll(getRemoteSones());
- return allSones;
+ public SoneBuilder soneBuilder() {
+ return database.newSoneBuilder();
}
/**
- * Returns the Sone with the given ID, regardless whether itâs local or
- * remote.
- *
- * @param id
- * The ID of the Sone to get
- * @return The Sone with the given ID, or {@code null} if there is no such
- * Sone
+ * {@inheritDocs}
*/
- public Sone getSone(String id) {
- return getSone(id, true);
+ @Override
+ public Collection getSones() {
+ return database.getSones();
+ }
+
+ @Override
+ public Function> soneLoader() {
+ return database.soneLoader();
}
/**
@@ -353,80 +347,20 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
*
* @param id
* The ID of the Sone to get
- * @param create
- * {@code true} to create a new Sone if none exists,
- * {@code false} to return {@code null} if a Sone with the given
- * ID does not exist
* @return The Sone with the given ID, or {@code null} if there is no such
* Sone
*/
@Override
- public Sone getSone(String id, boolean create) {
- if (isLocalSone(id)) {
- return getLocalSone(id);
- }
- return getRemoteSone(id, create);
- }
-
- /**
- * Checks whether the core knows a Sone with the given ID.
- *
- * @param id
- * The ID of the Sone
- * @return {@code true} if there is a Sone with the given ID, {@code false}
- * otherwise
- */
- public boolean hasSone(String id) {
- return isLocalSone(id) || isRemoteSone(id);
- }
-
- /**
- * Returns whether the given Sone is a local Sone.
- *
- * @param sone
- * The Sone to check for its locality
- * @return {@code true} if the given Sone is local, {@code false} otherwise
- */
- public boolean isLocalSone(Sone sone) {
- synchronized (localSones) {
- return localSones.containsKey(sone.getId());
- }
- }
-
- /**
- * Returns whether the given ID is the ID of a local Sone.
- *
- * @param id
- * The Sone ID to check for its locality
- * @return {@code true} if the given ID is a local Sone, {@code false}
- * otherwise
- */
- public boolean isLocalSone(String id) {
- synchronized (localSones) {
- return localSones.containsKey(id);
- }
- }
-
- /**
- * Returns all local Sones.
- *
- * @return All local Sones
- */
- public Set getLocalSones() {
- synchronized (localSones) {
- return new HashSet(localSones.values());
- }
+ public Optional getSone(String id) {
+ return database.getSone(id);
}
/**
- * Returns the local Sone with the given ID.
- *
- * @param id
- * The ID of the Sone to get
- * @return The Sone with the given ID
+ * {@inheritDocs}
*/
- public Sone getLocalSone(String id) {
- return getLocalSone(id, true);
+ @Override
+ public Collection getLocalSones() {
+ return database.getLocalSones();
}
/**
@@ -434,80 +368,34 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
*
* @param id
* The ID of the Sone
- * @param create
- * {@code true} to create a new Sone if none exists,
- * {@code false} to return null if none exists
* @return The Sone with the given ID, or {@code null}
*/
- public Sone getLocalSone(String id, boolean create) {
- synchronized (localSones) {
- Sone sone = localSones.get(id);
- if ((sone == null) && create) {
- sone = new Sone(id);
- localSones.put(id, sone);
- }
- return sone;
+ public Sone getLocalSone(String id) {
+ Optional sone = database.getSone(id);
+ if (sone.isPresent() && sone.get().isLocal()) {
+ return sone.get();
}
+ return null;
}
/**
- * Returns all remote Sones.
- *
- * @return All remote Sones
+ * {@inheritDocs}
*/
- public Set getRemoteSones() {
- synchronized (remoteSones) {
- return new HashSet(remoteSones.values());
- }
+ @Override
+ public Collection getRemoteSones() {
+ return database.getRemoteSones();
}
/**
* Returns the remote Sone with the given ID.
*
+ *
* @param id
* The ID of the remote Sone to get
- * @param create
- * {@code true} to always create a Sone, {@code false} to return
- * {@code null} if no Sone with the given ID exists
* @return The Sone with the given ID
*/
- public Sone getRemoteSone(String id, boolean create) {
- synchronized (remoteSones) {
- Sone sone = remoteSones.get(id);
- if ((sone == null) && create && (id != null) && (id.length() == 43)) {
- sone = new Sone(id);
- remoteSones.put(id, sone);
- }
- return sone;
- }
- }
-
- /**
- * Returns whether the given Sone is a remote Sone.
- *
- * @param sone
- * The Sone to check
- * @return {@code true} if the given Sone is a remote Sone, {@code false}
- * otherwise
- */
- public boolean isRemoteSone(Sone sone) {
- synchronized (remoteSones) {
- return remoteSones.containsKey(sone.getId());
- }
- }
-
- /**
- * Returns whether the Sone with the given ID is a remote Sone.
- *
- * @param id
- * The ID of the Sone to check
- * @return {@code true} if the Sone with the given ID is a remote Sone,
- * {@code false} otherwise
- */
- public boolean isRemoteSone(String id) {
- synchronized (remoteSones) {
- return remoteSones.containsKey(id);
- }
+ public Sone getRemoteSone(String id) {
+ return database.getSone(id).orNull();
}
/**
@@ -519,7 +407,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
* {@code false} otherwise
*/
public boolean isModifiedSone(Sone sone) {
- return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
+ return soneInserters.containsKey(sone) && soneInserters.get(sone).isModified();
}
/**
@@ -532,123 +420,76 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
*/
public long getSoneFollowingTime(Sone sone) {
synchronized (soneFollowingTimes) {
- if (soneFollowingTimes.containsKey(sone)) {
- return soneFollowingTimes.get(sone);
- }
- return Long.MAX_VALUE;
+ return Optional.fromNullable(soneFollowingTimes.get(sone.getId())).or(Long.MAX_VALUE);
}
}
/**
- * Returns whether the target Sone is trusted by the origin Sone.
+ * Returns a post builder.
*
- * @param origin
- * The origin Sone
- * @param target
- * The target Sone
- * @return {@code true} if the target Sone is trusted by the origin Sone
+ * @return A new post builder
*/
- public boolean isSoneTrusted(Sone origin, Sone target) {
- Validation.begin().isNotNull("Origin", origin).isNotNull("Target", target).check().isInstanceOf("Originâs OwnIdentity", origin.getIdentity(), OwnIdentity.class).check();
- return trustedIdentities.containsKey(origin.getIdentity()) && trustedIdentities.get(origin.getIdentity()).contains(target.getIdentity());
+ public PostBuilder postBuilder() {
+ return database.newPostBuilder();
}
/**
- * Returns the post with the given ID.
- *
- * @param postId
- * The ID of the post to get
- * @return The post with the given ID, or a new post with the given ID
+ * {@inheritDoc}
+ */
+ @Override
+ public Optional getPost(final String postId) {
+ Optional post = database.getPost(postId);
+ if (post.isPresent() || !isCompatibilityMode(CompatibilityMode.oldElementIds)) {
+ return post;
+ }
+ return FluentIterable.from(getSones()).transformAndConcat(Sone.toAllPosts).filter(new Predicate() {
+ @Override
+ public boolean apply(Post input) {
+ return (input != null) && input.getInternalId().equals(postId);
+ }
+ }).first();
+ }
+
+ /**
+ * {@inheritDocs}
*/
- public Post getPost(String postId) {
- return getPost(postId, true);
+ @Override
+ public Collection getPosts(String soneId) {
+ return database.getPosts(soneId);
}
/**
- * Returns the post with the given ID, optionally creating a new post.
- *
- * @param postId
- * The ID of the post to get
- * @param create
- * {@code true} it create a new post if no post with the given ID
- * exists, {@code false} to return {@code null}
- * @return The post, or {@code null} if there is no such post
+ * {@inheritDoc}
*/
@Override
- public Post getPost(String postId, boolean create) {
- synchronized (posts) {
- Post post = posts.get(postId);
- if ((post == null) && create) {
- post = new PostImpl(postId);
- posts.put(postId, post);
- }
- return post;
- }
+ public Collection getDirectedPosts(final String recipientId) {
+ checkNotNull(recipientId, "recipient must not be null");
+ return database.getDirectedPosts(recipientId);
}
/**
- * Returns all posts that have the given Sone as recipient.
+ * Returns a post reply builder.
*
- * @see Post#getRecipient()
- * @param recipient
- * The recipient of the posts
- * @return All posts that have the given Sone as recipient
- */
- public Set getDirectedPosts(Sone recipient) {
- Validation.begin().isNotNull("Recipient", recipient).check();
- Set directedPosts = new HashSet();
- synchronized (posts) {
- for (Post post : posts.values()) {
- if (recipient.equals(post.getRecipient())) {
- directedPosts.add(post);
- }
- }
- }
- return directedPosts;
+ * @return A new post reply builder
+ */
+ public PostReplyBuilder postReplyBuilder() {
+ return database.newPostReplyBuilder();
}
/**
- * Returns the reply with the given ID. If there is no reply with the given
- * ID yet, a new one is created, unless {@code create} is false in which
- * case {@code null} is returned.
- *
- * @param replyId
- * The ID of the reply to get
- * @param create
- * {@code true} to always return a {@link Reply}, {@code false}
- * to return {@code null} if no reply can be found
- * @return The reply, or {@code null} if there is no such reply
- */
- public PostReply getPostReply(String replyId, boolean create) {
- synchronized (replies) {
- PostReply reply = replies.get(replyId);
- if (create && (reply == null)) {
- reply = new PostReply(replyId);
- replies.put(replyId, reply);
- }
- return reply;
- }
+ * {@inheritDoc}
+ */
+ @Override
+ public Optional getPostReply(String replyId) {
+ return database.getPostReply(replyId);
}
/**
- * Returns all replies for the given post, order ascending by time.
- *
- * @param post
- * The post to get all replies for
- * @return All replies for the given post
+ * {@inheritDoc}
*/
- public List getReplies(Post post) {
- Set sones = getSones();
- List replies = new ArrayList();
- for (Sone sone : sones) {
- for (PostReply reply : sone.getReplies()) {
- if (reply.getPost().equals(post)) {
- replies.add(reply);
- }
- }
- }
- Collections.sort(replies, Reply.TIME_COMPARATOR);
- return replies;
+ @Override
+ public List getReplies(final String postId) {
+ return database.getReplies(postId);
}
/**
@@ -694,21 +535,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
* otherwise
*/
public boolean isBookmarked(Post post) {
- return isPostBookmarked(post.getId());
- }
-
- /**
- * Returns whether the post with the given ID is bookmarked.
- *
- * @param id
- * The ID of the post to check
- * @return {@code true} if the post with the given ID is bookmarked,
- * {@code false} otherwise
- */
- public boolean isPostBookmarked(String id) {
- synchronized (bookmarkedPosts) {
- return bookmarkedPosts.contains(id);
- }
+ return database.isPostBookmarked(post);
}
/**
@@ -717,28 +544,11 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
* @return All bookmarked posts
*/
public Set getBookmarkedPosts() {
- Set posts = new HashSet();
- synchronized (bookmarkedPosts) {
- for (String bookmarkedPostId : bookmarkedPosts) {
- Post post = getPost(bookmarkedPostId, false);
- if (post != null) {
- posts.add(post);
- }
- }
- }
- return posts;
+ return database.getBookmarkedPosts();
}
- /**
- * Returns the album with the given ID, creating a new album if no album
- * with the given ID can be found.
- *
- * @param albumId
- * The ID of the album
- * @return The album with the given ID
- */
- public Album getAlbum(String albumId) {
- return getAlbum(albumId, true);
+ public AlbumBuilder albumBuilder() {
+ return database.newAlbumBuilder();
}
/**
@@ -747,21 +557,15 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
*
* @param albumId
* The ID of the album
- * @param create
- * {@code true} to create a new album if none exists for the
- * given ID
* @return The album with the given ID, or {@code null} if no album with the
- * given ID exists and {@code create} is {@code false}
- */
- public Album getAlbum(String albumId, boolean create) {
- synchronized (albums) {
- Album album = albums.get(albumId);
- if (create && (album == null)) {
- album = new Album(albumId);
- albums.put(albumId, album);
- }
- return album;
- }
+ * given ID exists
+ */
+ public Optional getAlbum(String albumId) {
+ return database.getAlbum(albumId);
+ }
+
+ public ImageBuilder imageBuilder() {
+ return database.newImageBuilder();
}
/**
@@ -788,14 +592,16 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
* none was created
*/
public Image getImage(String imageId, boolean create) {
- synchronized (images) {
- Image image = images.get(imageId);
- if (create && (image == null)) {
- image = new Image(imageId);
- images.put(imageId, image);
- }
- return image;
+ Optional image = database.getImage(imageId);
+ if (image.isPresent()) {
+ return image.get();
+ }
+ if (!create) {
+ return null;
}
+ Image newImage = database.newImageBuilder().withId(imageId).build();
+ database.storeImage(newImage);
+ return newImage;
}
/**
@@ -827,7 +633,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
public void lockSone(Sone sone) {
synchronized (lockedSones) {
if (lockedSones.add(sone)) {
- coreListenerManager.fireSoneLocked(sone);
+ eventBus.post(new SoneLockedEvent(sone));
}
}
}
@@ -842,7 +648,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
public void unlockSone(Sone sone) {
synchronized (lockedSones) {
if (lockedSones.remove(sone)) {
- coreListenerManager.fireSoneUnlocked(sone);
+ eventBus.post(new SoneUnlockedEvent(sone));
}
}
}
@@ -859,27 +665,22 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
logger.log(Level.WARNING, "Given OwnIdentity is null!");
return null;
}
- synchronized (localSones) {
- final Sone sone;
- try {
- sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).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;
- }
- sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
- sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
- sone.setKnown(true);
- /* TODO - load posts ân stuff */
- localSones.put(ownIdentity.getId(), sone);
- final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
- soneInserter.addSoneInsertListener(this);
+ logger.info(String.format("Adding Sone from OwnIdentity: %s", ownIdentity));
+ Sone sone = database.newSoneBuilder().local().from(ownIdentity).build();
+ String property = fromNullable(ownIdentity.getProperty("Sone.LatestEdition")).or("0");
+ sone.setLatestEdition(fromNullable(tryParse(property)).or(0L));
+ sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
+ sone.setKnown(true);
+ SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, ownIdentity.getId());
+ eventBus.register(soneInserter);
+ synchronized (soneInserters) {
soneInserters.put(sone, soneInserter);
- sone.setStatus(SoneStatus.idle);
- loadSone(sone);
- soneInserter.start();
- return sone;
}
+ loadSone(sone);
+ database.storeSone(sone);
+ sone.setStatus(SoneStatus.idle);
+ soneInserter.start();
+ return sone;
}
/**
@@ -895,14 +696,8 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
return null;
}
Sone sone = addLocalSone(ownIdentity);
- sone.getOptions().addBooleanOption("AutoFollow", new DefaultOption(false));
- sone.getOptions().addBooleanOption("EnableSoneInsertNotifications", new DefaultOption(false));
- sone.getOptions().addBooleanOption("ShowNotification/NewSones", new DefaultOption(true));
- sone.getOptions().addBooleanOption("ShowNotification/NewPosts", new DefaultOption(true));
- sone.getOptions().addBooleanOption("ShowNotification/NewReplies", new DefaultOption(true));
- sone.getOptions().addEnumOption("ShowCustomAvatars", new DefaultOption(ShowCustomAvatars.NEVER));
-
- followSone(sone, getSone("nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI"));
+
+ followSone(sone, "nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI");
touchConfiguration();
return sone;
}
@@ -919,37 +714,33 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
logger.log(Level.WARNING, "Given Identity is null!");
return null;
}
- synchronized (remoteSones) {
- final Sone sone = getRemoteSone(identity.getId(), true).setIdentity(identity);
- boolean newSone = sone.getRequestUri() == null;
- sone.setRequestUri(getSoneUri(identity.getRequestUri()));
- sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
+ String property = fromNullable(identity.getProperty("Sone.LatestEdition")).or("0");
+ long latestEdition = fromNullable(tryParse(property)).or(0L);
+ Optional existingSone = getSone(identity.getId());
+ if (existingSone.isPresent() && existingSone.get().isLocal()) {
+ return existingSone.get();
+ }
+ boolean newSone = !existingSone.isPresent();
+ Sone sone = !newSone ? existingSone.get() : database.newSoneBuilder().from(identity).build();
+ sone.setLatestEdition(latestEdition);
+ if (newSone) {
+ synchronized (knownSones) {
+ newSone = !knownSones.contains(sone.getId());
+ }
+ sone.setKnown(!newSone);
if (newSone) {
- synchronized (knownSones) {
- newSone = !knownSones.contains(sone.getId());
- }
- sone.setKnown(!newSone);
- if (newSone) {
- coreListenerManager.fireNewSoneFound(sone);
- for (Sone localSone : getLocalSones()) {
- if (localSone.getOptions().getBooleanOption("AutoFollow").get()) {
- followSone(localSone, sone);
- }
+ eventBus.post(new NewSoneFoundEvent(sone));
+ for (Sone localSone : getLocalSones()) {
+ if (localSone.getOptions().isAutoFollow()) {
+ followSone(localSone, sone.getId());
}
}
}
- soneDownloader.addSone(sone);
- soneDownloaders.execute(new Runnable() {
-
- @Override
- @SuppressWarnings("synthetic-access")
- public void run() {
- soneDownloader.fetchSone(sone, sone.getRequestUri());
- }
-
- });
- return sone;
}
+ database.storeSone(sone);
+ soneDownloader.addSone(sone);
+ soneDownloaders.execute(soneDownloader.fetchSoneWithUriAction(sone));
+ return sone;
}
/**
@@ -961,39 +752,23 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
* The ID of the Sone to follow
*/
public void followSone(Sone sone, String soneId) {
- Validation.begin().isNotNull("Sone", sone).isNotNull("Sone ID", soneId).check();
- Sone followedSone = getSone(soneId, true);
- if (followedSone == null) {
- logger.log(Level.INFO, String.format("Ignored Sone with invalid ID: %s", soneId));
- return;
- }
- followSone(sone, getSone(soneId));
- }
-
- /**
- * Lets the given local Sone follow the other given Sone. If the given Sone
- * was not followed by any local Sone before, this will mark all elements of
- * the followed Sone as read that have been created before the current
- * moment.
- *
- * @param sone
- * The local Sone that should follow the other Sone
- * @param followedSone
- * The Sone that should be followed
- */
- public void followSone(Sone sone, Sone followedSone) {
- Validation.begin().isNotNull("Sone", sone).isNotNull("Followed Sone", followedSone).check();
- sone.addFriend(followedSone.getId());
+ checkNotNull(sone, "sone must not be null");
+ checkNotNull(soneId, "soneId must not be null");
+ database.addFriend(sone, soneId);
synchronized (soneFollowingTimes) {
- if (!soneFollowingTimes.containsKey(followedSone)) {
+ if (!soneFollowingTimes.containsKey(soneId)) {
long now = System.currentTimeMillis();
- soneFollowingTimes.put(followedSone, now);
- for (Post post : followedSone.getPosts()) {
+ soneFollowingTimes.put(soneId, now);
+ Optional followedSone = getSone(soneId);
+ if (!followedSone.isPresent()) {
+ return;
+ }
+ for (Post post : followedSone.get().getPosts()) {
if (post.getTime() < now) {
markPostKnown(post);
}
}
- for (PostReply reply : followedSone.getReplies()) {
+ for (PostReply reply : followedSone.get().getReplies()) {
if (reply.getTime() < now) {
markReplyKnown(reply);
}
@@ -1012,30 +787,16 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
* The ID of the Sone being unfollowed
*/
public void unfollowSone(Sone sone, String soneId) {
- Validation.begin().isNotNull("Sone", sone).isNotNull("Sone ID", soneId).check();
- unfollowSone(sone, getSone(soneId, false));
- }
-
- /**
- * Lets the given local Sone unfollow the other given Sone. If the given
- * local Sone is the last local Sone that followed the given Sone, its
- * following time will be removed.
- *
- * @param sone
- * The local Sone that should unfollow another Sone
- * @param unfollowedSone
- * The Sone being unfollowed
- */
- public void unfollowSone(Sone sone, Sone unfollowedSone) {
- Validation.begin().isNotNull("Sone", sone).isNotNull("Unfollowed Sone", unfollowedSone).check();
- sone.removeFriend(unfollowedSone.getId());
+ checkNotNull(sone, "sone must not be null");
+ checkNotNull(soneId, "soneId must not be null");
+ database.removeFriend(sone, soneId);
boolean unfollowedSoneStillFollowed = false;
for (Sone localSone : getLocalSones()) {
- unfollowedSoneStillFollowed |= localSone.hasFriend(unfollowedSone.getId());
+ unfollowedSoneStillFollowed |= localSone.hasFriend(soneId);
}
if (!unfollowedSoneStillFollowed) {
synchronized (soneFollowingTimes) {
- soneFollowingTimes.remove(unfollowedSone);
+ soneFollowingTimes.remove(soneId);
}
}
touchConfiguration();
@@ -1052,7 +813,10 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
* The trust value (from {@code -100} to {@code 100})
*/
public void setTrust(Sone origin, Sone target, int trustValue) {
- Validation.begin().isNotNull("Trust Origin", origin).check().isInstanceOf("Trust Origin", origin.getIdentity(), OwnIdentity.class).isNotNull("Trust Target", target).isLessOrEqual("Trust Value", trustValue, 100).isGreaterOrEqual("Trust Value", trustValue, -100).check();
+ checkNotNull(origin, "origin must not be null");
+ checkArgument(origin.getIdentity() instanceof OwnIdentity, "origin must be a local Sone");
+ checkNotNull(target, "target must not be null");
+ checkArgument((trustValue >= -100) && (trustValue <= 100), "trustValue must be within [-100, 100]");
webOfTrustUpdater.setTrust((OwnIdentity) origin.getIdentity(), target.getIdentity(), trustValue, preferences.getTrustComment());
}
@@ -1065,7 +829,9 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
* The trust target
*/
public void removeTrust(Sone origin, Sone target) {
- Validation.begin().isNotNull("Trust Origin", origin).isNotNull("Trust Target", target).check().isInstanceOf("Trust Origin Identity", origin.getIdentity(), OwnIdentity.class).check();
+ checkNotNull(origin, "origin must not be null");
+ checkNotNull(target, "target must not be null");
+ checkArgument(origin.getIdentity() instanceof OwnIdentity, "origin must be a local Sone");
webOfTrustUpdater.setTrust((OwnIdentity) origin.getIdentity(), target.getIdentity(), null, null);
}
@@ -1126,117 +892,73 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
* {@code true} if the stored Sone should be updated regardless
* of the age of the given Sone
*/
- public void updateSone(Sone sone, boolean soneRescueMode) {
- if (hasSone(sone.getId())) {
- Sone storedSone = getSone(sone.getId());
- if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
+ public void updateSone(final Sone sone, boolean soneRescueMode) {
+ Optional storedSone = getSone(sone.getId());
+ if (storedSone.isPresent()) {
+ if (!soneRescueMode && !(sone.getTime() > storedSone.get().getTime())) {
logger.log(Level.FINE, String.format("Downloaded Sone %s is not newer than stored Sone %s.", sone, storedSone));
return;
}
- synchronized (posts) {
- if (!soneRescueMode) {
- for (Post post : storedSone.getPosts()) {
- posts.remove(post.getId());
- if (!sone.getPosts().contains(post)) {
- coreListenerManager.firePostRemoved(post);
- }
- }
- }
- List storedPosts = storedSone.getPosts();
- synchronized (knownPosts) {
- for (Post post : sone.getPosts()) {
- post.setSone(storedSone).setKnown(knownPosts.contains(post.getId()));
- if (!storedPosts.contains(post)) {
- if (post.getTime() < getSoneFollowingTime(sone)) {
- knownPosts.add(post.getId());
- post.setKnown(true);
- } else if (!knownPosts.contains(post.getId())) {
- coreListenerManager.fireNewPostFound(post);
- }
- }
- posts.put(post.getId(), post);
- }
- }
+ List