From: David ‘Bombe’ Roden Date: Sat, 19 Oct 2013 15:23:25 +0000 (+0200) Subject: Use a modifier to set the latest edition of a Sone. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=9f9834453e9555175e4771932d9521209bd7188c;p=Sone.git Use a modifier to set the latest edition of a Sone. --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 7ea8134..1109537 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -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.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.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 = 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()); } diff --git a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java index bea268b..3baaa7d 100644 --- a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java +++ b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java @@ -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 diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java index 6eaa1b0..ef6c10a 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -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) { diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java index c4d2e85..12abc53 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java @@ -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)); diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index c01e120..083b3a5 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -207,16 +207,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable { 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 { PostReplyBuilder newPostReplyBuilder(String postId) throws IllegalStateException; + Modifier modify(); + + interface Modifier { + + Modifier setLatestEdition(long latestEdition); + Sone update(); + + } + } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java index ee3052d..3abd1d4 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java @@ -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 //