From f3cbb600ac240e77a1a614b3f897c2d9d909d0bc Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sat, 5 Aug 2017 17:04:07 -0700 Subject: [PATCH] Remove the default load-from-file behavior from the Abacus core. --- src/main/java/org/nwapw/abacus/Abacus.java | 11 +++-------- .../java/org/nwapw/abacus/config/Configuration.java | 9 +++++++++ .../java/org/nwapw/abacus/fx/AbacusController.java | 9 +++++++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/nwapw/abacus/Abacus.java b/src/main/java/org/nwapw/abacus/Abacus.java index 8d21cb4..eda7b80 100644 --- a/src/main/java/org/nwapw/abacus/Abacus.java +++ b/src/main/java/org/nwapw/abacus/Abacus.java @@ -12,7 +12,7 @@ import org.nwapw.abacus.plugin.StandardPlugin; import org.nwapw.abacus.tree.NumberReducer; import org.nwapw.abacus.tree.TreeNode; -import java.io.File; +import java.util.function.Supplier; /** * The main calculator class. This is responsible @@ -25,10 +25,6 @@ public class Abacus { * The default number implementation to be used if no other one is available / selected. */ public static final NumberImplementation DEFAULT_IMPLEMENTATION = StandardPlugin.IMPLEMENTATION_NAIVE; - /** - * The file used for saving and loading configuration. - */ - public static final File CONFIG_FILE = new File("config.toml"); /** * The plugin manager responsible for @@ -53,11 +49,10 @@ public class Abacus { /** * Creates a new instance of the Abacus calculator. */ - public Abacus() { + public Abacus(Supplier configurationSupplier) { pluginManager = new PluginManager(this); numberReducer = new NumberReducer(this); - configuration = new Configuration(CONFIG_FILE); - configuration.saveTo(CONFIG_FILE); + configuration = configurationSupplier.get(); LexerTokenizer lexerTokenizer = new LexerTokenizer(); ShuntingYardParser shuntingYardParser = new ShuntingYardParser(this); treeBuilder = new TreeBuilder<>(lexerTokenizer, shuntingYardParser); diff --git a/src/main/java/org/nwapw/abacus/config/Configuration.java b/src/main/java/org/nwapw/abacus/config/Configuration.java index 0715307..c7d7721 100644 --- a/src/main/java/org/nwapw/abacus/config/Configuration.java +++ b/src/main/java/org/nwapw/abacus/config/Configuration.java @@ -85,6 +85,15 @@ public class Configuration { } } + /** + * Gets the value of this configuration as a string. + * + * @return the string that represents this configuration. + */ + public String asTomlString(){ + return TOML_WRITER.write(this); + } + /** * Gets the number implementation from this configuration. * diff --git a/src/main/java/org/nwapw/abacus/fx/AbacusController.java b/src/main/java/org/nwapw/abacus/fx/AbacusController.java index 829c39d..1e75008 100644 --- a/src/main/java/org/nwapw/abacus/fx/AbacusController.java +++ b/src/main/java/org/nwapw/abacus/fx/AbacusController.java @@ -18,6 +18,7 @@ import org.nwapw.abacus.plugin.PluginManager; import org.nwapw.abacus.plugin.StandardPlugin; import org.nwapw.abacus.tree.TreeNode; +import java.io.File; import java.io.IOException; import java.util.Set; @@ -28,6 +29,10 @@ import java.util.Set; */ public class AbacusController implements PluginListener { + /** + * The file used for saving and loading configuration. + */ + public static final File CONFIG_FILE = new File("config.toml"); /** * The title for the apply alert dialog. */ @@ -175,7 +180,7 @@ public class AbacusController implements PluginListener { if (oldValue.equals(settingsTab)) alertIfApplyNeeded(true); }); - abacus = new Abacus(); + abacus = new Abacus(() -> new Configuration(new java.io.File("config.toml"))); PluginManager abacusPluginManager = abacus.getPluginManager(); abacusPluginManager.addListener(this); abacusPluginManager.addInstantiated(new StandardPlugin(abacus.getPluginManager())); @@ -279,7 +284,7 @@ public class AbacusController implements PluginListener { for (ToggleablePlugin pluginEntry : enabledPlugins) { if (!pluginEntry.isEnabled()) disabledPlugins.add(pluginEntry.getClassName()); } - configuration.saveTo(Abacus.CONFIG_FILE); + configuration.saveTo(CONFIG_FILE); changesMade = false; reloadAlertShown = false; }