From bdc36fce008474fbb910dd599ce64d88fbc60461 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 28 Jan 2011 08:15:12 +0100 Subject: [PATCH] =?utf8?q?Wrap=20the=20Core=E2=80=99s=20options=20into=20a?= =?utf8?q?=20Preferences=20object=20which=20is=20easier=20to=20use.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../java/net/pterodactylus/sone/core/Core.java | 218 +++++++++++++++++++-- .../pterodactylus/sone/core/SoneDownloader.java | 5 +- .../net/pterodactylus/sone/web/OptionsPage.java | 36 ++-- 3 files changed, 220 insertions(+), 39 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 99a119b..60d334e 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -85,6 +85,9 @@ public class Core implements IdentityListener, UpdateListener { /** The options. */ private final Options options = new Options(); + /** The preferences. */ + private final Preferences preferences = new Preferences(options); + /** The core listener manager. */ private final CoreListenerManager coreListenerManager = new CoreListenerManager(this); @@ -221,18 +224,8 @@ public class Core implements IdentityListener, UpdateListener { * * @return The options of the core */ - public Options getOptions() { - return options; - } - - /** - * Returns whether the “Sone rescue mode” is currently activated. - * - * @return {@code true} if the “Sone rescue mode” is currently activated, - * {@code false} if it is not - */ - public boolean isSoneRescueMode() { - return options.getBooleanOption("SoneRescueMode").get(); + public Preferences getPreferences() { + return preferences; } /** @@ -767,7 +760,7 @@ public class Core implements IdentityListener, UpdateListener { soneInserters.put(sone, soneInserter); setSoneStatus(sone, SoneStatus.idle); loadSone(sone); - if (!isSoneRescueMode()) { + if (!preferences.isSoneRescueMode()) { soneInserter.start(); } new Thread(new Runnable() { @@ -775,7 +768,7 @@ public class Core implements IdentityListener, UpdateListener { @Override @SuppressWarnings("synthetic-access") public void run() { - if (!isSoneRescueMode()) { + if (!preferences.isSoneRescueMode()) { soneDownloader.fetchSone(sone); return; } @@ -783,7 +776,7 @@ public class Core implements IdentityListener, UpdateListener { coreListenerManager.fireRescuingSone(sone); lockSone(sone); long edition = sone.getLatestEdition(); - while (!stopped && (edition >= 0) && isSoneRescueMode()) { + while (!stopped && (edition >= 0) && preferences.isSoneRescueMode()) { logger.log(Level.FINE, "Downloading edition " + edition + "…"); soneDownloader.fetchSone(sone, sone.getRequestUri().setKeyType("SSK").setDocName("Sone-" + edition)); --edition; @@ -893,7 +886,7 @@ public class Core implements IdentityListener, UpdateListener { public void setTrust(Sone origin, Sone target, int trustValue) { Validation.begin().isNotNull("Trust Origin", origin).check().isInstanceOf("Trust Origin", origin.getIdentity(), OwnIdentity.class).isNotNull("Trust Target", target).isLessOrEqual("Trust Value", trustValue, 100).isGreaterOrEqual("Trust Value", trustValue, -100).check(); try { - ((OwnIdentity) origin.getIdentity()).setTrust(target.getIdentity(), trustValue, options.getStringOption("TrustComment").get()); + ((OwnIdentity) origin.getIdentity()).setTrust(target.getIdentity(), trustValue, preferences.getTrustComment()); } catch (WebOfTrustException wote1) { logger.log(Level.WARNING, "Could not set trust for Sone: " + target, wote1); } @@ -925,7 +918,7 @@ public class Core implements IdentityListener, UpdateListener { * The trust target */ public void trustSone(Sone origin, Sone target) { - setTrust(origin, target, options.getIntegerOption("PositiveTrust").get()); + setTrust(origin, target, preferences.getPositiveTrust()); } /** @@ -937,7 +930,7 @@ public class Core implements IdentityListener, UpdateListener { * The trust target */ public void distrustSone(Sone origin, Sone target) { - setTrust(origin, target, options.getIntegerOption("NegativeTrust").get()); + setTrust(origin, target, preferences.getNegativeTrust()); } /** @@ -960,7 +953,7 @@ public class Core implements IdentityListener, UpdateListener { */ public void updateSone(Sone sone) { if (hasSone(sone.getId())) { - boolean soneRescueMode = isLocalSone(sone) && isSoneRescueMode(); + boolean soneRescueMode = isLocalSone(sone) && preferences.isSoneRescueMode(); Sone storedSone = getSone(sone.getId()); if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) { logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone }); @@ -1775,4 +1768,191 @@ public class Core implements IdentityListener, UpdateListener { coreListenerManager.fireUpdateFound(version, releaseTime, latestEdition); } + /** + * Convenience interface for external classes that want to access the core’s + * configuration. + * + * @author David ‘Bombe’ Roden + */ + public static class Preferences { + + /** The wrapped options. */ + private final Options options; + + /** + * Creates a new preferences object wrapped around the given options. + * + * @param options + * The options to wrap + */ + public Preferences(Options options) { + this.options = options; + } + + /** + * Returns the insertion delay. + * + * @return The insertion delay + */ + public int getInsertionDelay() { + return options.getIntegerOption("InsertionDelay").get(); + } + + /** + * Sets the insertion delay + * + * @param insertionDelay + * The new insertion delay, or {@code null} to restore it to + * the default value + * @return This preferences + */ + public Preferences setInsertionDelay(Integer insertionDelay) { + options.getIntegerOption("InsertionDelay").set(insertionDelay); + return this; + } + + /** + * Returns the positive trust. + * + * @return The positive trust + */ + public int getPositiveTrust() { + return options.getIntegerOption("PositiveTrust").get(); + } + + /** + * Sets the positive trust. + * + * @param positiveTrust + * The new positive trust, or {@code null} to restore it to + * the default vlaue + * @return This preferences + */ + public Preferences setPositiveTrust(Integer positiveTrust) { + options.getIntegerOption("PositiveTrust").set(positiveTrust); + return this; + } + + /** + * Returns the negative trust. + * + * @return The negative trust + */ + public int getNegativeTrust() { + return options.getIntegerOption("NegativeTrust").get(); + } + + /** + * Sets the negative trust. + * + * @param negativeTrust + * The negative trust, or {@code null} to restore it to the + * default value + * @return The preferences + */ + public Preferences setNegativeTrust(Integer negativeTrust) { + options.getIntegerOption("NegativeTrust").set(negativeTrust); + return this; + } + + /** + * Returns the trust comment. This is the comment that is set in the web + * of trust when a trust value is assigned to an identity. + * + * @return The trust comment + */ + public String getTrustComment() { + return options.getStringOption("TrustComment").get(); + } + + /** + * Sets the trust comment. + * + * @param trustComment + * The trust comment, or {@code null} to restore it to the + * default value + * @return This preferences + */ + public Preferences setTrustComment(String trustComment) { + options.getStringOption("TrustComment").set(trustComment); + return this; + } + + /** + * Returns whether the rescue mode is active. + * + * @return {@code true} if the rescue mode is active, {@code false} + * otherwise + */ + public boolean isSoneRescueMode() { + return options.getBooleanOption("SoneRescueMode").get(); + } + + /** + * Sets whether the rescue mode is active. + * + * @param soneRescueMode + * {@code true} if the rescue mode is active, {@code false} + * otherwise + * @return This preferences + */ + public Preferences setSoneRescueMode(Boolean soneRescueMode) { + options.getBooleanOption("SoneRescueMode").set(soneRescueMode); + return this; + } + + /** + * Returns whether Sone should clear its settings on the next restart. + * In order to be effective, {@link #isReallyClearOnNextRestart()} needs + * to return {@code true} as well! + * + * @return {@code true} if Sone should clear its settings on the next + * restart, {@code false} otherwise + */ + public boolean isClearOnNextRestart() { + return options.getBooleanOption("ClearOnNextRestart").get(); + } + + /** + * Sets whether Sone will clear its settings on the next restart. + * + * @param clearOnNextRestart + * {@code true} if Sone should clear its settings on the next + * restart, {@code false} otherwise + * @return This preferences + */ + public Preferences setClearOnNextRestart(Boolean clearOnNextRestart) { + options.getBooleanOption("ClearOnNextRestart").set(clearOnNextRestart); + return this; + } + + /** + * Returns whether Sone should really clear its settings on next + * restart. This is a confirmation option that needs to be set in + * addition to {@link #isClearOnNextRestart()} in order to clear Sone’s + * settings on the next restart. + * + * @return {@code true} if Sone should really clear its settings on the + * next restart, {@code false} otherwise + */ + public boolean isReallyClearOnNextRestart() { + return options.getBooleanOption("ReallyClearOnNextRestart").get(); + } + + /** + * Sets whether Sone should really clear its settings on the next + * restart. + * + * @param reallyClearOnNextRestart + * {@code true} if Sone should really clear its settings on + * the next restart, {@code false} otherwise + * @return This preferences + */ + public Preferences setReallyClearOnNextRestart(Boolean reallyClearOnNextRestart) { + options.getBooleanOption("ReallyClearOnNextRestart").set(reallyClearOnNextRestart); + return this; + } + + } + } diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java index c883301..2eb4c71 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -25,6 +25,7 @@ import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; +import net.pterodactylus.sone.core.Core.Preferences; import net.pterodactylus.sone.core.Core.SoneStatus; import net.pterodactylus.sone.data.Client; import net.pterodactylus.sone.data.Post; @@ -122,8 +123,8 @@ public class SoneDownloader extends AbstractService { /** * Fetches the updated Sone. This method can be used to fetch a Sone from a - * specific URI (which happens when {@link Core#isSoneRescueMode() „Sone - * rescue mode“} is active). + * specific URI (which happens when {@link Preferences#isSoneRescueMode() + * „Sone rescue mode“} is active). * * @param sone * The Sone to fetch diff --git a/src/main/java/net/pterodactylus/sone/web/OptionsPage.java b/src/main/java/net/pterodactylus/sone/web/OptionsPage.java index 421b2a2..748dff7 100644 --- a/src/main/java/net/pterodactylus/sone/web/OptionsPage.java +++ b/src/main/java/net/pterodactylus/sone/web/OptionsPage.java @@ -17,7 +17,7 @@ package net.pterodactylus.sone.web; -import net.pterodactylus.sone.core.Options; +import net.pterodactylus.sone.core.Core.Preferences; import net.pterodactylus.sone.web.page.Page.Request.Method; import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.template.Template; @@ -52,35 +52,35 @@ public class OptionsPage extends SoneTemplatePage { @Override protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException { super.processTemplate(request, templateContext); - Options options = webInterface.getCore().getOptions(); + Preferences preferences = webInterface.getCore().getPreferences(); if (request.getMethod() == Method.POST) { Integer insertionDelay = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16)); - options.getIntegerOption("InsertionDelay").set(insertionDelay); - Integer positiveTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("positive-trust", 3), options.getIntegerOption("PositiveTrust").getReal()); - options.getIntegerOption("PositiveTrust").set(positiveTrust); - Integer negativeTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("negative-trust", 3), options.getIntegerOption("NegativeTrust").getReal()); - options.getIntegerOption("NegativeTrust").set(negativeTrust); + preferences.setInsertionDelay(insertionDelay); + Integer positiveTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("positive-trust", 3)); + preferences.setPositiveTrust(positiveTrust); + Integer negativeTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("negative-trust", 3)); + preferences.setNegativeTrust(negativeTrust); String trustComment = request.getHttpRequest().getPartAsStringFailsafe("trust-comment", 256); if (trustComment.trim().length() == 0) { trustComment = null; } - options.getStringOption("TrustComment").set(trustComment); + preferences.setTrustComment(trustComment); boolean soneRescueMode = Boolean.parseBoolean(request.getHttpRequest().getPartAsStringFailsafe("sone-rescue-mode", 5)); - options.getBooleanOption("SoneRescueMode").set(soneRescueMode); + preferences.setSoneRescueMode(soneRescueMode); boolean clearOnNextRestart = Boolean.parseBoolean(request.getHttpRequest().getPartAsStringFailsafe("clear-on-next-restart", 5)); - options.getBooleanOption("ClearOnNextRestart").set(clearOnNextRestart); + preferences.setClearOnNextRestart(clearOnNextRestart); boolean reallyClearOnNextRestart = Boolean.parseBoolean(request.getHttpRequest().getPartAsStringFailsafe("really-clear-on-next-restart", 5)); - options.getBooleanOption("ReallyClearOnNextRestart").set(reallyClearOnNextRestart); + preferences.setReallyClearOnNextRestart(reallyClearOnNextRestart); webInterface.getCore().saveConfiguration(); throw new RedirectException(getPath()); } - templateContext.set("insertion-delay", options.getIntegerOption("InsertionDelay").get()); - templateContext.set("positive-trust", options.getIntegerOption("PositiveTrust").get()); - templateContext.set("negative-trust", options.getIntegerOption("NegativeTrust").get()); - templateContext.set("trust-comment", options.getStringOption("TrustComment").get()); - templateContext.set("sone-rescue-mode", options.getBooleanOption("SoneRescueMode").get()); - templateContext.set("clear-on-next-restart", options.getBooleanOption("ClearOnNextRestart").get()); - templateContext.set("really-clear-on-next-restart", options.getBooleanOption("ReallyClearOnNextRestart").get()); + templateContext.set("insertion-delay", preferences.getInsertionDelay()); + templateContext.set("positive-trust", preferences.getPositiveTrust()); + templateContext.set("negative-trust", preferences.getNegativeTrust()); + templateContext.set("trust-comment", preferences.getTrustComment()); + templateContext.set("sone-rescue-mode", preferences.isSoneRescueMode()); + templateContext.set("clear-on-next-restart", preferences.isClearOnNextRestart()); + templateContext.set("really-clear-on-next-restart", preferences.isReallyClearOnNextRestart()); } } -- 2.7.4