From b78707a0f48e44c1b7c6444ed69b093711989257 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 4 Aug 2017 13:20:57 -0700 Subject: [PATCH] Format code. --- .../nwapw/abacus/config/Configuration.java | 23 +- .../org/nwapw/abacus/fx/AbacusController.java | 28 +- .../org/nwapw/abacus/fx/CopyableCell.java | 5 +- .../org/nwapw/abacus/fx/HistoryModel.java | 14 +- .../org/nwapw/abacus/fx/ToggleablePlugin.java | 8 +- .../org/nwapw/abacus/number/NaiveNumber.java | 3 +- .../nwapw/abacus/number/NumberInterface.java | 4 + .../nwapw/abacus/number/PreciseNumber.java | 6 +- .../abacus/parsing/ShuntingYardParser.java | 4 +- .../abacus/plugin/NumberImplementation.java | 16 +- .../java/org/nwapw/abacus/plugin/Plugin.java | 14 +- .../nwapw/abacus/plugin/PluginManager.java | 36 +- .../nwapw/abacus/plugin/StandardPlugin.java | 440 +++++++++--------- .../java/org/nwapw/abacus/window/Window.java | 2 +- 14 files changed, 317 insertions(+), 286 deletions(-) diff --git a/src/main/java/org/nwapw/abacus/config/Configuration.java b/src/main/java/org/nwapw/abacus/config/Configuration.java index 951edee..0715307 100644 --- a/src/main/java/org/nwapw/abacus/config/Configuration.java +++ b/src/main/java/org/nwapw/abacus/config/Configuration.java @@ -20,7 +20,7 @@ public class Configuration { */ private static final String DEFAULT_CONFIG = "numberImplementation = \"naive\"\n" + - "disabledPlugins = []"; + "disabledPlugins = []"; /** * The defaults TOML object, parsed from the string. */ @@ -41,28 +41,31 @@ public class Configuration { /** * Creates a new configuration with the given values. + * * @param numberImplementation the number implementation, like "naive" or "precise" - * @param disabledPlugins the list of disabled plugins. + * @param disabledPlugins the list of disabled plugins. */ - public Configuration(String numberImplementation, String[] disabledPlugins){ + public Configuration(String numberImplementation, String[] disabledPlugins) { this.numberImplementation = numberImplementation; this.disabledPlugins.addAll(Arrays.asList(disabledPlugins)); } /** * Loads a configuration from a given file, keeping non-specified fields default. + * * @param fromFile the file to load from. */ - public Configuration(File fromFile){ - if(!fromFile.exists()) return; + public Configuration(File fromFile) { + if (!fromFile.exists()) return; copyFrom(new Toml(DEFAULT_TOML).read(fromFile).to(Configuration.class)); } /** * Copies the values from the given configuration into this one. + * * @param otherConfiguration the configuration to copy from. */ - public void copyFrom(Configuration otherConfiguration){ + public void copyFrom(Configuration otherConfiguration) { this.numberImplementation = otherConfiguration.numberImplementation; this.disabledPlugins.addAll(otherConfiguration.disabledPlugins); } @@ -70,10 +73,11 @@ public class Configuration { /** * Saves this configuration to the given file, creating * any directories that do not exist. + * * @param file the file to save to. */ - public void saveTo(File file){ - if(file.getParentFile() != null) file.getParentFile().mkdirs(); + public void saveTo(File file) { + if (file.getParentFile() != null) file.getParentFile().mkdirs(); try { TOML_WRITER.write(this, file); } catch (IOException e) { @@ -83,6 +87,7 @@ public class Configuration { /** * Gets the number implementation from this configuration. + * * @return the number implementation. */ public String getNumberImplementation() { @@ -91,6 +96,7 @@ public class Configuration { /** * Sets the number implementation for the configuration + * * @param numberImplementation the number implementation. */ public void setNumberImplementation(String numberImplementation) { @@ -99,6 +105,7 @@ public class Configuration { /** * Gets the list of disabled plugins. + * * @return the list of disabled plugins. */ public Set getDisabledPlugins() { diff --git a/src/main/java/org/nwapw/abacus/fx/AbacusController.java b/src/main/java/org/nwapw/abacus/fx/AbacusController.java index a924af9..e677834 100644 --- a/src/main/java/org/nwapw/abacus/fx/AbacusController.java +++ b/src/main/java/org/nwapw/abacus/fx/AbacusController.java @@ -36,7 +36,7 @@ public class AbacusController implements PluginListener { * The text for the dialog that is shown if settings haven't been applied. */ private static final String APPLY_MSG_TEXT = "You have made changes to the configuration, however, you haven't pressed \"Apply\". " + - "The changes to the configuration will not be present in the calculator until \"Apply\" is pressed."; + "The changes to the configuration will not be present in the calculator until \"Apply\" is pressed."; /** * Constant string that is displayed if the text could not be lexed or parsed. */ @@ -110,15 +110,15 @@ public class AbacusController implements PluginListener { * Alerts the user if the changes they made * have not yet been applied. */ - private void alertIfApplyNeeded(boolean ignorePrevious){ - if(changesMade && (!reloadAlertShown || ignorePrevious)) { + private void alertIfApplyNeeded(boolean ignorePrevious) { + if (changesMade && (!reloadAlertShown || ignorePrevious)) { reloadAlertShown = true; reloadAlert.showAndWait(); } } @FXML - public void initialize(){ + public void initialize() { Callback, TableCell> cellFactory = param -> new CopyableCell<>(); Callback, ListCell> pluginCellFactory = @@ -150,7 +150,7 @@ public class AbacusController implements PluginListener { outputColumn.setCellFactory(cellFactory); outputColumn.setCellValueFactory(cell -> cell.getValue().outputProperty()); coreTabPane.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { - if(oldValue.equals(settingsTab)) alertIfApplyNeeded(true); + if (oldValue.equals(settingsTab)) alertIfApplyNeeded(true); }); abacus = new Abacus(); @@ -167,16 +167,16 @@ public class AbacusController implements PluginListener { } @FXML - private void performCalculation(){ + private void performCalculation() { inputButton.setDisable(true); TreeNode constructedTree = abacus.parseString(inputField.getText()); - if(constructedTree == null){ + if (constructedTree == null) { outputText.setText(ERR_SYNTAX); inputButton.setDisable(false); return; } NumberInterface evaluatedNumber = abacus.evaluateTree(constructedTree); - if(evaluatedNumber == null){ + if (evaluatedNumber == null) { outputText.setText(ERR_EVAL); inputButton.setDisable(false); return; @@ -189,7 +189,7 @@ public class AbacusController implements PluginListener { } @FXML - private void performSaveAndReload(){ + private void performSaveAndReload() { performSave(); performReload(); changesMade = false; @@ -197,19 +197,19 @@ public class AbacusController implements PluginListener { } @FXML - private void performReload(){ + private void performReload() { alertIfApplyNeeded(true); abacus.getPluginManager().reload(); } @FXML - private void performSave(){ + private void performSave() { Configuration configuration = abacus.getConfiguration(); configuration.setNumberImplementation(numberImplementationBox.getSelectionModel().getSelectedItem()); Set disabledPlugins = configuration.getDisabledPlugins(); disabledPlugins.clear(); - for(ToggleablePlugin pluginEntry : enabledPlugins){ - if(!pluginEntry.isEnabled()) disabledPlugins.add(pluginEntry.getClassName()); + for (ToggleablePlugin pluginEntry : enabledPlugins) { + if (!pluginEntry.isEnabled()) disabledPlugins.add(pluginEntry.getClassName()); } configuration.saveTo(Abacus.CONFIG_FILE); changesMade = false; @@ -224,7 +224,7 @@ public class AbacusController implements PluginListener { String actualImplementation = configuration.getNumberImplementation(); String toSelect = (numberImplementationOptions.contains(actualImplementation)) ? actualImplementation : ""; numberImplementationBox.getSelectionModel().select(toSelect); - for(Class pluginClass : abacus.getPluginManager().getLoadedPluginClasses()){ + for (Class pluginClass : abacus.getPluginManager().getLoadedPluginClasses()) { String fullName = pluginClass.getName(); ToggleablePlugin plugin = new ToggleablePlugin(!disabledPlugins.contains(fullName), fullName); plugin.enabledProperty().addListener(e -> changesMade = true); diff --git a/src/main/java/org/nwapw/abacus/fx/CopyableCell.java b/src/main/java/org/nwapw/abacus/fx/CopyableCell.java index f7c4cb9..5f0d823 100644 --- a/src/main/java/org/nwapw/abacus/fx/CopyableCell.java +++ b/src/main/java/org/nwapw/abacus/fx/CopyableCell.java @@ -9,6 +9,7 @@ import java.awt.datatransfer.StringSelection; /** * A cell that copies its value to the clipboard * when double clicked. + * * @param The type of the table view generic type. * @param The type of the value contained in the cell. */ @@ -17,9 +18,9 @@ public class CopyableCell extends TableCell { /** * Creates a new copyable cell. */ - public CopyableCell(){ + public CopyableCell() { addEventFilter(MouseEvent.MOUSE_CLICKED, event -> { - if(event.getClickCount() == 2){ + if (event.getClickCount() == 2) { Toolkit.getDefaultToolkit().getSystemClipboard() .setContents(new StringSelection(getText()), null); } diff --git a/src/main/java/org/nwapw/abacus/fx/HistoryModel.java b/src/main/java/org/nwapw/abacus/fx/HistoryModel.java index 9a7ba2b..1e86853 100644 --- a/src/main/java/org/nwapw/abacus/fx/HistoryModel.java +++ b/src/main/java/org/nwapw/abacus/fx/HistoryModel.java @@ -26,11 +26,12 @@ public class HistoryModel { /** * Creates a new history model with the given variables. - * @param input the user input + * + * @param input the user input * @param parsed the parsed input * @param output the program output. */ - public HistoryModel(String input, String parsed, String output){ + public HistoryModel(String input, String parsed, String output) { this.input = new SimpleStringProperty(); this.parsed = new SimpleStringProperty(); this.output = new SimpleStringProperty(); @@ -41,13 +42,16 @@ public class HistoryModel { /** * Gets the input property. + * * @return the input property. */ public StringProperty inputProperty() { return input; } + /** * Gets the input. + * * @return the input. */ public String getInput() { @@ -56,13 +60,16 @@ public class HistoryModel { /** * Gets the parsed input property. + * * @return the parsed input property. */ public StringProperty parsedProperty() { return parsed; } + /** * Gets the parsed input. + * * @return the parsed input. */ public String getParsed() { @@ -71,13 +78,16 @@ public class HistoryModel { /** * Gets the output property. + * * @return the output property. */ public StringProperty outputProperty() { return output; } + /** * Gets the program output. + * * @return the output. */ public String getOutput() { diff --git a/src/main/java/org/nwapw/abacus/fx/ToggleablePlugin.java b/src/main/java/org/nwapw/abacus/fx/ToggleablePlugin.java index 45238cb..0a87571 100644 --- a/src/main/java/org/nwapw/abacus/fx/ToggleablePlugin.java +++ b/src/main/java/org/nwapw/abacus/fx/ToggleablePlugin.java @@ -20,10 +20,11 @@ public class ToggleablePlugin { /** * Creates a new toggleable plugin with the given properties. - * @param enabled the enabled / disabled state at the beginning. + * + * @param enabled the enabled / disabled state at the beginning. * @param className the name of the class this plugin toggles. */ - public ToggleablePlugin(boolean enabled, String className){ + public ToggleablePlugin(boolean enabled, String className) { this.enabled = new SimpleBooleanProperty(); this.enabled.setValue(enabled); this.className = className; @@ -31,6 +32,7 @@ public class ToggleablePlugin { /** * Gets the enabled property of this plugin. + * * @return the enabled property. */ public BooleanProperty enabledProperty() { @@ -39,6 +41,7 @@ public class ToggleablePlugin { /** * Checks if this plugin entry should be enabled. + * * @return whether this plugin will be enabled. */ public boolean isEnabled() { @@ -47,6 +50,7 @@ public class ToggleablePlugin { /** * Gets the class name this plugin toggles. + * * @return the class name that should be disabled. */ public String getClassName() { diff --git a/src/main/java/org/nwapw/abacus/number/NaiveNumber.java b/src/main/java/org/nwapw/abacus/number/NaiveNumber.java index b32e2fa..2f97497 100755 --- a/src/main/java/org/nwapw/abacus/number/NaiveNumber.java +++ b/src/main/java/org/nwapw/abacus/number/NaiveNumber.java @@ -26,6 +26,7 @@ public class NaiveNumber implements NumberInterface { public NaiveNumber(String value) { this(Double.parseDouble(value)); } + /** * Creates a new NaiveNumber with the given value. * @@ -110,7 +111,7 @@ public class NaiveNumber implements NumberInterface { @Override public int intValue() { - return (int)value; + return (int) value; } @Override diff --git a/src/main/java/org/nwapw/abacus/number/NumberInterface.java b/src/main/java/org/nwapw/abacus/number/NumberInterface.java index dad9274..e8ca571 100755 --- a/src/main/java/org/nwapw/abacus/number/NumberInterface.java +++ b/src/main/java/org/nwapw/abacus/number/NumberInterface.java @@ -81,18 +81,21 @@ public interface NumberInterface { /** * Returns the least integer greater than or equal to the number. + * * @return the least integer >= the number, if int can hold the value. */ NumberInterface ceiling(); /** * Return the greatest integer less than or equal to the number. + * * @return the greatest int >= the number, if int can hold the value. */ NumberInterface floor(); /** * Returns the fractional part of the number. + * * @return the fractional part of the number. */ NumberInterface fractionalPart(); @@ -100,6 +103,7 @@ public interface NumberInterface { /** * Returns the integer representation of this number, discarding any fractional part, * if int can hold the value. + * * @return */ int intValue(); diff --git a/src/main/java/org/nwapw/abacus/number/PreciseNumber.java b/src/main/java/org/nwapw/abacus/number/PreciseNumber.java index 458815f..1baa867 100755 --- a/src/main/java/org/nwapw/abacus/number/PreciseNumber.java +++ b/src/main/java/org/nwapw/abacus/number/PreciseNumber.java @@ -102,7 +102,7 @@ public class PreciseNumber implements NumberInterface { public NumberInterface ceiling() { String str = value.toPlainString(); int decimalIndex = str.indexOf('.'); - if(decimalIndex != -1){ + if (decimalIndex != -1) { return this.floor().add(ONE); } return this; @@ -112,7 +112,7 @@ public class PreciseNumber implements NumberInterface { public NumberInterface floor() { String str = value.toPlainString(); int decimalIndex = str.indexOf('.'); - if(decimalIndex != -1){ + if (decimalIndex != -1) { return new PreciseNumber(str.substring(0, decimalIndex)); } return this; @@ -122,7 +122,7 @@ public class PreciseNumber implements NumberInterface { public NumberInterface fractionalPart() { String str = value.toPlainString(); int decimalIndex = str.indexOf('.'); - if(decimalIndex != -1){ + if (decimalIndex != -1) { return new PreciseNumber(str.substring(decimalIndex + 1)); } return ZERO; diff --git a/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java b/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java index 61d76e4..6239ed9 100644 --- a/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java +++ b/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java @@ -77,8 +77,8 @@ public class ShuntingYardParser implements Parser>, PluginListe continue; } - if(tokenString.equals("-") && (previousType == null || previousType == TokenType.OP || - previousType == TokenType.OPEN_PARENTH)){ + if (tokenString.equals("-") && (previousType == null || previousType == TokenType.OP || + previousType == TokenType.OPEN_PARENTH)) { from.add(0, new Match<>("`", TokenType.OP)); continue; } diff --git a/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java b/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java index 66e2dd2..8ccbf75 100644 --- a/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java +++ b/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java @@ -26,10 +26,11 @@ public abstract class NumberImplementation { /** * Creates a new number implementation with the given data. + * * @param implementation the implementation class. - * @param priority the priority, higher -> more likely to be converted into. + * @param priority the priority, higher -> more likely to be converted into. */ - public NumberImplementation(Class implementation, int priority){ + public NumberImplementation(Class implementation, int priority) { this.implementation = implementation; this.priority = priority; promotionPaths = new HashMap<>(); @@ -37,30 +38,34 @@ public abstract class NumberImplementation { /** * Gets the list of all promotion paths this implementation can take. + * * @return the map of documentation paths. */ - public final Map, Function> getPromotionPaths(){ + public final Map, Function> getPromotionPaths() { return promotionPaths; } /** * Gets the implementation class used by this implementation. + * * @return the implementation class. */ - public final Class getImplementation(){ + public final Class getImplementation() { return implementation; } /** * Gets the priority of this number implementation. + * * @return the priority. */ - public final int getPriority(){ + public final int getPriority() { return priority; } /** * Abstract function to create a new instance from a string. + * * @param string the string to create a number from. * @return the resulting number. */ @@ -68,6 +73,7 @@ public abstract class NumberImplementation { /** * Get the instance of pi with the given implementation. + * * @return pi */ public abstract NumberInterface instanceForPi(); diff --git a/src/main/java/org/nwapw/abacus/plugin/Plugin.java b/src/main/java/org/nwapw/abacus/plugin/Plugin.java index 3a0ed31..b569dea 100644 --- a/src/main/java/org/nwapw/abacus/plugin/Plugin.java +++ b/src/main/java/org/nwapw/abacus/plugin/Plugin.java @@ -78,7 +78,7 @@ public abstract class Plugin { * * @return the list of registered number implementations. */ - public final Set providedNumberImplementations(){ + public final Set providedNumberImplementations() { return numberImplementations.keySet(); } @@ -108,7 +108,7 @@ public abstract class Plugin { * @param name the name of the number implementation to look up. * @return the number implementation associated with that name, or null if the plugin doesn't provide it. */ - public final NumberImplementation getNumberImplementation(String name){ + public final NumberImplementation getNumberImplementation(String name) { return numberImplementations.get(name); } @@ -160,10 +160,11 @@ public abstract class Plugin { /** * To be used in load(). Registers a new number implementation with the plugin. * This makes it accessible to the plugin manager. - * @param name the name of the implementation. + * + * @param name the name of the implementation. * @param implementation the actual implementation class to register. */ - protected final void registerNumberImplementation(String name, NumberImplementation implementation){ + protected final void registerNumberImplementation(String name, NumberImplementation implementation) { numberImplementations.put(name, implementation); } @@ -199,7 +200,7 @@ public abstract class Plugin { * @param name the name for which to search. * @return the resulting number implementation, or null if none was found. */ - protected final NumberImplementation numberImplementationFor(String name){ + protected final NumberImplementation numberImplementationFor(String name) { return manager.numberImplementationFor(name); } @@ -208,10 +209,11 @@ public abstract class Plugin { * This is done so that number implementations with various degrees of precision * can provide their own pi values, without losing said precision by * promoting NaiveNumbers. + * * @param forClass the class to which to find the pi instance. * @return the pi value for the given class. */ - protected final NumberInterface getPi(Class forClass){ + protected final NumberInterface getPi(Class forClass) { return manager.piFor(forClass); } diff --git a/src/main/java/org/nwapw/abacus/plugin/PluginManager.java b/src/main/java/org/nwapw/abacus/plugin/PluginManager.java index 921b9dd..a6899d8 100644 --- a/src/main/java/org/nwapw/abacus/plugin/PluginManager.java +++ b/src/main/java/org/nwapw/abacus/plugin/PluginManager.java @@ -102,9 +102,9 @@ public class PluginManager { * @return the retrieved element, or null if it was not found. */ private static T searchCached(Collection plugins, Map cache, - java.util.function.Function> setFunction, - java.util.function.BiFunction getFunction, - K name) { + java.util.function.Function> setFunction, + java.util.function.BiFunction getFunction, + K name) { if (cache.containsKey(name)) return cache.get(name); T loadedValue = null; @@ -141,27 +141,29 @@ public class PluginManager { /** * Gets the number implementation under the given name. + * * @param name the name of the implementation. * @return the implementation. */ - public NumberImplementation numberImplementationFor(String name){ + public NumberImplementation numberImplementationFor(String name) { return searchCached(plugins, cachedNumberImplementations, Plugin::providedNumberImplementations, Plugin::getNumberImplementation, name); } /** * Gets the number implementation for the given implementation class. + * * @param name the class for which to find the implementation. * @return the implementation. */ - public NumberImplementation interfaceImplementationFor(Class name){ - if(cachedInterfaceImplementations.containsKey(name)) return cachedInterfaceImplementations.get(name); + public NumberImplementation interfaceImplementationFor(Class name) { + if (cachedInterfaceImplementations.containsKey(name)) return cachedInterfaceImplementations.get(name); NumberImplementation toReturn = null; outside: - for(Plugin plugin : plugins){ - for(String implementationName : plugin.providedNumberImplementations()){ + for (Plugin plugin : plugins) { + for (String implementationName : plugin.providedNumberImplementations()) { NumberImplementation implementation = plugin.getNumberImplementation(implementationName); - if(implementation.getImplementation().equals(name)) { + if (implementation.getImplementation().equals(name)) { toReturn = implementation; break outside; } @@ -173,14 +175,15 @@ public class PluginManager { /** * Gets the mathematical constant pi for the given implementation class. + * * @param forClass the class for which to find pi. * @return pi */ - public NumberInterface piFor(Class forClass){ - if(cachedPi.containsKey(forClass)) return cachedPi.get(forClass); + public NumberInterface piFor(Class forClass) { + if (cachedPi.containsKey(forClass)) return cachedPi.get(forClass); NumberImplementation implementation = interfaceImplementationFor(forClass); NumberInterface generatedPi = null; - if(implementation != null){ + if (implementation != null) { generatedPi = implementation.instanceForPi(); } cachedPi.put(forClass, generatedPi); @@ -219,11 +222,11 @@ public class PluginManager { public void load() { Set disabledPlugins = abacus.getConfiguration().getDisabledPlugins(); for (Plugin plugin : plugins) { - if(disabledPlugins.contains(plugin.getClass().getName())) continue; + if (disabledPlugins.contains(plugin.getClass().getName())) continue; plugin.enable(); } for (Plugin plugin : plugins) { - if(disabledPlugins.contains(plugin.getClass().getName())) continue; + if (disabledPlugins.contains(plugin.getClass().getName())) continue; allFunctions.addAll(plugin.providedFunctions()); allOperators.addAll(plugin.providedOperators()); allNumberImplementations.addAll(plugin.providedNumberImplementations()); @@ -238,7 +241,7 @@ public class PluginManager { listeners.forEach(e -> e.onUnload(this)); Set disabledPlugins = abacus.getConfiguration().getDisabledPlugins(); for (Plugin plugin : plugins) { - if(disabledPlugins.contains(plugin.getClass().getName())) continue; + if (disabledPlugins.contains(plugin.getClass().getName())) continue; plugin.disable(); } cachedFunctions.clear(); @@ -283,7 +286,7 @@ public class PluginManager { * * @return the set of all implementations that were loaded. */ - public Set getAllNumberImplementations(){ + public Set getAllNumberImplementations() { return allNumberImplementations; } @@ -308,6 +311,7 @@ public class PluginManager { /** * Gets a list of all the plugin class files that have been * added to the plugin manager. + * * @return the list of all the added plugin classes. */ public Set> getLoadedPluginClasses() { diff --git a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java index e4197c5..be9f9ea 100755 --- a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java @@ -18,8 +18,6 @@ import java.util.function.BiFunction; */ public class StandardPlugin extends Plugin { - private static final HashMap, ArrayList> FACTORIAL_LISTS = new HashMap<>(); - /** * The addition operator, + */ @@ -129,20 +127,6 @@ public class StandardPlugin extends Plugin { }*/ } }); - /** - * The caret / pow operator, ^ - */ - public static final Operator OP_CARET = new Operator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2, new Function() { - @Override - protected boolean matchesParams(NumberInterface[] params) { - return params.length == 2; - } - - @Override - protected NumberInterface applyInternal(NumberInterface[] params) { - return FUNCTION_EXP.apply(FUNCTION_LN.apply(params[0]).multiply(params[1])); - } - }); /** * The absolute value function, abs(-3) = 3 */ @@ -157,49 +141,6 @@ public class StandardPlugin extends Plugin { return params[0].multiply((new NaiveNumber(params[0].signum())).promoteTo(params[0].getClass())); } }; - /** - * The exponential function, exp(1) = e^1 = 2.71... - */ - public static final Function FUNCTION_EXP = new Function() { - @Override - protected boolean matchesParams(NumberInterface[] params) { - return params.length == 1; - } - - @Override - protected NumberInterface applyInternal(NumberInterface[] params) { - NumberInterface maxError = getMaxError(params[0]); - int n = 0; - if(params[0].signum() <= 0){ - NumberInterface currentTerm = NaiveNumber.ONE.promoteTo(params[0].getClass()), sum = currentTerm; - while(FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0){ - n++; - currentTerm = currentTerm.multiply(params[0]).divide((new NaiveNumber(n)).promoteTo(params[0].getClass())); - sum = sum.add(currentTerm); - } - return sum; - } - else{ - //We need n such that x^(n+1) * 3^ceil(x) <= maxError * (n+1)!. - //right and left refer to lhs and rhs in the above inequality. - NumberInterface sum = NaiveNumber.ONE.promoteTo(params[0].getClass()); - NumberInterface nextNumerator = params[0]; - NumberInterface left = params[0].multiply((new NaiveNumber(3)).promoteTo(params[0].getClass()).intPow(params[0].ceiling().intValue())), right = maxError; - do{ - sum = sum.add(nextNumerator.divide(factorial(params[0].getClass(), n+1))); - n++; - nextNumerator = nextNumerator.multiply(params[0]); - left = left.multiply(params[0]); - NumberInterface nextN = (new NaiveNumber(n+1)).promoteTo(params[0].getClass()); - right = right.multiply(nextN); - //System.out.println(left + ", " + right); - } - while(left.compareTo(right) > 0); - //System.out.println(n+1); - return sum; - } - } - }; /** * The natural log function. */ @@ -291,109 +232,6 @@ public class StandardPlugin extends Plugin { return OP_CARET.getFunction().apply(params[0], ((new NaiveNumber(0.5)).promoteTo(params[0].getClass()))); } }; - - /** - * The sine function (the argument is interpreted in radians). - */ - public final Function functionSin = new Function() { - @Override - protected boolean matchesParams(NumberInterface[] params) { - return params.length == 1; - } - - @Override - protected NumberInterface applyInternal(NumberInterface[] params) { - NumberInterface pi = getPi(params[0].getClass()); - NumberInterface twoPi = pi.multiply(new NaiveNumber(2).promoteTo(pi.getClass())); - NumberInterface theta = getSmallAngle(params[0], pi); - //System.out.println(theta); - if(theta.compareTo(pi.multiply(new NaiveNumber(1.5).promoteTo(twoPi.getClass()))) >= 0){ - theta = theta.subtract(twoPi); - } - else if(theta.compareTo(pi.divide(new NaiveNumber(2).promoteTo(pi.getClass()))) > 0){ - theta = pi.subtract(theta); - } - //System.out.println(theta); - return sinTaylor(theta); - } - }; - - /** - * The cosine function (the argument is in radians). - */ - public final Function functionCos = new Function() { - @Override - protected boolean matchesParams(NumberInterface[] params) { - return params.length == 1; - } - - @Override - protected NumberInterface applyInternal(NumberInterface[] params) { - return functionSin.apply(getPi(params[0].getClass()).divide(new NaiveNumber(2).promoteTo(params[0].getClass())) - .subtract(params[0])); - } - }; - - /** - * The tangent function (the argument is in radians). - */ - public final Function functionTan = new Function() { - @Override - protected boolean matchesParams(NumberInterface[] params) { - return params.length == 1; - } - - @Override - protected NumberInterface applyInternal(NumberInterface[] params) { - return functionSin.apply(params[0]).divide(functionCos.apply(params[0])); - } - }; - - /** - * The secant function (the argument is in radians). - */ - public final Function functionSec = new Function() { - @Override - protected boolean matchesParams(NumberInterface[] params) { - return params.length == 1; - } - - @Override - protected NumberInterface applyInternal(NumberInterface[] params) { - return NaiveNumber.ONE.promoteTo(params[0].getClass()).divide(functionCos.apply(params[0])); - } - }; - - /** - * The cosecant function (the argument is in radians). - */ - public final Function functionCsc = new Function() { - @Override - protected boolean matchesParams(NumberInterface[] params) { - return params.length == 1; - } - - @Override - protected NumberInterface applyInternal(NumberInterface[] params) { - return NaiveNumber.ONE.promoteTo(params[0].getClass()).divide(functionSin.apply(params[0])); - } - }; - - /** - * The cotangent function (the argument is in radians). - */ - public final Function functionCot = new Function() { - @Override - protected boolean matchesParams(NumberInterface[] params) { - return params.length == 1; - } - - @Override - protected NumberInterface applyInternal(NumberInterface[] params) { - return functionCos.apply(params[0]).divide(functionCos.apply(params[0])); - } - }; - /** * The implementation for double-based naive numbers. */ @@ -408,7 +246,6 @@ public class StandardPlugin extends Plugin { return new NaiveNumber(Math.PI); } }; - /** * The implementation for the infinite-precision BigDecimal. */ @@ -425,19 +262,19 @@ public class StandardPlugin extends Plugin { NumberInterface L = new PreciseNumber("13591409"); NumberInterface X = M; NumberInterface sum = L; - int termsNeeded = C.getMaxPrecision()/13 + 1; + int termsNeeded = C.getMaxPrecision() / 13 + 1; NumberInterface lSummand = new PreciseNumber("545140134"); NumberInterface xMultiplier = new PreciseNumber("262537412") .multiply(new PreciseNumber("1000000000")) .add(new PreciseNumber("640768000")) .negate(); - for(int i = 0; i < termsNeeded; i++){ + for (int i = 0; i < termsNeeded; i++) { M = M - .multiply(new NaiveNumber(12*i+2).promoteTo(PreciseNumber.class)) - .multiply(new NaiveNumber(12*i+6).promoteTo(PreciseNumber.class)) - .multiply(new NaiveNumber(12*i+10).promoteTo(PreciseNumber.class)) - .divide(new NaiveNumber(Math.pow(i+1,3)).promoteTo(PreciseNumber.class)); + .multiply(new NaiveNumber(12 * i + 2).promoteTo(PreciseNumber.class)) + .multiply(new NaiveNumber(12 * i + 6).promoteTo(PreciseNumber.class)) + .multiply(new NaiveNumber(12 * i + 10).promoteTo(PreciseNumber.class)) + .divide(new NaiveNumber(Math.pow(i + 1, 3)).promoteTo(PreciseNumber.class)); L = L.add(lSummand); X = X.multiply(xMultiplier); sum = sum.add(M.multiply(L).divide(X)); @@ -445,6 +282,158 @@ public class StandardPlugin extends Plugin { return C.divide(sum); } }; + private static final HashMap, ArrayList> FACTORIAL_LISTS = new HashMap<>(); + /** + * The exponential function, exp(1) = e^1 = 2.71... + */ + public static final Function FUNCTION_EXP = new Function() { + @Override + protected boolean matchesParams(NumberInterface[] params) { + return params.length == 1; + } + + @Override + protected NumberInterface applyInternal(NumberInterface[] params) { + NumberInterface maxError = getMaxError(params[0]); + int n = 0; + if (params[0].signum() <= 0) { + NumberInterface currentTerm = NaiveNumber.ONE.promoteTo(params[0].getClass()), sum = currentTerm; + while (FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0) { + n++; + currentTerm = currentTerm.multiply(params[0]).divide((new NaiveNumber(n)).promoteTo(params[0].getClass())); + sum = sum.add(currentTerm); + } + return sum; + } else { + //We need n such that x^(n+1) * 3^ceil(x) <= maxError * (n+1)!. + //right and left refer to lhs and rhs in the above inequality. + NumberInterface sum = NaiveNumber.ONE.promoteTo(params[0].getClass()); + NumberInterface nextNumerator = params[0]; + NumberInterface left = params[0].multiply((new NaiveNumber(3)).promoteTo(params[0].getClass()).intPow(params[0].ceiling().intValue())), right = maxError; + do { + sum = sum.add(nextNumerator.divide(factorial(params[0].getClass(), n + 1))); + n++; + nextNumerator = nextNumerator.multiply(params[0]); + left = left.multiply(params[0]); + NumberInterface nextN = (new NaiveNumber(n + 1)).promoteTo(params[0].getClass()); + right = right.multiply(nextN); + //System.out.println(left + ", " + right); + } + while (left.compareTo(right) > 0); + //System.out.println(n+1); + return sum; + } + } + }; + /** + * The caret / pow operator, ^ + */ + public static final Operator OP_CARET = new Operator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2, new Function() { + @Override + protected boolean matchesParams(NumberInterface[] params) { + return params.length == 2; + } + + @Override + protected NumberInterface applyInternal(NumberInterface[] params) { + return FUNCTION_EXP.apply(FUNCTION_LN.apply(params[0]).multiply(params[1])); + } + }); + /** + * The sine function (the argument is interpreted in radians). + */ + public final Function functionSin = new Function() { + @Override + protected boolean matchesParams(NumberInterface[] params) { + return params.length == 1; + } + + @Override + protected NumberInterface applyInternal(NumberInterface[] params) { + NumberInterface pi = getPi(params[0].getClass()); + NumberInterface twoPi = pi.multiply(new NaiveNumber(2).promoteTo(pi.getClass())); + NumberInterface theta = getSmallAngle(params[0], pi); + //System.out.println(theta); + if (theta.compareTo(pi.multiply(new NaiveNumber(1.5).promoteTo(twoPi.getClass()))) >= 0) { + theta = theta.subtract(twoPi); + } else if (theta.compareTo(pi.divide(new NaiveNumber(2).promoteTo(pi.getClass()))) > 0) { + theta = pi.subtract(theta); + } + //System.out.println(theta); + return sinTaylor(theta); + } + }; + /** + * The cosine function (the argument is in radians). + */ + public final Function functionCos = new Function() { + @Override + protected boolean matchesParams(NumberInterface[] params) { + return params.length == 1; + } + + @Override + protected NumberInterface applyInternal(NumberInterface[] params) { + return functionSin.apply(getPi(params[0].getClass()).divide(new NaiveNumber(2).promoteTo(params[0].getClass())) + .subtract(params[0])); + } + }; + /** + * The tangent function (the argument is in radians). + */ + public final Function functionTan = new Function() { + @Override + protected boolean matchesParams(NumberInterface[] params) { + return params.length == 1; + } + + @Override + protected NumberInterface applyInternal(NumberInterface[] params) { + return functionSin.apply(params[0]).divide(functionCos.apply(params[0])); + } + }; + /** + * The secant function (the argument is in radians). + */ + public final Function functionSec = new Function() { + @Override + protected boolean matchesParams(NumberInterface[] params) { + return params.length == 1; + } + + @Override + protected NumberInterface applyInternal(NumberInterface[] params) { + return NaiveNumber.ONE.promoteTo(params[0].getClass()).divide(functionCos.apply(params[0])); + } + }; + /** + * The cosecant function (the argument is in radians). + */ + public final Function functionCsc = new Function() { + @Override + protected boolean matchesParams(NumberInterface[] params) { + return params.length == 1; + } + + @Override + protected NumberInterface applyInternal(NumberInterface[] params) { + return NaiveNumber.ONE.promoteTo(params[0].getClass()).divide(functionSin.apply(params[0])); + } + }; + /** + * The cotangent function (the argument is in radians). + */ + public final Function functionCot = new Function() { + @Override + protected boolean matchesParams(NumberInterface[] params) { + return params.length == 1; + } + + @Override + protected NumberInterface applyInternal(NumberInterface[] params) { + return functionCos.apply(params[0]).divide(functionCos.apply(params[0])); + } + }; public StandardPlugin(PluginManager manager) { super(manager); @@ -476,6 +465,64 @@ public class StandardPlugin extends Plugin { return (new NaiveNumber(10)).promoteTo(number.getClass()).intPow(-number.getMaxPrecision()); } + /** + * A factorial function that uses memoization for each number class; it efficiently + * computes factorials of non-negative integers. + * + * @param numberClass type of number to return. + * @param n non-negative integer. + * @return a number of numClass with value n factorial. + */ + public static NumberInterface factorial(Class numberClass, int n) { + if (!FACTORIAL_LISTS.containsKey(numberClass)) { + FACTORIAL_LISTS.put(numberClass, new ArrayList<>()); + FACTORIAL_LISTS.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass)); + FACTORIAL_LISTS.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass)); + } + ArrayList list = FACTORIAL_LISTS.get(numberClass); + if (n >= list.size()) { + while (list.size() < n + 16) { + list.add(list.get(list.size() - 1).multiply(new NaiveNumber(list.size()).promoteTo(numberClass))); + } + } + return list.get(n); + } + + /** + * Returns the value of the Taylor series for sin (centered at 0) at x. + * + * @param x where the series is evaluated. + * @return the value of the series + */ + private static NumberInterface sinTaylor(NumberInterface x) { + NumberInterface power = x, multiplier = x.multiply(x).negate(), currentTerm = x, sum = x; + NumberInterface maxError = getMaxError(x); + int n = 1; + do { + n += 2; + power = power.multiply(multiplier); + currentTerm = power.divide(factorial(x.getClass(), n)); + sum = sum.add(currentTerm); + } while (FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0); + return sum; + } + + /** + * Returns an equivalent angle in the interval [0, 2pi) + * + * @param phi an angle (in radians). + * @return theta in [0, 2pi) that differs from phi by a multiple of 2pi. + */ + private static NumberInterface getSmallAngle(NumberInterface phi, NumberInterface pi) { + NumberInterface twoPi = pi.multiply(new NaiveNumber("2").promoteTo(phi.getClass())); + NumberInterface theta = FUNCTION_ABS.apply(phi).subtract(twoPi + .multiply(FUNCTION_ABS.apply(phi).divide(twoPi).floor())); //Now theta is in [0, 2pi). + if (phi.signum() < 0) { + theta = twoPi.subtract(theta); + } + return theta; + } + @Override public void onEnable() { registerNumberImplementation("naive", IMPLEMENTATION_NAIVE); @@ -505,59 +552,4 @@ public class StandardPlugin extends Plugin { public void onDisable() { } - - /** - * A factorial function that uses memoization for each number class; it efficiently - * computes factorials of non-negative integers. - * @param numberClass type of number to return. - * @param n non-negative integer. - * @return a number of numClass with value n factorial. - */ - public static NumberInterface factorial(Class numberClass, int n){ - if(!FACTORIAL_LISTS.containsKey(numberClass)){ - FACTORIAL_LISTS.put(numberClass, new ArrayList<>()); - FACTORIAL_LISTS.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass)); - FACTORIAL_LISTS.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass)); - } - ArrayList list = FACTORIAL_LISTS.get(numberClass); - if(n >= list.size()){ - while(list.size() < n + 16){ - list.add(list.get(list.size()-1).multiply(new NaiveNumber(list.size()).promoteTo(numberClass))); - } - } - return list.get(n); - } - - /** - * Returns the value of the Taylor series for sin (centered at 0) at x. - * @param x where the series is evaluated. - * @return the value of the series - */ - private static NumberInterface sinTaylor(NumberInterface x){ - NumberInterface power = x, multiplier = x.multiply(x).negate(), currentTerm = x, sum = x; - NumberInterface maxError = getMaxError(x); - int n = 1; - do{ - n += 2; - power = power.multiply(multiplier); - currentTerm = power.divide(factorial(x.getClass(), n)); - sum = sum.add(currentTerm); - } while (FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0); - return sum; - } - - /** - * Returns an equivalent angle in the interval [0, 2pi) - * @param phi an angle (in radians). - * @return theta in [0, 2pi) that differs from phi by a multiple of 2pi. - */ - private static NumberInterface getSmallAngle(NumberInterface phi, NumberInterface pi){ - NumberInterface twoPi = pi.multiply(new NaiveNumber("2").promoteTo(phi.getClass())); - NumberInterface theta = FUNCTION_ABS.apply(phi).subtract(twoPi - .multiply(FUNCTION_ABS.apply(phi).divide(twoPi).floor())); //Now theta is in [0, 2pi). - if(phi.signum() < 0){ - theta = twoPi.subtract(theta); - } - return theta; - } } diff --git a/src/main/java/org/nwapw/abacus/window/Window.java b/src/main/java/org/nwapw/abacus/window/Window.java index 79749f9..d7eeef0 100644 --- a/src/main/java/org/nwapw/abacus/window/Window.java +++ b/src/main/java/org/nwapw/abacus/window/Window.java @@ -250,6 +250,6 @@ public class Window extends JFrame { @Override public void setVisible(boolean b) { super.setVisible(b); - if(b) inputField.requestFocusInWindow(); + if (b) inputField.requestFocusInWindow(); } }