From: David ‘Bombe’ Roden Date: Sun, 22 Jun 2014 14:32:21 +0000 (+0200) Subject: Move detection of modification into modification detector. X-Git-Tag: 0.9-rc1^2~3^2~238 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=2bccc50e24eec9fc0f20df72fd67c6887c1254dd;p=Sone.git Move detection of modification into modification detector. --- diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java index a511d14..7001be1 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java @@ -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(); } } diff --git a/src/main/java/net/pterodactylus/sone/core/SoneModificationDetector.java b/src/main/java/net/pterodactylus/sone/core/SoneModificationDetector.java index 0a26e9e..2dd4d78 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneModificationDetector.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneModificationDetector.java @@ -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); + } + } diff --git a/src/test/java/net/pterodactylus/sone/core/SoneModificationDetectorTest.java b/src/test/java/net/pterodactylus/sone/core/SoneModificationDetectorTest.java index 90f2935..3e4da70 100644 --- a/src/test/java/net/pterodactylus/sone/core/SoneModificationDetectorTest.java +++ b/src/test/java/net/pterodactylus/sone/core/SoneModificationDetectorTest.java @@ -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)); }