X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneModificationDetector.java;h=92fa3dc60a8c4432a5db69f2651553c58c805081;hb=03cec6a6772c2d836d94864adddaf544cbe9d72f;hp=aba329c67d5509a1937238dd49cac66e363eb34a;hpb=8e5165e6dd908f3ec6b67007c42cb1ce90a079ee;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/core/SoneModificationDetector.java b/src/main/java/net/pterodactylus/sone/core/SoneModificationDetector.java
index aba329c..92fa3dc 100644
--- a/src/main/java/net/pterodactylus/sone/core/SoneModificationDetector.java
+++ b/src/main/java/net/pterodactylus/sone/core/SoneModificationDetector.java
@@ -10,6 +10,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import net.pterodactylus.sone.data.Sone;
import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.base.Ticker;
@@ -17,60 +18,55 @@ import com.google.common.base.Ticker;
* Class that detects {@link Sone} modifications (as per their {@link
* Sone#getFingerprint() fingerprints} and determines when a modified Sone may
* be inserted.
- *
- * @author David âBombeâ Roden
*/
class SoneModificationDetector {
private final Ticker ticker;
- private final Core core;
- private final Sone sone;
+ private final LockableFingerprintProvider lockableFingerprintProvider;
private final AtomicInteger insertionDelay;
private Optional lastModificationTime;
- private String originalFingerprint;
- private String lastFingerprint;
+ private String lastInsertFingerprint;
+ private String lastCheckFingerprint;
- SoneModificationDetector(Core core, Sone sone, AtomicInteger insertionDelay) {
- this(systemTicker(), core, sone, insertionDelay);
+ SoneModificationDetector(LockableFingerprintProvider lockableFingerprintProvider, AtomicInteger insertionDelay) {
+ this(systemTicker(), lockableFingerprintProvider, insertionDelay);
}
@VisibleForTesting
- SoneModificationDetector(Ticker ticker, Core core, Sone sone, AtomicInteger insertionDelay) {
+ SoneModificationDetector(Ticker ticker, LockableFingerprintProvider lockableFingerprintProvider, AtomicInteger insertionDelay) {
this.ticker = ticker;
- this.core = core;
- this.sone = sone;
+ this.lockableFingerprintProvider = lockableFingerprintProvider;
this.insertionDelay = insertionDelay;
- originalFingerprint = sone.getFingerprint();
- lastFingerprint = originalFingerprint;
+ lastCheckFingerprint = lastInsertFingerprint;
}
public boolean isEligibleForInsert() {
- if (core.isLocked(sone)) {
+ if (lockableFingerprintProvider.isLocked()) {
lastModificationTime = absent();
- lastFingerprint = "";
+ lastCheckFingerprint = "";
return false;
}
- String fingerprint = sone.getFingerprint();
- if (originalFingerprint.equals(fingerprint)) {
+ String fingerprint = lockableFingerprintProvider.getFingerprint();
+ if (fingerprint.equals(lastInsertFingerprint)) {
lastModificationTime = absent();
- lastFingerprint = fingerprint;
+ lastCheckFingerprint = fingerprint;
return false;
}
- if (!lastFingerprint.equals(fingerprint)) {
+ if (!Objects.equal(lastCheckFingerprint, fingerprint)) {
lastModificationTime = of(ticker.read());
- lastFingerprint = fingerprint;
+ lastCheckFingerprint = fingerprint;
return false;
}
return insertionDelayHasPassed();
}
- public String getOriginalFingerprint() {
- return originalFingerprint;
+ public String getLastInsertFingerprint() {
+ return lastInsertFingerprint;
}
public void setFingerprint(String fingerprint) {
- originalFingerprint = fingerprint;
- lastFingerprint = originalFingerprint;
+ lastInsertFingerprint = fingerprint;
+ lastCheckFingerprint = lastInsertFingerprint;
lastModificationTime = absent();
}
@@ -79,7 +75,18 @@ class SoneModificationDetector {
}
public boolean isModified() {
- return !sone.getFingerprint().equals(originalFingerprint);
+ return !Objects.equal(lockableFingerprintProvider.getFingerprint(), lastInsertFingerprint);
+ }
+
+ /**
+ * Provider for a fingerprint and the information if a {@link Sone} is locked. This
+ * prevents us from having to lug a Sone object around.
+ */
+ static interface LockableFingerprintProvider {
+
+ boolean isLocked();
+ String getFingerprint();
+
}
}