Use local Sone with the Sone inserters.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 71dd349..2d23de3 100644 (file)
@@ -55,6 +55,7 @@ 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.Image;
+import net.pterodactylus.sone.data.LocalSone;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Reply;
@@ -147,15 +148,15 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
 
        /** Locked local Sones. */
        /* synchronize on itself. */
-       private final Set<Sone> lockedSones = new HashSet<Sone>();
+       private final Set<LocalSone> lockedSones = new HashSet<LocalSone>();
 
        /** Sone inserters. */
        /* synchronize access on this on sones. */
-       private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
+       private final Map<LocalSone, SoneInserter> soneInserters = new HashMap<LocalSone, SoneInserter>();
 
        /** Sone rescuers. */
        /* synchronize access on this on sones. */
-       private final Map<Sone, SoneRescuer> soneRescuers = new HashMap<Sone, SoneRescuer>();
+       private final Map<LocalSone, SoneRescuer> soneRescuers = new HashMap<LocalSone, SoneRescuer>();
 
        /** All known Sones. */
        private final Set<String> knownSones = new HashSet<String>();
@@ -268,9 +269,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         *            The local Sone to get the rescuer for
         * @return The Sone rescuer for the given Sone
         */
-       public SoneRescuer getSoneRescuer(Sone sone) {
+       public SoneRescuer getSoneRescuer(LocalSone sone) {
                checkNotNull(sone, "sone must not be null");
-               checkArgument(sone.isLocal(), "sone must be local");
                synchronized (soneRescuers) {
                        SoneRescuer soneRescuer = soneRescuers.get(sone);
                        if (soneRescuer == null) {
@@ -289,7 +289,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         *            The sone to check
         * @return {@code true} if the Sone is locked, {@code false} if it is not
         */
-       public boolean isLocked(Sone sone) {
+       public boolean isLocked(LocalSone sone) {
                synchronized (lockedSones) {
                        return lockedSones.contains(sone);
                }
@@ -326,27 +326,13 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                return database.getSone(id);
        }
 
-       /**
-        * {@inheritDocs}
-        */
        @Override
-       public Collection<Sone> getLocalSones() {
+       public Collection<LocalSone> getLocalSones() {
                return database.getLocalSones();
        }
 
-       /**
-        * Returns the local Sone with the given ID, optionally creating a new Sone.
-        *
-        * @param id
-        *            The ID of the Sone
-        * @return The Sone with the given ID, or {@code null}
-        */
-       public Sone getLocalSone(String id) {
-               Optional<Sone> sone = database.getSone(id);
-               if (sone.isPresent() && sone.get().isLocal()) {
-                       return sone.get();
-               }
-               return null;
+       public Optional<LocalSone> getLocalSone(String id) {
+               return database.getLocalSone(id);
        }
 
        /**
@@ -586,13 +572,13 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
 
        /**
         * Locks the given Sone. A locked Sone will not be inserted by
-        * {@link SoneInserter} until it is {@link #unlockSone(Sone) unlocked}
+        * {@link SoneInserter} until it is {@link #unlockSone(LocalSone) unlocked}
         * again.
         *
         * @param sone
         *            The sone to lock
         */
-       public void lockSone(Sone sone) {
+       public void lockSone(LocalSone sone) {
                synchronized (lockedSones) {
                        if (lockedSones.add(sone)) {
                                eventBus.post(new SoneLockedEvent(sone));
@@ -603,11 +589,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        /**
         * Unlocks the given Sone.
         *
-        * @see #lockSone(Sone)
+        * @see #lockSone(LocalSone)
         * @param sone
         *            The sone to unlock
         */
-       public void unlockSone(Sone sone) {
+       public void unlockSone(LocalSone sone) {
                synchronized (lockedSones) {
                        if (lockedSones.remove(sone)) {
                                eventBus.post(new SoneUnlockedEvent(sone));
@@ -628,7 +614,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        return null;
                }
                logger.info(String.format("Adding Sone from OwnIdentity: %s", ownIdentity));
-               Sone sone = database.registerLocalSone(ownIdentity);
+               LocalSone sone = database.registerLocalSone(ownIdentity);
                SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, ownIdentity.getId());
                eventBus.register(soneInserter);
                synchronized (soneInserters) {
@@ -1264,7 +1250,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        public void serviceStop() {
                localElementTicker.shutdownNow();
                synchronized (soneInserters) {
-                       for (Entry<Sone, SoneInserter> soneInserter : soneInserters.entrySet()) {
+                       for (Entry<LocalSone, SoneInserter> soneInserter : soneInserters.entrySet()) {
                                soneInserter.getValue().stop();
                        }
                }