Use a modifier to set the latest edition of a Sone.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 19 Oct 2013 15:23:25 +0000 (17:23 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:30 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/FreenetInterface.java
src/main/java/net/pterodactylus/sone/core/SoneDownloader.java
src/main/java/net/pterodactylus/sone/core/SoneInserter.java
src/main/java/net/pterodactylus/sone/data/Sone.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java

index 7ea8134..1109537 100644 (file)
@@ -560,7 +560,7 @@ public class Core extends AbstractService implements SoneProvider {
                synchronized (sones) {
                        final Sone sone;
                        sone = database.newSoneBuilder().by(ownIdentity.getId()).local().build(Optional.<SoneCreated>absent());
-                       sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
+                       sone.modify().setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0)).update();
                        sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
                        sone.setKnown(true);
                        /* TODO - load posts ’n stuff */
@@ -618,7 +618,7 @@ public class Core extends AbstractService implements SoneProvider {
                        }
                        boolean newSone = !existingSone.isPresent();
                        final Sone sone = newSone ? database.newSoneBuilder().by(identity.getId()).build(Optional.<SoneCreated>absent()) : existingSone.get();
-                       sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
+                       sone.modify().setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0)).update();
                        if (newSone) {
                                synchronized (knownSones) {
                                        newSone = !knownSones.contains(sone.getId());
@@ -1725,7 +1725,7 @@ public class Core extends AbstractService implements SoneProvider {
                        @SuppressWarnings("synthetic-access")
                        public void run() {
                                Optional<Sone> sone = getRemoteSone(identity.getId());
-                               sone.get().setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), sone.get().getLatestEdition()));
+                               sone.get().modify().setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), sone.get().getLatestEdition())).update();
                                soneDownloader.addSone(sone.get());
                                soneDownloader.fetchSone(sone.get());
                        }
index bea268b..3baaa7d 100644 (file)
@@ -225,7 +225,7 @@ public class FreenetInterface {
                                public void onFoundEdition(long edition, USK key, ObjectContainer objectContainer, ClientContext clientContext, boolean metadata, short codec, byte[] data, boolean newKnownGood, boolean newSlotToo) {
                                        logger.log(Level.FINE, String.format("Found USK update for Sone “%s” at %s, new known good: %s, new slot too: %s.", sone, key, newKnownGood, newSlotToo));
                                        if (edition > sone.getLatestEdition()) {
-                                               sone.setLatestEdition(edition);
+                                               sone.modify().setLatestEdition(edition).update();
                                                new Thread(new Runnable() {
 
                                                        @Override
index 6eaa1b0..ef6c10a 100644 (file)
@@ -182,7 +182,7 @@ public class SoneDownloader extends AbstractService {
                        soneInputStream = soneBucket.getInputStream();
                        Sone parsedSone = parseSone(originalSone, soneInputStream);
                        if (parsedSone != null) {
-                               parsedSone.setLatestEdition(requestUri.getEdition());
+                               parsedSone.modify().setLatestEdition(requestUri.getEdition()).update();
                        }
                        return parsedSone;
                } catch (Exception e1) {
index c4d2e85..12abc53 100644 (file)
@@ -245,7 +245,7 @@ public class SoneInserter extends AbstractService {
                                                        break;
                                                }
                                                sone.setTime(insertTime);
-                                               sone.setLatestEdition(finalUri.getEdition());
+                                               sone.modify().setLatestEdition(finalUri.getEdition()).update();
                                                core.touchConfiguration();
                                                success = true;
                                                logger.log(Level.INFO, String.format("Inserted Sone “%s” at %s.", sone.getName(), finalUri));
index c01e120..083b3a5 100644 (file)
@@ -207,16 +207,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
        long getLatestEdition();
 
        /**
-        * Sets the latest edition of this Sone. If the given latest edition is not
-        * greater than the current latest edition, the latest edition of this Sone is
-        * not changed.
-        *
-        * @param latestEdition
-        *              The latest edition of this Sone
-        */
-       void setLatestEdition(long latestEdition);
-
-       /**
         * Return the time of the last inserted update of this Sone.
         *
         * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
@@ -519,4 +509,13 @@ public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
 
        PostReplyBuilder newPostReplyBuilder(String postId) throws IllegalStateException;
 
+       Modifier modify();
+
+       interface Modifier {
+
+               Modifier setLatestEdition(long latestEdition);
+               Sone update();
+
+       }
+
 }
index ee3052d..3abd1d4 100644 (file)
@@ -398,6 +398,23 @@ public class DefaultSone implements Sone {
                };
        }
 
+       public Modifier modify() {
+               return new Modifier() {
+                       private long latestEdition = DefaultSone.this.latestEdition;
+                       @Override
+                       public Modifier setLatestEdition(long latestEdition) {
+                               this.latestEdition = latestEdition;
+                               return this;
+                       }
+
+                       @Override
+                       public Sone update() {
+                               DefaultSone.this.latestEdition = latestEdition;
+                               return DefaultSone.this;
+                       }
+               };
+       }
+
        //
        // FINGERPRINTABLE METHODS
        //