From 7faef42dea826684084479716da9ba79bfe601a5 Mon Sep 17 00:00:00 2001 From: serso Date: Wed, 20 Jan 2016 10:46:23 +0100 Subject: [PATCH] Engine refactor --- .../android/calculator/AndroidCalculator.java | 17 +- .../android/calculator/BaseNumberBuilder.java | 6 +- .../android/calculator/Calculator.java | 4 +- .../calculator/CalculatorApplication.java | 22 +- .../calculator/CalculatorEventType.java | 10 - .../android/calculator/CalculatorImpl.java | 58 ++-- .../calculator/ConversionMenuItem.java | 2 +- .../solovyev/android/calculator/Editor.java | 13 +- .../solovyev/android/calculator/Engine.java | 275 ++++++++---------- .../android/calculator/LiteNumberBuilder.java | 4 +- .../android/calculator/NumberBuilder.java | 23 +- .../preferences/PreferencesFragment.java | 22 +- .../calculator/view/AngleUnitsButton.java | 2 +- .../view/CalculatorAdditionalTitle.java | 65 ----- .../view/NumeralBaseConverterDialog.java | 8 +- .../calculator/view/NumeralBasesButton.java | 2 +- .../calculator/wizard/CalculatorMode.java | 12 +- app/src/main/res/values/preferences.xml | 25 -- .../main/res/xml/preferences_appearance.xml | 2 +- .../main/res/xml/preferences_calculations.xml | 12 +- .../calculator/AbstractCalculatorTest.java | 7 +- .../calculator/CalculatorTestUtils.java | 36 ++- .../FromJsclSimplifyTextProcessorTest.java | 2 +- .../calculator/model/AndroidEngineTest.java | 2 +- .../calculator/model/ComparisonTest.java | 2 +- .../calculator/model/EvaluateTest.java | 2 +- .../calculator/model/NumeralBaseTest.java | 28 +- .../model/ToJsclTextProcessorTest.java | 4 +- 28 files changed, 280 insertions(+), 387 deletions(-) delete mode 100644 app/src/main/java/org/solovyev/android/calculator/view/CalculatorAdditionalTitle.java diff --git a/app/src/main/java/org/solovyev/android/calculator/AndroidCalculator.java b/app/src/main/java/org/solovyev/android/calculator/AndroidCalculator.java index f341570c..bab9f656 100644 --- a/app/src/main/java/org/solovyev/android/calculator/AndroidCalculator.java +++ b/app/src/main/java/org/solovyev/android/calculator/AndroidCalculator.java @@ -40,11 +40,6 @@ import javax.annotation.Nullable; import jscl.NumeralBase; import jscl.math.Generic; -/** - * User: serso - * Date: 9/22/12 - * Time: 5:42 PM - */ public class AndroidCalculator implements Calculator, CalculatorEventListener, SharedPreferences.OnSharedPreferenceChangeListener { @Nonnull @@ -61,14 +56,6 @@ public class AndroidCalculator implements Calculator, CalculatorEventListener, S PreferenceManager.getDefaultSharedPreferences(application).registerOnSharedPreferenceChangeListener(this); } - /* - ********************************************************************** - * - * DELEGATED TO CALCULATOR - * - ********************************************************************** - */ - @Override @Nonnull public CalculatorEventData evaluate(@Nonnull JsclOperation operation, @Nonnull String expression) { @@ -127,8 +114,8 @@ public class AndroidCalculator implements Calculator, CalculatorEventListener, S } @Override - public void init() { - this.calculator.init(); + public void init(@Nonnull Executor initThread) { + this.calculator.init(initThread); final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); this.calculator.setCalculateOnFly(Preferences.Calculations.calculateOnFly.getPreference(prefs)); diff --git a/app/src/main/java/org/solovyev/android/calculator/BaseNumberBuilder.java b/app/src/main/java/org/solovyev/android/calculator/BaseNumberBuilder.java index 54d12f97..0e22f144 100644 --- a/app/src/main/java/org/solovyev/android/calculator/BaseNumberBuilder.java +++ b/app/src/main/java/org/solovyev/android/calculator/BaseNumberBuilder.java @@ -50,7 +50,7 @@ public abstract class BaseNumberBuilder { protected BaseNumberBuilder(@Nonnull Engine engine) { this.engine = engine; - this.nb = engine.getNumeralBase(); + this.nb = engine.getMathEngine().getNumeralBase(); } /** @@ -94,12 +94,12 @@ public abstract class BaseNumberBuilder { } public boolean isHexMode() { - return nb == NumeralBase.hex || (nb == null && engine.getNumeralBase() == NumeralBase.hex); + return nb == NumeralBase.hex || (nb == null && engine.getMathEngine().getNumeralBase() == NumeralBase.hex); } @Nonnull protected NumeralBase getNumeralBase() { - return nb == null ? engine.getNumeralBase() : nb; + return nb == null ? engine.getMathEngine().getNumeralBase() : nb; } public abstract int process(@Nonnull SpannableStringBuilder sb, @Nonnull MathType.Result result); diff --git a/app/src/main/java/org/solovyev/android/calculator/Calculator.java b/app/src/main/java/org/solovyev/android/calculator/Calculator.java index 0e22c317..6c9ce478 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Calculator.java +++ b/app/src/main/java/org/solovyev/android/calculator/Calculator.java @@ -24,6 +24,8 @@ package org.solovyev.android.calculator; import org.solovyev.android.calculator.jscl.JsclOperation; +import java.util.concurrent.Executor; + import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -37,7 +39,7 @@ import jscl.math.Generic; */ public interface Calculator extends CalculatorEventContainer { - void init(); + void init(@Nonnull Executor initThread); /* ********************************************************************** diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java index a10fab8b..2f6b5b91 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java @@ -26,9 +26,10 @@ import android.content.SharedPreferences; import android.os.Handler; import android.preference.PreferenceManager; import android.util.Log; + import com.squareup.leakcanary.LeakCanary; import com.squareup.otto.Bus; -import jscl.MathEngine; + import org.acra.ACRA; import org.acra.ACRAConfiguration; import org.acra.sender.HttpSender; @@ -41,14 +42,17 @@ import org.solovyev.android.calculator.plot.AndroidCalculatorPlotter; import org.solovyev.android.calculator.plot.CalculatorPlotterImpl; import org.solovyev.common.msg.MessageType; -import javax.annotation.Nonnull; -import javax.inject.Inject; -import javax.inject.Named; import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.concurrent.Executor; +import javax.annotation.Nonnull; +import javax.inject.Inject; +import javax.inject.Named; + +import jscl.MathEngine; + public class CalculatorApplication extends android.app.Application implements SharedPreferences.OnSharedPreferenceChangeListener { @Inject @@ -79,6 +83,9 @@ public class CalculatorApplication extends android.app.Application implements Sh @Inject Calculator calculator; + @Inject + Engine engine; + @Inject Keyboard keyboard; @@ -103,6 +110,7 @@ public class CalculatorApplication extends android.app.Application implements Sh .appModule(new AppModule(this)) .build(); component.inject(this); + editor.init(); history.init(initThread); onPostCreate(preferences, languages); @@ -116,7 +124,7 @@ public class CalculatorApplication extends android.app.Application implements Sh App.getGa().reportInitially(preferences); Locator.getInstance().init(calculator, - new Engine(this), + engine, new AndroidCalculatorClipboard(this), new AndroidCalculatorNotifier(this), errorReporter, @@ -130,7 +138,7 @@ public class CalculatorApplication extends android.app.Application implements Sh calculator.addCalculatorEventListener(listener); } - Locator.getInstance().getCalculator().init(); + calculator.init(initThread); initThread.execute(new Runnable() { @Override @@ -143,7 +151,7 @@ public class CalculatorApplication extends android.app.Application implements Sh private void warmUpEngine() { try { // warm-up engine - MathEngine mathEngine = Locator.getInstance().getEngine().getMathEngine(); + final MathEngine mathEngine = engine.getMathEngine(); mathEngine.evaluate("1+1"); mathEngine.evaluate("1*1"); } catch (Throwable e) { diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorEventType.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorEventType.java index 048734c0..d3efe39c 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorEventType.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorEventType.java @@ -70,16 +70,6 @@ public enum CalculatorEventType { conversion_finished, - /* - ********************************************************************** - * - * ENGINE - * - ********************************************************************** - */ - - engine_preferences_changed, - /* ********************************************************************** * diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java index 14d57501..68768352 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java @@ -24,17 +24,10 @@ package org.solovyev.android.calculator; import android.text.TextUtils; import android.util.Log; + import com.squareup.otto.Bus; import com.squareup.otto.Subscribe; -import jscl.AbstractJsclArithmeticException; -import jscl.MathEngine; -import jscl.NumeralBase; -import jscl.NumeralBaseException; -import jscl.math.Generic; -import jscl.math.function.Function; -import jscl.math.function.IConstant; -import jscl.math.operator.Operator; -import jscl.text.ParseInterruptedException; + import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.model.Var; import org.solovyev.android.calculator.text.TextProcessor; @@ -47,40 +40,30 @@ import org.solovyev.common.text.Strings; import org.solovyev.common.units.ConversionException; import org.solovyev.common.units.Conversions; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicLong; -/** - * User: Solovyev_S - * Date: 20.09.12 - * Time: 16:42 - */ -public class CalculatorImpl implements Calculator, CalculatorEventListener { +import javax.annotation.Nonnull; +import javax.annotation.Nullable; - /* - ********************************************************************** - * - * CONSTANTS - * - ********************************************************************** - */ +import jscl.AbstractJsclArithmeticException; +import jscl.MathEngine; +import jscl.NumeralBase; +import jscl.NumeralBaseException; +import jscl.math.Generic; +import jscl.math.function.Function; +import jscl.math.function.IConstant; +import jscl.math.operator.Operator; +import jscl.text.ParseInterruptedException; + +public class CalculatorImpl implements Calculator, CalculatorEventListener { // one minute private static final long PREFERENCE_CHECK_INTERVAL = 1000L * 60L; - /* - ********************************************************************** - * - * FIELDS - * - ********************************************************************** - */ - @Nonnull private final CalculatorEventContainer calculatorEventContainer = new ListCalculatorEventContainer(); @@ -205,8 +188,8 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { } @Override - public void init() { - Locator.getInstance().getEngine().init(); + public void init(@Nonnull Executor initThread) { + Locator.getInstance().getEngine().init(initThread); } @Override @@ -373,7 +356,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { final CalculatorEventData eventDataId = nextEventData(); final DisplayState displayViewState = App.getDisplay().getState(); - final NumeralBase from = Locator.getInstance().getEngine().getNumeralBase(); + final NumeralBase from = Locator.getInstance().getEngine().getMathEngine().getNumeralBase(); calculationsExecutor.execute(new Runnable() { @Override @@ -524,11 +507,6 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { case function_removed: evaluate(); break; - - case engine_preferences_changed: - evaluate(calculatorEventData.getSequenceId()); - break; - case use_constant: final IConstant constant = (IConstant) data; Locator.getInstance().getKeyboard().buttonPressed(constant.getName()); diff --git a/app/src/main/java/org/solovyev/android/calculator/ConversionMenuItem.java b/app/src/main/java/org/solovyev/android/calculator/ConversionMenuItem.java index a5a10e98..8342aa8a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/ConversionMenuItem.java +++ b/app/src/main/java/org/solovyev/android/calculator/ConversionMenuItem.java @@ -56,7 +56,7 @@ enum ConversionMenuItem implements AMenuItem { if (operation == JsclOperation.numeric) { if (generic.getConstants().isEmpty()) { // conversion possible => return true - final NumeralBase fromNumeralBase = Locator.getInstance().getEngine().getNumeralBase(); + final NumeralBase fromNumeralBase = Locator.getInstance().getEngine().getMathEngine().getNumeralBase(); if (fromNumeralBase != toNumeralBase) { result = Locator.getInstance().getCalculator().isConversionPossible(generic, fromNumeralBase, this.toNumeralBase); } diff --git a/app/src/main/java/org/solovyev/android/calculator/Editor.java b/app/src/main/java/org/solovyev/android/calculator/Editor.java index c7d63fbb..71adbea7 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Editor.java +++ b/app/src/main/java/org/solovyev/android/calculator/Editor.java @@ -25,6 +25,7 @@ package org.solovyev.android.calculator; import android.content.SharedPreferences; import com.squareup.otto.Bus; +import com.squareup.otto.Subscribe; import org.solovyev.android.Check; import org.solovyev.android.calculator.text.TextProcessorEditorResult; @@ -40,7 +41,6 @@ import static java.lang.Math.min; @Singleton public class Editor { - private static final String TAG = App.subTag("Editor"); @Nullable private final EditorTextProcessor textProcessor; @Nullable @@ -55,6 +55,10 @@ public class Editor { textProcessor = new EditorTextProcessor(preferences); } + public void init() { + bus.register(this); + } + public static int clamp(int selection, @Nonnull CharSequence text) { return clamp(selection, text.length()); } @@ -214,6 +218,13 @@ public class Editor { return onSelectionChanged(EditorState.forNewSelection(state, clamp(selection, state.text))); } + @Subscribe + public void onEngineChanged(@Nonnull Engine.ChangedEvent e) { + // this will effectively apply new formatting (if f.e. grouping separator has changed) and + // will start new evaluation + onTextChanged(getState()); + } + public static class ChangedEvent { @Nonnull public final EditorState oldState; diff --git a/app/src/main/java/org/solovyev/android/calculator/Engine.java b/app/src/main/java/org/solovyev/android/calculator/Engine.java index ad30248d..a1905ff1 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Engine.java +++ b/app/src/main/java/org/solovyev/android/calculator/Engine.java @@ -23,9 +23,30 @@ package org.solovyev.android.calculator; import android.app.Application; -import android.content.Context; import android.content.SharedPreferences; -import android.preference.PreferenceManager; + +import com.squareup.otto.Bus; + +import org.solovyev.android.calculator.model.AndroidMathEntityDao; +import org.solovyev.android.calculator.model.Functions; +import org.solovyev.android.calculator.model.Vars; +import org.solovyev.android.prefs.BooleanPreference; +import org.solovyev.android.prefs.IntegerPreference; +import org.solovyev.android.prefs.Preference; +import org.solovyev.android.prefs.StringPreference; +import org.solovyev.common.text.EnumMapper; +import org.solovyev.common.text.NumberMapper; +import org.solovyev.common.text.Strings; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.Executor; + +import javax.annotation.Nonnull; +import javax.inject.Inject; +import javax.inject.Singleton; + import jscl.AngleUnit; import jscl.JsclMathEngine; import jscl.MathEngine; @@ -33,48 +54,10 @@ import jscl.NumeralBase; import jscl.math.function.Function; import jscl.math.function.IConstant; import jscl.math.operator.Operator; -import org.solovyev.android.calculator.model.AndroidMathEntityDao; -import org.solovyev.android.calculator.model.Functions; -import org.solovyev.android.calculator.model.Vars; -import org.solovyev.android.prefs.BooleanPreference; -import org.solovyev.android.prefs.Preference; -import org.solovyev.android.prefs.StringPreference; -import org.solovyev.common.text.EnumMapper; -import org.solovyev.common.text.NumberMapper; -import org.solovyev.common.text.Strings; - -import javax.annotation.Nonnull; -import java.text.DecimalFormatSymbols; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +@Singleton public class Engine implements SharedPreferences.OnSharedPreferenceChangeListener { - private static final String MULTIPLICATION_SIGN_DEFAULT = "×"; - - private static final String GROUPING_SEPARATOR_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_calc_grouping_separator"; - - private static final String MULTIPLICATION_SIGN_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_calc_multiplication_sign"; - - private static final String SCIENCE_NOTATION_P_KEY = "calculation.output.science_notation"; - private static final boolean SCIENCE_NOTATION_DEFAULT = false; - - private static final String ROUND_RESULT_P_KEY = "org.solovyev.android.calculator.CalculatorModel_round_result"; - private static final boolean ROUND_RESULT_DEFAULT = true; - - private static final String RESULT_PRECISION_P_KEY = "org.solovyev.android.calculator.CalculatorModel_result_precision"; - private static final String RESULT_PRECISION_DEFAULT = "5"; - - private static final String NUMERAL_BASES_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_numeral_bases"; - private static final String NUMERAL_BASES_DEFAULT = "dec"; - - private static final String ANGLE_UNITS_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_angle_units"; - private static final String ANGLE_UNITS_DEFAULT = "deg"; - @Nonnull - private final Context context; - @Nonnull - private final Object lock; @Nonnull private final MathEngine mathEngine; @Nonnull @@ -85,12 +68,16 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene private final EntitiesRegistry operatorsRegistry; @Nonnull private final EntitiesRegistry postfixFunctionsRegistry; + @Inject + SharedPreferences preferences; + @Inject + Bus bus; + @Inject + ErrorReporter errorReporter; @Nonnull - private String multiplicationSign = MULTIPLICATION_SIGN_DEFAULT; + private String multiplicationSign = Preferences.multiplicationSign.getDefaultValue(); - public Engine(@Nonnull Context context, @Nonnull MathEngine mathEngine, @Nonnull EntitiesRegistry varsRegistry, @Nonnull EntitiesRegistry functionsRegistry, @Nonnull EntitiesRegistry operatorsRegistry, @Nonnull EntitiesRegistry postfixFunctionsRegistry) { - this.context = context; - this.lock = new Object(); + public Engine(@Nonnull MathEngine mathEngine, @Nonnull EntitiesRegistry varsRegistry, @Nonnull EntitiesRegistry functionsRegistry, @Nonnull EntitiesRegistry operatorsRegistry, @Nonnull EntitiesRegistry postfixFunctionsRegistry) { this.mathEngine = mathEngine; this.varsRegistry = varsRegistry; this.functionsRegistry = functionsRegistry; @@ -98,13 +85,10 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene this.postfixFunctionsRegistry = postfixFunctionsRegistry; } + @Inject public Engine(@Nonnull Application application) { this.mathEngine = JsclMathEngine.getInstance(); - this.context = application; - PreferenceManager.getDefaultSharedPreferences(application).registerOnSharedPreferenceChangeListener(this); - - this.lock = new Object(); this.mathEngine.setRoundResult(true); this.mathEngine.setUseGroupingSeparator(true); @@ -114,34 +98,38 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene this.postfixFunctionsRegistry = new PostfixFunctionsRegistry(mathEngine.getPostfixFunctionsRegistry(), new AndroidMathEntityDao<>(null, application, null)); } - @Nonnull - public static NumeralBase getNumeralBaseFromPrefs(@Nonnull SharedPreferences preferences) { - return Preferences.numeralBase.getPreference(preferences); + private static void migratePreference(@Nonnull SharedPreferences preferences, @Nonnull BooleanPreference preference, @Nonnull String oldKey, @Nonnull SharedPreferences.Editor editor) { + if (!preferences.contains(oldKey)) { + return; + } + editor.putBoolean(preference.getKey(), preferences.getBoolean(oldKey, false)); } - @Nonnull - public static AngleUnit getAngleUnitsFromPrefs(@Nonnull SharedPreferences preferences) { - return Preferences.angleUnit.getPreference(preferences); + private static void migratePreference(@Nonnull SharedPreferences preferences, @Nonnull StringPreference preference, @Nonnull String oldKey, @Nonnull SharedPreferences.Editor editor) { + if (!preferences.contains(oldKey)) { + return; + } + editor.putString(preference.getKey(), preferences.getString(oldKey, null)); } @Nonnull public EntitiesRegistry getVarsRegistry() { - return this.varsRegistry; + return varsRegistry; } @Nonnull public EntitiesRegistry getFunctionsRegistry() { - return this.functionsRegistry; + return functionsRegistry; } @Nonnull public EntitiesRegistry getOperatorsRegistry() { - return this.operatorsRegistry; + return operatorsRegistry; } @Nonnull public EntitiesRegistry getPostfixFunctionsRegistry() { - return this.postfixFunctionsRegistry; + return postfixFunctionsRegistry; } @Nonnull @@ -149,45 +137,75 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene return mathEngine; } - public void init() { - synchronized (lock) { - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - resetPreferences(preferences); - safeLoadRegistry(varsRegistry); - safeLoadRegistry(functionsRegistry); - safeLoadRegistry(operatorsRegistry); - safeLoadRegistry(postfixFunctionsRegistry); - } + public void init(@Nonnull Executor initThread) { + checkPreferences(); + preferences.registerOnSharedPreferenceChangeListener(this); + applyPreferences(); + initThread.execute(new Runnable() { + @Override + public void run() { + initAsync(); + } + }); } - public void resetPreferences() { - resetPreferences(App.getPreferences()); + private void checkPreferences() { + final int oldVersion; + if (Preferences.version.isSet(preferences)) { + oldVersion = Preferences.version.getPreference(preferences); + } else { + oldVersion = 0; + } + final int newVersion = Preferences.version.getPreference(preferences); + if (oldVersion == newVersion) { + return; + } + final SharedPreferences.Editor editor = preferences.edit(); + if (oldVersion == 0) { + migratePreference(preferences, Preferences.groupingSeparator, "org.solovyev.android.calculator.CalculatorActivity_calc_grouping_separator", editor); + migratePreference(preferences, Preferences.multiplicationSign, "org.solovyev.android.calculator.CalculatorActivity_calc_multiplication_sign", editor); + migratePreference(preferences, Preferences.numeralBase, "org.solovyev.android.calculator.CalculatorActivity_numeral_bases", editor); + migratePreference(preferences, Preferences.angleUnit, "org.solovyev.android.calculator.CalculatorActivity_angle_units", editor); + migratePreference(preferences, Preferences.Output.precision, "org.solovyev.android.calculator.CalculatorModel_result_precision", editor); + migratePreference(preferences, Preferences.Output.scientificNotation, "calculation.output.science_notation", editor); + migratePreference(preferences, Preferences.Output.round, "org.solovyev.android.calculator.CalculatorModel_round_result", editor); + } + Preferences.version.putDefault(preferences); + editor.apply(); + } + + private void initAsync() { + safeLoadRegistry(varsRegistry); + safeLoadRegistry(functionsRegistry); + safeLoadRegistry(operatorsRegistry); + safeLoadRegistry(postfixFunctionsRegistry); } private void safeLoadRegistry(@Nonnull EntitiesRegistry registry) { try { registry.load(); } catch (Exception e) { - Locator.getInstance().getErrorReporter().onException(e); + errorReporter.onException(e); } } - private void resetPreferences(@Nonnull SharedPreferences preferences) { - setPrecision(Preferences.precision.getPreference(preferences)); - setRoundResult(Preferences.roundResult.getPreference(preferences)); - setAngleUnits(getAngleUnitsFromPrefs(preferences)); - setNumeralBase(getNumeralBaseFromPrefs(preferences)); + private void applyPreferences() { + mathEngine.setAngleUnits(Preferences.angleUnit.getPreference(preferences)); + mathEngine.setNumeralBase(Preferences.numeralBase.getPreference(preferences)); setMultiplicationSign(Preferences.multiplicationSign.getPreference(preferences)); - setScienceNotation(Preferences.scienceNotation.getPreference(preferences)); + + mathEngine.setPrecision(Preferences.Output.precision.getPreference(preferences)); + mathEngine.setScienceNotation(Preferences.Output.scientificNotation.getPreference(preferences)); + mathEngine.setRoundResult(Preferences.Output.round.getPreference(preferences)); final String groupingSeparator = Preferences.groupingSeparator.getPreference(preferences); if (Strings.isEmpty(groupingSeparator)) { - setUseGroupingSeparator(false); + mathEngine.setUseGroupingSeparator(false); } else { - setUseGroupingSeparator(true); - setGroupingSeparator(groupingSeparator.charAt(0)); + mathEngine.setUseGroupingSeparator(true); + mathEngine.setGroupingSeparator(groupingSeparator.charAt(0)); } - Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.engine_preferences_changed, null); + bus.post(ChangedEvent.INSTANCE); } @Nonnull @@ -199,99 +217,46 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene this.multiplicationSign = multiplicationSign; } - public void setUseGroupingSeparator(boolean useGroupingSeparator) { - synchronized (lock) { - this.mathEngine.setUseGroupingSeparator(useGroupingSeparator); - } - } - - public void setGroupingSeparator(char groupingSeparator) { - synchronized (lock) { - this.mathEngine.setGroupingSeparator(groupingSeparator); - } - } - - public void setPrecision(@Nonnull Integer precision) { - synchronized (lock) { - this.mathEngine.setPrecision(precision); - } - } - - public void setRoundResult(@Nonnull Boolean round) { - synchronized (lock) { - this.mathEngine.setRoundResult(round); - } - } - - @Nonnull - public AngleUnit getAngleUnits() { - synchronized (lock) { - return this.mathEngine.getAngleUnits(); - } - } - - public void setAngleUnits(@Nonnull AngleUnit angleUnits) { - synchronized (lock) { - this.mathEngine.setAngleUnits(angleUnits); - } - } - - @Nonnull - public NumeralBase getNumeralBase() { - synchronized (lock) { - return this.mathEngine.getNumeralBase(); - } - } - - public void setNumeralBase(@Nonnull NumeralBase numeralBase) { - synchronized (lock) { - this.mathEngine.setNumeralBase(numeralBase); - } - } - - public void setScienceNotation(@Nonnull Boolean scienceNotation) { - synchronized (lock) { - this.mathEngine.setScienceNotation(scienceNotation); - } - } - - public void setDecimalGroupSymbols(@Nonnull DecimalFormatSymbols decimalGroupSymbols) { - synchronized (lock) { - this.mathEngine.setDecimalGroupSymbols(decimalGroupSymbols); - } - } - @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (Preferences.getPreferenceKeys().contains(key)) { - this.resetPreferences(); + applyPreferences(); + } + } + + public static class ChangedEvent { + static final ChangedEvent INSTANCE = new ChangedEvent(); + private ChangedEvent() { } } public static class Preferences { - public static final Preference groupingSeparator = StringPreference.of(GROUPING_SEPARATOR_P_KEY, JsclMathEngine.GROUPING_SEPARATOR_DEFAULT); - public static final Preference multiplicationSign = StringPreference.of(MULTIPLICATION_SIGN_P_KEY, MULTIPLICATION_SIGN_DEFAULT); - public static final Preference precision = StringPreference.ofTypedValue(RESULT_PRECISION_P_KEY, RESULT_PRECISION_DEFAULT, NumberMapper.of(Integer.class)); - public static final Preference roundResult = BooleanPreference.of(ROUND_RESULT_P_KEY, ROUND_RESULT_DEFAULT); - public static final Preference numeralBase = StringPreference.ofTypedValue(NUMERAL_BASES_P_KEY, NUMERAL_BASES_DEFAULT, EnumMapper.of(NumeralBase.class)); - public static final Preference angleUnit = StringPreference.ofTypedValue(ANGLE_UNITS_P_KEY, ANGLE_UNITS_DEFAULT, EnumMapper.of(AngleUnit.class)); - public static final Preference scienceNotation = BooleanPreference.of(SCIENCE_NOTATION_P_KEY, SCIENCE_NOTATION_DEFAULT); - + public static final StringPreference groupingSeparator = StringPreference.of("engine.groupingSeparator", JsclMathEngine.GROUPING_SEPARATOR_DEFAULT); + public static final StringPreference multiplicationSign = StringPreference.of("engine.multiplicationSign", "×"); + public static final StringPreference numeralBase = StringPreference.ofTypedValue("engine.numeralBase", "dec", EnumMapper.of(NumeralBase.class)); + public static final StringPreference angleUnit = StringPreference.ofTypedValue("engine.angleUnit", "deg", EnumMapper.of(AngleUnit.class)); + public static final Preference version = IntegerPreference.of("engine.version", 1); private static final List preferenceKeys = new ArrayList<>(); static { preferenceKeys.add(groupingSeparator.getKey()); preferenceKeys.add(multiplicationSign.getKey()); - preferenceKeys.add(precision.getKey()); - preferenceKeys.add(roundResult.getKey()); preferenceKeys.add(numeralBase.getKey()); preferenceKeys.add(angleUnit.getKey()); - preferenceKeys.add(scienceNotation.getKey()); + preferenceKeys.add(Output.precision.getKey()); + preferenceKeys.add(Output.scientificNotation.getKey()); + preferenceKeys.add(Output.round.getKey()); } @Nonnull public static List getPreferenceKeys() { return Collections.unmodifiableList(preferenceKeys); } + + public static class Output { + public static final StringPreference precision = StringPreference.ofTypedValue("engine.output.precision", "5", NumberMapper.of(Integer.class)); + public static final BooleanPreference scientificNotation = BooleanPreference.of("engine.output.scientificNotation", false); + public static final BooleanPreference round = BooleanPreference.of("engine.output.round", true); + } } } diff --git a/app/src/main/java/org/solovyev/android/calculator/LiteNumberBuilder.java b/app/src/main/java/org/solovyev/android/calculator/LiteNumberBuilder.java index ef4f85a7..db96956d 100644 --- a/app/src/main/java/org/solovyev/android/calculator/LiteNumberBuilder.java +++ b/app/src/main/java/org/solovyev/android/calculator/LiteNumberBuilder.java @@ -40,7 +40,7 @@ public class LiteNumberBuilder extends BaseNumberBuilder { public LiteNumberBuilder(@Nonnull Engine engine) { super(engine); - this.nb = engine.getNumeralBase(); + this.nb = engine.getMathEngine().getNumeralBase(); } @Override @@ -71,7 +71,7 @@ public class LiteNumberBuilder extends BaseNumberBuilder { numberBuilder = null; // must set default numeral base (exit numeral base mode) - nb = engine.getNumeralBase(); + nb = engine.getMathEngine().getNumeralBase(); } } } diff --git a/app/src/main/java/org/solovyev/android/calculator/NumberBuilder.java b/app/src/main/java/org/solovyev/android/calculator/NumberBuilder.java index 946b33a4..75b9f6d8 100644 --- a/app/src/main/java/org/solovyev/android/calculator/NumberBuilder.java +++ b/app/src/main/java/org/solovyev/android/calculator/NumberBuilder.java @@ -23,17 +23,24 @@ package org.solovyev.android.calculator; import android.text.SpannableStringBuilder; + +import org.solovyev.android.calculator.math.MathType; + +import java.util.ArrayList; +import java.util.List; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + import jscl.MathContext; import jscl.MathEngine; import jscl.NumeralBase; import jscl.math.numeric.Real; -import jscl.text.*; -import org.solovyev.android.calculator.math.MathType; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.ArrayList; -import java.util.List; +import jscl.text.DoubleParser; +import jscl.text.JsclIntegerParser; +import jscl.text.MutableInt; +import jscl.text.ParseException; +import jscl.text.Parser; /** * User: serso @@ -193,7 +200,7 @@ public class NumberBuilder extends BaseNumberBuilder { numberBuilder = null; // must set default numeral base (exit numeral base mode) - nb = engine.getNumeralBase(); + nb = engine.getMathEngine().getNumeralBase(); } return replaceNumberInText(sb, number, trimmedChars, localNb, engine.getMathEngine()); diff --git a/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesFragment.java b/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesFragment.java index 61f6e32a..ede0ab26 100644 --- a/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesFragment.java @@ -10,7 +10,12 @@ import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.widget.ListView; -import org.solovyev.android.calculator.*; + +import org.solovyev.android.calculator.AdView; +import org.solovyev.android.calculator.App; +import org.solovyev.android.calculator.Engine; +import org.solovyev.android.calculator.Preferences; +import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.language.Language; import org.solovyev.android.calculator.language.Languages; import org.solovyev.android.checkout.BillingRequests; @@ -18,12 +23,11 @@ import org.solovyev.android.checkout.Checkout; import org.solovyev.android.checkout.ProductTypes; import org.solovyev.android.checkout.RequestListener; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; import java.util.List; -import static org.solovyev.android.calculator.Engine.Preferences.precision; -import static org.solovyev.android.calculator.Engine.Preferences.roundResult; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + import static org.solovyev.android.calculator.wizard.CalculatorWizards.DEFAULT_WIZARD_FLOW; import static org.solovyev.android.wizard.WizardUi.startWizard; @@ -118,7 +122,7 @@ public class PreferencesFragment extends org.solovyev.android.material.preferenc }); final SharedPreferences preferences = App.getPreferences(); - onSharedPreferenceChanged(preferences, roundResult.getKey()); + onSharedPreferenceChanged(preferences, Engine.Preferences.Output.round.getKey()); } private void prepareLanguagePreference(int preference) { @@ -156,10 +160,10 @@ public class PreferencesFragment extends org.solovyev.android.material.preferenc @Override public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { - if (roundResult.getKey().equals(key)) { - final Preference preference = findPreference(precision.getKey()); + if (Engine.Preferences.Output.round.getKey().equals(key)) { + final Preference preference = findPreference(Engine.Preferences.Output.precision.getKey()); if (preference != null) { - preference.setEnabled(preferences.getBoolean(key, roundResult.getDefaultValue())); + preference.setEnabled(preferences.getBoolean(key, Engine.Preferences.Output.round.getDefaultValue())); } } } diff --git a/app/src/main/java/org/solovyev/android/calculator/view/AngleUnitsButton.java b/app/src/main/java/org/solovyev/android/calculator/view/AngleUnitsButton.java index c9e224e8..e10db193 100644 --- a/app/src/main/java/org/solovyev/android/calculator/view/AngleUnitsButton.java +++ b/app/src/main/java/org/solovyev/android/calculator/view/AngleUnitsButton.java @@ -48,7 +48,7 @@ public class AngleUnitsButton extends DirectionDragButton { public AngleUnitsButton(Context context, @Nonnull AttributeSet attrs) { super(context, attrs); - this.angleUnit = Locator.getInstance().getEngine().getAngleUnits(); + this.angleUnit = Locator.getInstance().getEngine().getMathEngine().getAngleUnits(); } @Override diff --git a/app/src/main/java/org/solovyev/android/calculator/view/CalculatorAdditionalTitle.java b/app/src/main/java/org/solovyev/android/calculator/view/CalculatorAdditionalTitle.java deleted file mode 100644 index 421db060..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/view/CalculatorAdditionalTitle.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2013 serso aka se.solovyev - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * Contact details - * - * Email: se.solovyev@gmail.com - * Site: http://se.solovyev.org - */ - -package org.solovyev.android.calculator.view; - -import android.content.Context; -import android.content.SharedPreferences; -import android.util.AttributeSet; -import android.widget.TextView; - -import org.solovyev.android.calculator.Engine; -import org.solovyev.android.calculator.Locator; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * User: serso - * Date: 12/10/11 - * Time: 10:34 PM - */ -public class CalculatorAdditionalTitle extends TextView implements SharedPreferences.OnSharedPreferenceChangeListener { - - public CalculatorAdditionalTitle(Context context) { - super(context); - } - - public CalculatorAdditionalTitle(Context context, AttributeSet attrs) { - super(context, attrs); - } - - public CalculatorAdditionalTitle(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - } - - public void init(@Nonnull SharedPreferences preferences) { - onSharedPreferenceChanged(preferences, null); - } - - @Override - public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String key) { - setText(((Engine) Locator.getInstance().getEngine()).getNumeralBaseFromPrefs(preferences) - + " / " + - ((Engine) Locator.getInstance().getEngine()).getAngleUnitsFromPrefs(preferences)); - } -} diff --git a/app/src/main/java/org/solovyev/android/calculator/view/NumeralBaseConverterDialog.java b/app/src/main/java/org/solovyev/android/calculator/view/NumeralBaseConverterDialog.java index cb1b2126..59f10d5d 100644 --- a/app/src/main/java/org/solovyev/android/calculator/view/NumeralBaseConverterDialog.java +++ b/app/src/main/java/org/solovyev/android/calculator/view/NumeralBaseConverterDialog.java @@ -65,12 +65,12 @@ public class NumeralBaseConverterDialog { String value = initialFromValue; try { value = ToJsclTextProcessor.getInstance().process(value).getExpression(); - b.setFromValue(UnitImpl.newInstance(value, CalculatorNumeralBase.valueOf(Locator.getInstance().getEngine().getNumeralBase()))); + b.setFromValue(UnitImpl.newInstance(value, CalculatorNumeralBase.valueOf(Locator.getInstance().getEngine().getMathEngine().getNumeralBase()))); } catch (CalculatorParseException e) { - b.setFromValue(UnitImpl.newInstance(value, CalculatorNumeralBase.valueOf(Locator.getInstance().getEngine().getNumeralBase()))); + b.setFromValue(UnitImpl.newInstance(value, CalculatorNumeralBase.valueOf(Locator.getInstance().getEngine().getMathEngine().getNumeralBase()))); } } else { - b.setFromValue(UnitImpl.newInstance("", CalculatorNumeralBase.valueOf(Locator.getInstance().getEngine().getNumeralBase()))); + b.setFromValue(UnitImpl.newInstance("", CalculatorNumeralBase.valueOf(Locator.getInstance().getEngine().getMathEngine().getNumeralBase()))); } b.setConverter(CalculatorNumeralBase.getConverter()); @@ -91,7 +91,7 @@ public class NumeralBaseConverterDialog { public void onClick(@Nonnull Unit fromUnits, @Nonnull Unit toUnits) { String toUnitsValue = toUnits.getValue(); - if (!toUnits.getUnitType().equals(CalculatorNumeralBase.valueOf(Locator.getInstance().getEngine().getNumeralBase()))) { + if (!toUnits.getUnitType().equals(CalculatorNumeralBase.valueOf(Locator.getInstance().getEngine().getMathEngine().getNumeralBase()))) { toUnitsValue = ((CalculatorNumeralBase) toUnits.getUnitType()).getNumeralBase().getJsclPrefix() + toUnitsValue; } diff --git a/app/src/main/java/org/solovyev/android/calculator/view/NumeralBasesButton.java b/app/src/main/java/org/solovyev/android/calculator/view/NumeralBasesButton.java index e7d21544..15a42972 100644 --- a/app/src/main/java/org/solovyev/android/calculator/view/NumeralBasesButton.java +++ b/app/src/main/java/org/solovyev/android/calculator/view/NumeralBasesButton.java @@ -47,7 +47,7 @@ public class NumeralBasesButton extends DirectionDragButton { public NumeralBasesButton(Context context, @Nonnull AttributeSet attrs) { super(context, attrs); - this.numeralBase = Locator.getInstance().getEngine().getNumeralBase(); + this.numeralBase = Locator.getInstance().getEngine().getMathEngine().getNumeralBase(); } @Override diff --git a/app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorMode.java b/app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorMode.java index 8eacf390..6f1db027 100644 --- a/app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorMode.java +++ b/app/src/main/java/org/solovyev/android/calculator/wizard/CalculatorMode.java @@ -23,13 +23,15 @@ package org.solovyev.android.calculator.wizard; import android.content.SharedPreferences; -import jscl.AngleUnit; + import org.solovyev.android.calculator.Engine; import org.solovyev.android.calculator.Preferences; import org.solovyev.android.calculator.R; import javax.annotation.Nonnull; +import jscl.AngleUnit; + import static org.solovyev.android.calculator.Preferences.Gui.Layout.main_calculator; import static org.solovyev.android.calculator.Preferences.Gui.Layout.main_calculator_mobile; @@ -51,8 +53,8 @@ enum CalculatorMode { } Preferences.Calculations.preferredAngleUnits.putPreference(preferences, AngleUnit.deg); Engine.Preferences.angleUnit.putPreference(preferences, AngleUnit.deg); - Engine.Preferences.scienceNotation.putPreference(preferences, false); - Engine.Preferences.roundResult.putPreference(preferences, true); + Engine.Preferences.Output.scientificNotation.putPreference(preferences, false); + Engine.Preferences.Output.round.putPreference(preferences, true); } }, @@ -67,8 +69,8 @@ enum CalculatorMode { } Preferences.Calculations.preferredAngleUnits.putPreference(preferences, AngleUnit.rad); Engine.Preferences.angleUnit.putPreference(preferences, AngleUnit.rad); - Engine.Preferences.scienceNotation.putPreference(preferences, true); - Engine.Preferences.roundResult.putPreference(preferences, false); + Engine.Preferences.Output.scientificNotation.putPreference(preferences, true); + Engine.Preferences.Output.round.putPreference(preferences, false); } }; diff --git a/app/src/main/res/values/preferences.xml b/app/src/main/res/values/preferences.xml index 24b13ff7..867d4dcf 100644 --- a/app/src/main/res/values/preferences.xml +++ b/app/src/main/res/values/preferences.xml @@ -21,9 +21,6 @@ --> - org.solovyev.android.calculator.CalculatorModel_result_precision - 5 - org.solovyev.android.calculator.CalculatorModel_color_display true @@ -39,36 +36,14 @@ true - org.solovyev.android.calculator.CalculatorModel_round_result - true - - calculation.output.science_notation - false - org.solovyev.android.calculator.CalculatorModel_history - org.solovyev.android.calculator.CalculatorActivity_angle_units - deg - - org.solovyev.android.calculator.CalculatorActivity_numeral_bases - dec - org.solovyev.android.calculator.CalculatorActivity_calc_theme default_theme org.solovyev.android.calculator.CalculatorActivity_calc_layout main_calculator - - org.solovyev.android.calculator.CalculatorActivity_calc_grouping_separator - - " " - - - org.solovyev.android.calculator.CalculatorActivity_calc_multiplication_sign - - "×" - false \ No newline at end of file diff --git a/app/src/main/res/xml/preferences_appearance.xml b/app/src/main/res/xml/preferences_appearance.xml index fe5f55e9..8fbf0dda 100644 --- a/app/src/main/res/xml/preferences_appearance.xml +++ b/app/src/main/res/xml/preferences_appearance.xml @@ -39,7 +39,7 @@ diff --git a/app/src/main/res/xml/preferences_calculations.xml b/app/src/main/res/xml/preferences_calculations.xml index 9751a104..47acc38f 100644 --- a/app/src/main/res/xml/preferences_calculations.xml +++ b/app/src/main/res/xml/preferences_calculations.xml @@ -27,27 +27,27 @@ @@ -61,7 +61,7 @@ @@ -75,7 +75,7 @@ diff --git a/app/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java b/app/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java index 7d7bfe81..29462243 100644 --- a/app/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java @@ -37,7 +37,12 @@ public class AbstractCalculatorTest { protected void setUp() throws Exception { Locator.getInstance().init(new CalculatorImpl(Mockito.mock(Bus.class), Mockito.mock(Executor.class)), CalculatorTestUtils.newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), new SystemErrorReporter(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(Keyboard.class), Mockito.mock(CalculatorPlotter.class)); - Locator.getInstance().getEngine().init(); + Locator.getInstance().getEngine().init(new Executor() { + @Override + public void execute(Runnable command) { + command.run(); + } + }); } } diff --git a/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java b/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java index 64c42b4b..169c7485 100644 --- a/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java +++ b/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java @@ -23,19 +23,22 @@ package org.solovyev.android.calculator; import android.content.Context; + import com.squareup.otto.Bus; -import jscl.JsclMathEngine; + import org.junit.Assert; import org.mockito.Mockito; -import org.robolectric.RuntimeEnvironment; import org.robolectric.fakes.RoboSharedPreferences; import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.language.Languages; import org.solovyev.android.calculator.plot.CalculatorPlotter; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.io.*; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; import java.text.DecimalFormatSymbols; import java.util.HashMap; import java.util.Map; @@ -43,6 +46,11 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import jscl.JsclMathEngine; + /** * User: serso * Date: 10/7/12 @@ -56,17 +64,27 @@ public class CalculatorTestUtils { public static void staticSetUp() throws Exception { App.init(new CalculatorApplication(), new Languages(new RoboSharedPreferences(new HashMap>(), "test", 0))); Locator.getInstance().init(new CalculatorImpl(Mockito.mock(Bus.class), Mockito.mock(Executor.class)), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), new SystemErrorReporter(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(Keyboard.class), Mockito.mock(CalculatorPlotter.class)); - Locator.getInstance().getEngine().init(); + Locator.getInstance().getEngine().init(new Executor() { + @Override + public void execute(Runnable command) { + command.run(); + } + }); final DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(); decimalGroupSymbols.setDecimalSeparator('.'); decimalGroupSymbols.setGroupingSeparator(' '); - Locator.getInstance().getEngine().setDecimalGroupSymbols(decimalGroupSymbols); + Locator.getInstance().getEngine().getMathEngine().setDecimalGroupSymbols(decimalGroupSymbols); } public static void staticSetUp(@Nullable Context context) throws Exception { Locator.getInstance().init(new CalculatorImpl(Mockito.mock(Bus.class), Mockito.mock(Executor.class)), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), new SystemErrorReporter(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(Keyboard.class), Mockito.mock(CalculatorPlotter.class)); - Locator.getInstance().getEngine().init(); + Locator.getInstance().getEngine().init(new Executor() { + @Override + public void execute(Runnable command) { + command.run(); + } + }); if (context != null) { initViews(context); @@ -89,7 +107,7 @@ public class CalculatorTestUtils { final OperatorsRegistry operatorsRegistry = new OperatorsRegistry(jsclEngine.getOperatorsRegistry(), mathEntityDao); final PostfixFunctionsRegistry postfixFunctionsRegistry = new PostfixFunctionsRegistry(jsclEngine.getPostfixFunctionsRegistry(), mathEntityDao); - return new Engine(RuntimeEnvironment.application, jsclEngine, varsRegistry, functionsRegistry, operatorsRegistry, postfixFunctionsRegistry); + return new Engine(jsclEngine, varsRegistry, functionsRegistry, operatorsRegistry, postfixFunctionsRegistry); } public static void assertEval(@Nonnull String expected, @Nonnull String expression) { diff --git a/app/src/test/java/org/solovyev/android/calculator/FromJsclSimplifyTextProcessorTest.java b/app/src/test/java/org/solovyev/android/calculator/FromJsclSimplifyTextProcessorTest.java index 36d269c3..3406dcfc 100644 --- a/app/src/test/java/org/solovyev/android/calculator/FromJsclSimplifyTextProcessorTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/FromJsclSimplifyTextProcessorTest.java @@ -50,7 +50,7 @@ public class FromJsclSimplifyTextProcessorTest extends AbstractCalculatorTest { //Assert.assertEquals("((e)(e))", tp.process("((2.718281828459045)*(2.718281828459045))")); DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(); decimalGroupSymbols.setGroupingSeparator(' '); - Locator.getInstance().getEngine().setDecimalGroupSymbols(decimalGroupSymbols); + Locator.getInstance().getEngine().getMathEngine().setDecimalGroupSymbols(decimalGroupSymbols); //Assert.assertEquals("123 456 789e", tp.process("123456789*2.718281828459045")); //Assert.assertEquals("123 456 789e", tp.process("123 456 789 * 2.718281828459045")); //Assert.assertEquals("t11e", tp.process("t11*2.718281828459045")); diff --git a/app/src/test/java/org/solovyev/android/calculator/model/AndroidEngineTest.java b/app/src/test/java/org/solovyev/android/calculator/model/AndroidEngineTest.java index 6ab1c8b2..f08fb6f8 100644 --- a/app/src/test/java/org/solovyev/android/calculator/model/AndroidEngineTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/model/AndroidEngineTest.java @@ -58,7 +58,7 @@ public class AndroidEngineTest extends AbstractCalculatorTest { @BeforeClass public static void staticSetUp() throws Exception { CalculatorTestUtils.staticSetUp(); - Locator.getInstance().getEngine().setPrecision(3); + Locator.getInstance().getEngine().getMathEngine().setPrecision(3); } diff --git a/app/src/test/java/org/solovyev/android/calculator/model/ComparisonTest.java b/app/src/test/java/org/solovyev/android/calculator/model/ComparisonTest.java index 84a1bc5c..07b73536 100644 --- a/app/src/test/java/org/solovyev/android/calculator/model/ComparisonTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/model/ComparisonTest.java @@ -41,7 +41,7 @@ public class ComparisonTest extends AbstractCalculatorTest { @BeforeClass public static void staticSetUp() throws Exception { CalculatorTestUtils.staticSetUp(); - Locator.getInstance().getEngine().setPrecision(3); + Locator.getInstance().getEngine().getMathEngine().setPrecision(3); } @Test diff --git a/app/src/test/java/org/solovyev/android/calculator/model/EvaluateTest.java b/app/src/test/java/org/solovyev/android/calculator/model/EvaluateTest.java index f958573b..bdc550d0 100644 --- a/app/src/test/java/org/solovyev/android/calculator/model/EvaluateTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/model/EvaluateTest.java @@ -49,7 +49,7 @@ public class EvaluateTest extends AbstractCalculatorTest { @BeforeClass public static void staticSetUp() throws Exception { CalculatorTestUtils.staticSetUp(); - Locator.getInstance().getEngine().setPrecision(3); + Locator.getInstance().getEngine().getMathEngine().setPrecision(3); } @Test diff --git a/app/src/test/java/org/solovyev/android/calculator/model/NumeralBaseTest.java b/app/src/test/java/org/solovyev/android/calculator/model/NumeralBaseTest.java index 1e0f84fc..c5ff3af5 100644 --- a/app/src/test/java/org/solovyev/android/calculator/model/NumeralBaseTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/model/NumeralBaseTest.java @@ -22,22 +22,28 @@ package org.solovyev.android.calculator.model; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.solovyev.android.calculator.AbstractCalculatorTest; +import org.solovyev.android.calculator.CalculatorEvalException; +import org.solovyev.android.calculator.CalculatorParseException; +import org.solovyev.android.calculator.CalculatorTestUtils; +import org.solovyev.android.calculator.Locator; +import org.solovyev.common.Converter; + +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; + +import javax.annotation.Nonnull; + import au.com.bytecode.opencsv.CSVReader; import jscl.JsclMathEngine; import jscl.MathEngine; import jscl.math.Expression; import jscl.text.ParseException; import jscl.util.ExpressionGeneratorWithInput; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; -import org.solovyev.android.calculator.*; -import org.solovyev.common.Converter; - -import javax.annotation.Nonnull; -import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.List; /** * User: serso @@ -49,7 +55,7 @@ public class NumeralBaseTest extends AbstractCalculatorTest { @BeforeClass public static void staticSetUp() throws Exception { CalculatorTestUtils.staticSetUp(); - Locator.getInstance().getEngine().setPrecision(3); + Locator.getInstance().getEngine().getMathEngine().setPrecision(3); } public static void testExpression(@Nonnull String[] line, @Nonnull Converter converter) throws ParseException, CalculatorEvalException, CalculatorParseException { diff --git a/app/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java b/app/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java index d4cf008d..fdc3b487 100644 --- a/app/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java @@ -87,10 +87,10 @@ public class ToJsclTextProcessorTest extends AbstractCalculatorTest { Assert.assertEquals("EE", preprocessor.process("EE").toString()); try { - Locator.getInstance().getEngine().setNumeralBase(NumeralBase.hex); + Locator.getInstance().getEngine().getMathEngine().setNumeralBase(NumeralBase.hex); Assert.assertEquals("22F*exp(F)", preprocessor.process("22Fexp(F)").toString()); } finally { - Locator.getInstance().getEngine().setNumeralBase(NumeralBase.dec); + Locator.getInstance().getEngine().getMathEngine().setNumeralBase(NumeralBase.dec); } Assert.assertEquals("0x:ABCDEF", preprocessor.process("0x:ABCDEF").toString()); Assert.assertEquals("0x:ABCDEF", preprocessor.process("0x:A BC DEF").toString());