Move detection of modification into modification detector.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 22 Jun 2014 14:32:21 +0000 (16:32 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 22 Jun 2014 14:33:52 +0000 (16:33 +0200)
src/main/java/net/pterodactylus/sone/core/SoneInserter.java
src/main/java/net/pterodactylus/sone/core/SoneModificationDetector.java
src/test/java/net/pterodactylus/sone/core/SoneModificationDetectorTest.java

index a511d14..7001be1 100644 (file)
@@ -94,12 +94,11 @@ public class SoneInserter extends AbstractService {
        /** The Freenet interface. */
        private final FreenetInterface freenetInterface;
 
+       private final SoneModificationDetector soneModificationDetector;
+
        /** The Sone to insert. */
        private volatile Sone sone;
 
-       /** The fingerprint of the last insert. */
-       private volatile String lastInsertFingerprint;
-
        /**
         * Creates a new Sone inserter.
         *
@@ -118,6 +117,7 @@ public class SoneInserter extends AbstractService {
                this.eventBus = eventBus;
                this.freenetInterface = freenetInterface;
                this.sone = sone;
+               this.soneModificationDetector = new SoneModificationDetector(core, sone, insertionDelay);
        }
 
        //
@@ -154,7 +154,7 @@ public class SoneInserter extends AbstractService {
         * @return The fingerprint of the last insert
         */
        public String getLastInsertFingerprint() {
-               return lastInsertFingerprint;
+               return soneModificationDetector.getOriginalFingerprint();
        }
 
        /**
@@ -164,7 +164,7 @@ public class SoneInserter extends AbstractService {
         *            The fingerprint of the last insert
         */
        public void setLastInsertFingerprint(String lastInsertFingerprint) {
-               this.lastInsertFingerprint = lastInsertFingerprint;
+               soneModificationDetector.setFingerprint(lastInsertFingerprint);
        }
 
        /**
@@ -175,7 +175,7 @@ public class SoneInserter extends AbstractService {
         *         otherwise
         */
        public boolean isModified() {
-               return !lastInsertFingerprint.equals(sone.getFingerprint());
+               return soneModificationDetector.isModified();
        }
 
        //
@@ -187,7 +187,6 @@ public class SoneInserter extends AbstractService {
         */
        @Override
        protected void serviceRun() {
-               SoneModificationDetector soneModificationDetector = new SoneModificationDetector(core, sone, insertionDelay);
                while (!shouldStop()) {
                        try {
                                /* check every second. */
@@ -231,7 +230,6 @@ public class SoneInserter extends AbstractService {
                                                        if (insertInformation.getFingerprint().equals(sone.getFingerprint())) {
                                                                logger.log(Level.FINE, String.format("Sone “%s” was not modified further, resetting counter…", sone));
                                                                soneModificationDetector.setFingerprint(insertInformation.getFingerprint());
-                                                               lastInsertFingerprint = insertInformation.getFingerprint();
                                                                core.touchConfiguration();
                                                        }
                                                }
index 0a26e9e..2dd4d78 100644 (file)
@@ -64,6 +64,10 @@ class SoneModificationDetector {
                return insertionDelayHasPassed();
        }
 
+       public String getOriginalFingerprint() {
+               return originalFingerprint;
+       }
+
        public void setFingerprint(String fingerprint) {
                originalFingerprint = fingerprint;
                lastFingerprint = originalFingerprint;
@@ -74,4 +78,8 @@ class SoneModificationDetector {
                return lastModificationTime.isPresent() && (NANOSECONDS.toSeconds(ticker.read() - lastModificationTime.get()) >= insertionDelay.get());
        }
 
+       public boolean isModified() {
+               return !sone.getFingerprint().equals(originalFingerprint);
+       }
+
 }
index 90f2935..3e4da70 100644 (file)
@@ -45,10 +45,13 @@ public class SoneModificationDetectorTest {
 
        @Test
        public void modifiedSoneIsEligibleAfter60Seconds() {
+               assertThat(soneModificationDetector.isModified(), is(false));
                assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
                modifySone();
+               assertThat(soneModificationDetector.isModified(), is(true));
                assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
                passTime(100);
+               assertThat(soneModificationDetector.isModified(), is(true));
                assertThat(soneModificationDetector.isEligibleForInsert(), is(true));
        }