From 1d366123b3a35518b4b7c809fb1620e01c4b6372 Mon Sep 17 00:00:00 2001 From: serso Date: Tue, 19 Jan 2016 23:22:05 +0100 Subject: [PATCH] CalculatorAndroidEngine and CalculatorEngineImple merged into one Engine --- .../AndroidCalculatorPreferenceService.java | 18 +- .../android/calculator/BaseNumberBuilder.java | 4 +- .../solovyev/android/calculator/BaseUi.java | 4 +- .../calculator/CalculatorApplication.java | 5 +- .../android/calculator/CalculatorButtons.java | 11 +- .../android/calculator/CalculatorEngine.java | 67 ----- .../calculator/CalculatorEngineImpl.java | 262 ------------------ .../android/calculator/CalculatorImpl.java | 12 +- .../CalculatorKeyboardFragment.java | 4 +- .../android/calculator/CalculatorLocator.java | 4 +- ...droidCalculatorEngine.java => Engine.java} | 246 ++++++++-------- .../android/calculator/FunctionsRegistry.java | 9 +- .../android/calculator/LiteNumberBuilder.java | 2 +- .../solovyev/android/calculator/Locator.java | 10 +- .../android/calculator/NumberBuilder.java | 6 +- .../calculator/NumeralBaseButtons.java | 5 +- ...thRegistry.java => OperatorsRegistry.java} | 17 +- ...try.java => PostfixFunctionsRegistry.java} | 17 +- .../android/calculator/Preferences.java | 39 +-- ...torVarsRegistry.java => VarsRegistry.java} | 25 +- .../android/calculator/history/History.java | 2 +- .../calculator/math/edit/VarEditorSaver.java | 6 +- .../plot/AbstractCalculatorPlotFragment.java | 2 +- .../preferences/PreferencesFragment.java | 4 +- .../view/CalculatorAdditionalTitle.java | 6 +- .../calculator/view/TextHighlighter.java | 13 +- .../calculator/wizard/CalculatorMode.java | 18 +- .../calculator/CalculatorTestUtils.java | 28 +- .../calculator/TextHighlighterTest.java | 12 +- .../calculator/history/HistoryTest.java | 22 +- ...EngineTest.java => AndroidEngineTest.java} | 16 +- .../calculator/model/EvaluateTest.java | 2 +- .../calculator/model/NumeralBaseTest.java | 6 +- 33 files changed, 257 insertions(+), 647 deletions(-) delete mode 100644 app/src/main/java/org/solovyev/android/calculator/CalculatorEngine.java delete mode 100644 app/src/main/java/org/solovyev/android/calculator/CalculatorEngineImpl.java rename app/src/main/java/org/solovyev/android/calculator/{model/AndroidCalculatorEngine.java => Engine.java} (59%) rename app/src/main/java/org/solovyev/android/calculator/{CalculatorOperatorsMathRegistry.java => OperatorsRegistry.java} (88%) rename app/src/main/java/org/solovyev/android/calculator/{CalculatorPostfixFunctionsRegistry.java => PostfixFunctionsRegistry.java} (87%) rename app/src/main/java/org/solovyev/android/calculator/{CalculatorVarsRegistry.java => VarsRegistry.java} (89%) rename app/src/test/java/org/solovyev/android/calculator/model/{AndroidCalculatorEngineTest.java => AndroidEngineTest.java} (97%) diff --git a/app/src/main/java/org/solovyev/android/calculator/AndroidCalculatorPreferenceService.java b/app/src/main/java/org/solovyev/android/calculator/AndroidCalculatorPreferenceService.java index 62784211..7497cdf1 100644 --- a/app/src/main/java/org/solovyev/android/calculator/AndroidCalculatorPreferenceService.java +++ b/app/src/main/java/org/solovyev/android/calculator/AndroidCalculatorPreferenceService.java @@ -25,19 +25,15 @@ package org.solovyev.android.calculator; import android.app.Application; import android.content.SharedPreferences; import android.preference.PreferenceManager; - -import org.solovyev.android.calculator.model.AndroidCalculatorEngine; +import jscl.AngleUnit; +import jscl.NumeralBase; import org.solovyev.android.msg.AndroidMessage; import org.solovyev.common.msg.MessageType; +import javax.annotation.Nonnull; import java.util.ArrayList; import java.util.List; -import javax.annotation.Nonnull; - -import jscl.AngleUnit; -import jscl.NumeralBase; - /** * User: serso * Date: 11/17/12 @@ -63,10 +59,10 @@ public class AndroidCalculatorPreferenceService implements CalculatorPreferenceS if (force || (Preferences.Calculations.showCalculationMessagesDialog.getPreference(prefs) && isTimeForCheck(currentTime, prefs))) { final NumeralBase preferredNumeralBase = Preferences.Calculations.preferredNumeralBase.getPreference(prefs); - final NumeralBase numeralBase = AndroidCalculatorEngine.Preferences.numeralBase.getPreference(prefs); + final NumeralBase numeralBase = Engine.Preferences.numeralBase.getPreference(prefs); final AngleUnit preferredAngleUnits = Preferences.Calculations.preferredAngleUnits.getPreference(prefs); - final AngleUnit angleUnits = AndroidCalculatorEngine.Preferences.angleUnit.getPreference(prefs); + final AngleUnit angleUnits = Engine.Preferences.angleUnit.getPreference(prefs); final List messages = new ArrayList(2); if (numeralBase != preferredNumeralBase) { @@ -98,7 +94,7 @@ public class AndroidCalculatorPreferenceService implements CalculatorPreferenceS @Override public void setAngleUnits(@Nonnull AngleUnit angleUnit) { final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(application); - AndroidCalculatorEngine.Preferences.angleUnit.putPreference(preferences, angleUnit); + Engine.Preferences.angleUnit.putPreference(preferences, angleUnit); Locator.getInstance().getNotifier().showMessage(new AndroidMessage(R.string.c_angle_units_changed_to, MessageType.info, application, angleUnit.name())); } @@ -112,7 +108,7 @@ public class AndroidCalculatorPreferenceService implements CalculatorPreferenceS @Override public void setNumeralBase(@Nonnull NumeralBase numeralBase) { final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(application); - AndroidCalculatorEngine.Preferences.numeralBase.putPreference(preferences, numeralBase); + Engine.Preferences.numeralBase.putPreference(preferences, numeralBase); Locator.getInstance().getNotifier().showMessage(new AndroidMessage(R.string.c_numeral_base_changed_to, MessageType.info, application, numeralBase.name())); } 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 1337587f..54d12f97 100644 --- a/app/src/main/java/org/solovyev/android/calculator/BaseNumberBuilder.java +++ b/app/src/main/java/org/solovyev/android/calculator/BaseNumberBuilder.java @@ -40,7 +40,7 @@ import jscl.NumeralBase; public abstract class BaseNumberBuilder { @Nonnull - protected final CalculatorEngine engine; + protected final Engine engine; @Nullable protected StringBuilder numberBuilder = null; @@ -48,7 +48,7 @@ public abstract class BaseNumberBuilder { @Nullable protected NumeralBase nb; - protected BaseNumberBuilder(@Nonnull CalculatorEngine engine) { + protected BaseNumberBuilder(@Nonnull Engine engine) { this.engine = engine; this.nb = engine.getNumeralBase(); } diff --git a/app/src/main/java/org/solovyev/android/calculator/BaseUi.java b/app/src/main/java/org/solovyev/android/calculator/BaseUi.java index 971625c7..71c1e324 100644 --- a/app/src/main/java/org/solovyev/android/calculator/BaseUi.java +++ b/app/src/main/java/org/solovyev/android/calculator/BaseUi.java @@ -56,8 +56,8 @@ import javax.inject.Inject; import static org.solovyev.android.calculator.Preferences.Gui.Layout.simple; import static org.solovyev.android.calculator.Preferences.Gui.Layout.simple_mobile; -import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.angleUnit; -import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.numeralBase; +import static org.solovyev.android.calculator.Engine.Preferences.angleUnit; +import static org.solovyev.android.calculator.Engine.Preferences.numeralBase; public abstract class BaseUi implements SharedPreferences.OnSharedPreferenceChangeListener { 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 eb219795..a10fab8b 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java @@ -36,7 +36,6 @@ import org.solovyev.android.Android; import org.solovyev.android.calculator.history.History; import org.solovyev.android.calculator.language.Language; import org.solovyev.android.calculator.language.Languages; -import org.solovyev.android.calculator.model.AndroidCalculatorEngine; import org.solovyev.android.calculator.onscreen.CalculatorOnscreenStartActivity; import org.solovyev.android.calculator.plot.AndroidCalculatorPlotter; import org.solovyev.android.calculator.plot.CalculatorPlotterImpl; @@ -117,7 +116,7 @@ public class CalculatorApplication extends android.app.Application implements Sh App.getGa().reportInitially(preferences); Locator.getInstance().init(calculator, - new AndroidCalculatorEngine(this), + new Engine(this), new AndroidCalculatorClipboard(this), new AndroidCalculatorNotifier(this), errorReporter, @@ -144,7 +143,7 @@ public class CalculatorApplication extends android.app.Application implements Sh private void warmUpEngine() { try { // warm-up engine - MathEngine mathEngine = Locator.getInstance().getEngine().getEngine(); + MathEngine mathEngine = Locator.getInstance().getEngine().getMathEngine(); mathEngine.evaluate("1+1"); mathEngine.evaluate("1*1"); } catch (Throwable e) { diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorButtons.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorButtons.java index c2deb78f..70732357 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorButtons.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorButtons.java @@ -33,9 +33,9 @@ import android.util.TypedValue; import android.view.MotionEvent; import android.view.View; import android.widget.Button; - +import jscl.AngleUnit; +import jscl.NumeralBase; import org.solovyev.android.Views; -import org.solovyev.android.calculator.model.AndroidCalculatorEngine; import org.solovyev.android.calculator.view.AngleUnitsButton; import org.solovyev.android.calculator.view.NumeralBasesButton; import org.solovyev.android.calculator.view.ScreenMetrics; @@ -46,9 +46,6 @@ import org.solovyev.android.views.dragbutton.SimpleDragListener; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import jscl.AngleUnit; -import jscl.NumeralBase; - public final class CalculatorButtons { private CalculatorButtons() { @@ -178,7 +175,7 @@ public final class CalculatorButtons { final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - final AngleUnit oldAngleUnits = AndroidCalculatorEngine.Preferences.angleUnit.getPreference(preferences); + final AngleUnit oldAngleUnits = Engine.Preferences.angleUnit.getPreference(preferences); if (oldAngleUnits != angleUnits) { Locator.getInstance().getPreferenceService().setAngleUnits(angleUnits); } @@ -218,7 +215,7 @@ public final class CalculatorButtons { final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - final NumeralBase oldNumeralBase = AndroidCalculatorEngine.Preferences.numeralBase.getPreference(preferences); + final NumeralBase oldNumeralBase = Engine.Preferences.numeralBase.getPreference(preferences); if (oldNumeralBase != numeralBase) { Locator.getInstance().getPreferenceService().setNumeralBase(numeralBase); } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorEngine.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorEngine.java deleted file mode 100644 index 89b67838..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorEngine.java +++ /dev/null @@ -1,67 +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; - -import jscl.AngleUnit; -import jscl.MathEngine; -import jscl.NumeralBase; -import jscl.math.function.Function; -import jscl.math.function.IConstant; -import jscl.math.operator.Operator; - -import javax.annotation.Nonnull; -import java.text.DecimalFormatSymbols; - -public interface CalculatorEngine { - - void init(); - void reset(); - void softReset(); - - @Nonnull - EntitiesRegistry getVarsRegistry(); - @Nonnull - EntitiesRegistry getFunctionsRegistry(); - @Nonnull - EntitiesRegistry getOperatorsRegistry(); - @Nonnull - EntitiesRegistry getPostfixFunctionsRegistry(); - @Nonnull - MathEngine getEngine(); - - @Nonnull - String getMultiplicationSign(); - void setMultiplicationSign(@Nonnull String multiplicationSign); - void setUseGroupingSeparator(boolean useGroupingSeparator); - void setGroupingSeparator(char groupingSeparator); - void setPrecision(@Nonnull Integer precision); - void setRoundResult(@Nonnull Boolean round); - @Nonnull - AngleUnit getAngleUnits(); - void setAngleUnits(@Nonnull AngleUnit angleUnits); - @Nonnull - NumeralBase getNumeralBase(); - void setNumeralBase(@Nonnull NumeralBase numeralBase); - void setScienceNotation(@Nonnull Boolean scienceNotation); - void setDecimalGroupSymbols(@Nonnull DecimalFormatSymbols decimalGroupSymbols); -} diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorEngineImpl.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorEngineImpl.java deleted file mode 100644 index 6f2d6141..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorEngineImpl.java +++ /dev/null @@ -1,262 +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; - -import jscl.AngleUnit; -import jscl.JsclMathEngine; -import jscl.MathEngine; -import jscl.NumeralBase; -import jscl.math.function.Function; -import jscl.math.function.IConstant; -import jscl.math.operator.Operator; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.text.DecimalFormatSymbols; - -public class CalculatorEngineImpl implements CalculatorEngine { - - /* - ********************************************************************** - * - * CONSTANTS - * - ********************************************************************** - */ - - private static final String MULTIPLICATION_SIGN_DEFAULT = "×"; - - /* - ********************************************************************** - * - * ENGINE/REGISTRIES - * - ********************************************************************** - */ - @Nonnull - private final MathEngine engine; - - @Nonnull - private final EntitiesRegistry varsRegistry; - - @Nonnull - private final EntitiesRegistry functionsRegistry; - - @Nonnull - private final EntitiesRegistry operatorsRegistry; - - @Nonnull - private final EntitiesRegistry postfixFunctionsRegistry; - - @Nonnull - private final Object lock; - - /* - ********************************************************************** - * - * PREFERENCES - * - ********************************************************************** - */ - - - @Nonnull - private String multiplicationSign = MULTIPLICATION_SIGN_DEFAULT; - - public CalculatorEngineImpl(@Nonnull JsclMathEngine engine, - @Nonnull EntitiesRegistry varsRegistry, - @Nonnull EntitiesRegistry functionsRegistry, - @Nonnull EntitiesRegistry operatorsRegistry, - @Nonnull EntitiesRegistry postfixFunctionsRegistry, - @Nullable Object lock) { - - this.engine = engine; - - this.engine.setRoundResult(true); - this.engine.setUseGroupingSeparator(true); - - this.varsRegistry = varsRegistry; - this.functionsRegistry = functionsRegistry; - this.operatorsRegistry = operatorsRegistry; - this.postfixFunctionsRegistry = postfixFunctionsRegistry; - this.lock = lock == null ? new Object() : lock; - } - - /* - ********************************************************************** - * - * REGISTRIES - * - ********************************************************************** - */ - @Nonnull - @Override - public EntitiesRegistry getVarsRegistry() { - return this.varsRegistry; - } - - @Nonnull - @Override - public EntitiesRegistry getFunctionsRegistry() { - return this.functionsRegistry; - } - - @Nonnull - @Override - public EntitiesRegistry getOperatorsRegistry() { - return this.operatorsRegistry; - } - - @Nonnull - @Override - public EntitiesRegistry getPostfixFunctionsRegistry() { - return this.postfixFunctionsRegistry; - } - - @Nonnull - @Override - public MathEngine getEngine() { - return engine; - } - - /* - ********************************************************************** - * - * INIT - * - ********************************************************************** - */ - - @Override - public void init() { - synchronized (lock) { - reset(); - } - } - - @Override - public void reset() { - synchronized (lock) { - 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); - } - } - - @Override - public void softReset() { - Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.engine_preferences_changed, null); - } - - @Nonnull - @Override - public String getMultiplicationSign() { - return this.multiplicationSign; - } - - @Override - public void setMultiplicationSign(@Nonnull String multiplicationSign) { - this.multiplicationSign = multiplicationSign; - } - - @Override - public void setUseGroupingSeparator(boolean useGroupingSeparator) { - synchronized (lock) { - this.engine.setUseGroupingSeparator(true); - } - } - - @Override - public void setGroupingSeparator(char groupingSeparator) { - synchronized (lock) { - this.engine.setGroupingSeparator(groupingSeparator); - } - } - - @Override - public void setPrecision(@Nonnull Integer precision) { - synchronized (lock) { - this.engine.setPrecision(precision); - } - } - - @Override - public void setRoundResult(@Nonnull Boolean round) { - synchronized (lock) { - this.engine.setRoundResult(round); - } - } - - @Nonnull - @Override - public AngleUnit getAngleUnits() { - synchronized (lock) { - return this.engine.getAngleUnits(); - } - } - - @Override - public void setAngleUnits(@Nonnull AngleUnit angleUnits) { - synchronized (lock) { - this.engine.setAngleUnits(angleUnits); - } - } - - @Nonnull - @Override - public NumeralBase getNumeralBase() { - synchronized (lock) { - return this.engine.getNumeralBase(); - } - } - - @Override - public void setNumeralBase(@Nonnull NumeralBase numeralBase) { - synchronized (lock) { - this.engine.setNumeralBase(numeralBase); - } - } - - @Override - public void setScienceNotation(@Nonnull Boolean scienceNotation) { - synchronized (lock) { - this.engine.setScienceNotation(scienceNotation); - } - } - - @Override - public void setDecimalGroupSymbols(@Nonnull DecimalFormatSymbols decimalGroupSymbols) { - synchronized (lock) { - this.engine.setDecimalGroupSymbols(decimalGroupSymbols); - } - } -} 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 15ba9747..14d57501 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java @@ -255,10 +255,10 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { try { - final MathEngine mathEngine = Locator.getInstance().getEngine().getEngine(); + final MathEngine mathEngine = Locator.getInstance().getEngine().getMathEngine(); final MessageRegistry messageRegistry = new ListMessageRegistry(); - Locator.getInstance().getEngine().getEngine().setMessageRegistry(messageRegistry); + Locator.getInstance().getEngine().getMathEngine().setMessageRegistry(messageRegistry); final Generic result = operation.evaluateGeneric(jsclExpression, mathEngine); @@ -496,14 +496,14 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { return; } final EntitiesRegistry varsRegistry = Locator.getInstance().getEngine().getVarsRegistry(); - final IConstant ansVar = varsRegistry.get(CalculatorVarsRegistry.ANS); + final IConstant ansVar = varsRegistry.get(VarsRegistry.ANS); final Var.Builder builder = ansVar != null ? new Var.Builder(ansVar) : new Var.Builder(); - builder.setName(CalculatorVarsRegistry.ANS); + builder.setName(VarsRegistry.ANS); builder.setValue(text); builder.setDescription(CalculatorMessages.getBundle().getString(CalculatorMessages.ans_description)); - CalculatorVarsRegistry.saveVariable(varsRegistry, builder, ansVar, this, false); + VarsRegistry.saveVariable(varsRegistry, builder, ansVar, this, false); } @Override @@ -512,7 +512,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { switch (calculatorEventType) { case constant_changed: final IConstant newConstant = ((Change) data).getNewValue(); - if (!newConstant.getName().equals(CalculatorVarsRegistry.ANS)) { + if (!newConstant.getName().equals(VarsRegistry.ANS)) { evaluate(); } break; diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorKeyboardFragment.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorKeyboardFragment.java index 50e4dfb2..3fd5bf63 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorKeyboardFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorKeyboardFragment.java @@ -34,8 +34,8 @@ import javax.annotation.Nonnull; import static org.solovyev.android.calculator.NumeralBaseButtons.toggleNumericDigits; import static org.solovyev.android.calculator.Preferences.Gui.hideNumeralBaseDigits; import static org.solovyev.android.calculator.Preferences.Gui.showEqualsButton; -import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.multiplicationSign; -import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.numeralBase; +import static org.solovyev.android.calculator.Engine.Preferences.multiplicationSign; +import static org.solovyev.android.calculator.Engine.Preferences.numeralBase; /** * User: Solovyev_S diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java index e3030010..7855599a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java @@ -29,7 +29,7 @@ import javax.annotation.Nonnull; public interface CalculatorLocator { void init(@Nonnull Calculator calculator, - @Nonnull CalculatorEngine engine, + @Nonnull Engine engine, @Nonnull CalculatorClipboard clipboard, @Nonnull CalculatorNotifier notifier, @Nonnull ErrorReporter errorReporter, @@ -41,7 +41,7 @@ public interface CalculatorLocator { Calculator getCalculator(); @Nonnull - CalculatorEngine getEngine(); + Engine getEngine(); @Nonnull Keyboard getKeyboard(); diff --git a/app/src/main/java/org/solovyev/android/calculator/model/AndroidCalculatorEngine.java b/app/src/main/java/org/solovyev/android/calculator/Engine.java similarity index 59% rename from app/src/main/java/org/solovyev/android/calculator/model/AndroidCalculatorEngine.java rename to app/src/main/java/org/solovyev/android/calculator/Engine.java index 66c1ba42..ad30248d 100644 --- a/app/src/main/java/org/solovyev/android/calculator/model/AndroidCalculatorEngine.java +++ b/app/src/main/java/org/solovyev/android/calculator/Engine.java @@ -20,7 +20,7 @@ * Site: http://se.solovyev.org */ -package org.solovyev.android.calculator.model; +package org.solovyev.android.calculator; import android.app.Application; import android.content.Context; @@ -33,7 +33,9 @@ import jscl.NumeralBase; import jscl.math.function.Function; import jscl.math.function.IConstant; import jscl.math.operator.Operator; -import org.solovyev.android.calculator.*; +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; @@ -47,18 +49,13 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -/** - * User: serso - * Date: 9/12/11 - * Time: 11:38 PM - */ +public class Engine implements SharedPreferences.OnSharedPreferenceChangeListener { -public class AndroidCalculatorEngine implements CalculatorEngine, 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 MULTIPLICATION_SIGN_DEFAULT = "×"; private static final String SCIENCE_NOTATION_P_KEY = "calculation.output.science_notation"; private static final boolean SCIENCE_NOTATION_DEFAULT = false; @@ -77,24 +74,44 @@ public class AndroidCalculatorEngine implements CalculatorEngine, SharedPreferen @Nonnull private final Context context; @Nonnull - private final CalculatorEngine calculatorEngine; - @Nonnull private final Object lock; + @Nonnull + private final MathEngine mathEngine; + @Nonnull + private final EntitiesRegistry varsRegistry; + @Nonnull + private final EntitiesRegistry functionsRegistry; + @Nonnull + private final EntitiesRegistry operatorsRegistry; + @Nonnull + private final EntitiesRegistry postfixFunctionsRegistry; + @Nonnull + private String multiplicationSign = MULTIPLICATION_SIGN_DEFAULT; - public AndroidCalculatorEngine(@Nonnull Application application) { + 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(); + this.mathEngine = mathEngine; + this.varsRegistry = varsRegistry; + this.functionsRegistry = functionsRegistry; + this.operatorsRegistry = operatorsRegistry; + this.postfixFunctionsRegistry = postfixFunctionsRegistry; + } + + 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); - final JsclMathEngine engine = JsclMathEngine.getInstance(); - this.calculatorEngine = new CalculatorEngineImpl(engine, - new CalculatorVarsRegistry(engine.getConstantsRegistry(), new AndroidMathEntityDao("org.solovyev.android.calculator.CalculatorModel_vars", application, Vars.class)), - new FunctionsRegistry(engine.getFunctionsRegistry(), new AndroidMathEntityDao("org.solovyev.android.calculator.CalculatorModel_functions", application, Functions.class)), - new CalculatorOperatorsMathRegistry(engine.getOperatorsRegistry(), new AndroidMathEntityDao(null, application, null)), - new CalculatorPostfixFunctionsRegistry(engine.getPostfixFunctionsRegistry(), new AndroidMathEntityDao(null, application, null)), - this.lock); + this.varsRegistry = new VarsRegistry(mathEngine.getConstantsRegistry(), new AndroidMathEntityDao<>("org.solovyev.android.calculator.CalculatorModel_vars", application, Vars.class)); + this.functionsRegistry = new FunctionsRegistry(mathEngine.getFunctionsRegistry(), new AndroidMathEntityDao<>("org.solovyev.android.calculator.CalculatorModel_functions", application, Functions.class)); + this.operatorsRegistry = new OperatorsRegistry(mathEngine.getOperatorsRegistry(), new AndroidMathEntityDao<>(null, application, null)); + this.postfixFunctionsRegistry = new PostfixFunctionsRegistry(mathEngine.getPostfixFunctionsRegistry(), new AndroidMathEntityDao<>(null, application, null)); } @Nonnull @@ -107,147 +124,147 @@ public class AndroidCalculatorEngine implements CalculatorEngine, SharedPreferen return Preferences.angleUnit.getPreference(preferences); } - @Override @Nonnull public EntitiesRegistry getVarsRegistry() { - return calculatorEngine.getVarsRegistry(); + return this.varsRegistry; } - @Override @Nonnull public EntitiesRegistry getFunctionsRegistry() { - return calculatorEngine.getFunctionsRegistry(); + return this.functionsRegistry; } - @Override @Nonnull public EntitiesRegistry getOperatorsRegistry() { - return calculatorEngine.getOperatorsRegistry(); + return this.operatorsRegistry; } - @Override @Nonnull public EntitiesRegistry getPostfixFunctionsRegistry() { - return calculatorEngine.getPostfixFunctionsRegistry(); - } - - @Override - @Nonnull - public MathEngine getEngine() { - return calculatorEngine.getEngine(); + return this.postfixFunctionsRegistry; } @Nonnull - @Override - public NumeralBase getNumeralBase() { - return calculatorEngine.getNumeralBase(); + public MathEngine getMathEngine() { + return mathEngine; } - @Override - public void setNumeralBase(@Nonnull NumeralBase numeralBase) { - calculatorEngine.setNumeralBase(numeralBase); - } - - @Override public void init() { - synchronized (lock) { - reset(); - } - } - - @Override - public void reset() { synchronized (lock) { final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - - softReset(preferences); - - calculatorEngine.reset(); + resetPreferences(preferences); + safeLoadRegistry(varsRegistry); + safeLoadRegistry(functionsRegistry); + safeLoadRegistry(operatorsRegistry); + safeLoadRegistry(postfixFunctionsRegistry); } } - @Override - public void softReset() { - synchronized (lock) { - softReset(PreferenceManager.getDefaultSharedPreferences(context)); + public void resetPreferences() { + resetPreferences(App.getPreferences()); + } - calculatorEngine.softReset(); + private void safeLoadRegistry(@Nonnull EntitiesRegistry registry) { + try { + registry.load(); + } catch (Exception e) { + Locator.getInstance().getErrorReporter().onException(e); } } - @Override - public void setUseGroupingSeparator(boolean useGroupingSeparator) { - calculatorEngine.setUseGroupingSeparator(useGroupingSeparator); - } - - @Override - public void setGroupingSeparator(char groupingSeparator) { - calculatorEngine.setGroupingSeparator(groupingSeparator); - } - - @Override - public void setPrecision(@Nonnull Integer precision) { - calculatorEngine.setPrecision(precision); - } - - @Override - public void setRoundResult(@Nonnull Boolean round) { - calculatorEngine.setRoundResult(round); - } - - @Nonnull - @Override - public AngleUnit getAngleUnits() { - return calculatorEngine.getAngleUnits(); - } - - @Override - public void setAngleUnits(@Nonnull AngleUnit angleUnits) { - calculatorEngine.setAngleUnits(angleUnits); - } - - @Override - public void setScienceNotation(@Nonnull Boolean scienceNotation) { - calculatorEngine.setScienceNotation(scienceNotation); - } - - private void softReset(@Nonnull SharedPreferences preferences) { - this.setPrecision(Preferences.precision.getPreference(preferences)); - this.setRoundResult(Preferences.roundResult.getPreference(preferences)); - this.setAngleUnits(getAngleUnitsFromPrefs(preferences)); - this.setNumeralBase(getNumeralBaseFromPrefs(preferences)); - this.setMultiplicationSign(Preferences.multiplicationSign.getPreference(preferences)); - this.setScienceNotation(Preferences.scienceNotation.getPreference(preferences)); + private void resetPreferences(@Nonnull SharedPreferences preferences) { + setPrecision(Preferences.precision.getPreference(preferences)); + setRoundResult(Preferences.roundResult.getPreference(preferences)); + setAngleUnits(getAngleUnitsFromPrefs(preferences)); + setNumeralBase(getNumeralBaseFromPrefs(preferences)); + setMultiplicationSign(Preferences.multiplicationSign.getPreference(preferences)); + setScienceNotation(Preferences.scienceNotation.getPreference(preferences)); final String groupingSeparator = Preferences.groupingSeparator.getPreference(preferences); if (Strings.isEmpty(groupingSeparator)) { - this.setUseGroupingSeparator(false); + setUseGroupingSeparator(false); } else { - this.setUseGroupingSeparator(true); + setUseGroupingSeparator(true); setGroupingSeparator(groupingSeparator.charAt(0)); } + Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.engine_preferences_changed, null); + } + + @Nonnull + public String getMultiplicationSign() { + return this.multiplicationSign; + } + + public void setMultiplicationSign(@Nonnull String multiplicationSign) { + this.multiplicationSign = multiplicationSign; + } + + public void setUseGroupingSeparator(boolean useGroupingSeparator) { + synchronized (lock) { + this.mathEngine.setUseGroupingSeparator(useGroupingSeparator); + } } - //for tests only - public void setDecimalGroupSymbols(@Nonnull DecimalFormatSymbols decimalGroupSymbols) { - this.calculatorEngine.setDecimalGroupSymbols(decimalGroupSymbols); + 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); + } } - @Override @Nonnull - public String getMultiplicationSign() { - return calculatorEngine.getMultiplicationSign(); + public AngleUnit getAngleUnits() { + synchronized (lock) { + return this.mathEngine.getAngleUnits(); + } } - @Override - public void setMultiplicationSign(@Nonnull String multiplicationSign) { - calculatorEngine.setMultiplicationSign(multiplicationSign); + 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.softReset(); + this.resetPreferences(); } } @@ -260,7 +277,7 @@ public class AndroidCalculatorEngine implements CalculatorEngine, SharedPreferen 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); - private static final List preferenceKeys = new ArrayList(); + private static final List preferenceKeys = new ArrayList<>(); static { preferenceKeys.add(groupingSeparator.getKey()); @@ -277,5 +294,4 @@ public class AndroidCalculatorEngine implements CalculatorEngine, SharedPreferen return Collections.unmodifiableList(preferenceKeys); } } - } diff --git a/app/src/main/java/org/solovyev/android/calculator/FunctionsRegistry.java b/app/src/main/java/org/solovyev/android/calculator/FunctionsRegistry.java index e88dc039..ed294d78 100644 --- a/app/src/main/java/org/solovyev/android/calculator/FunctionsRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/FunctionsRegistry.java @@ -37,6 +37,7 @@ import org.solovyev.common.text.Strings; import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -76,11 +77,11 @@ public class FunctionsRegistry extends BaseEntitiesRegistry @Override public void load() { add(new CustomFunction.Builder(true, "log", Arrays.asList("base", "x"), "ln(x)/ln(base)")); - add(new CustomFunction.Builder(true, "√3", Arrays.asList("x"), "x^(1/3)")); - add(new CustomFunction.Builder(true, "√4", Arrays.asList("x"), "x^(1/4)")); + add(new CustomFunction.Builder(true, "√3", Collections.singletonList("x"), "x^(1/3)")); + add(new CustomFunction.Builder(true, "√4", Collections.singletonList("x"), "x^(1/4)")); add(new CustomFunction.Builder(true, "√n", Arrays.asList("x", "n"), "x^(1/n)")); - add(new CustomFunction.Builder(true, "re", Arrays.asList("x"), "(x+conjugate(x))/2")); - add(new CustomFunction.Builder(true, "im", Arrays.asList("x"), "(x-conjugate(x))/(2*i)")); + add(new CustomFunction.Builder(true, "re", Collections.singletonList("x"), "(x+conjugate(x))/2")); + add(new CustomFunction.Builder(true, "im", Collections.singletonList("x"), "(x-conjugate(x))/(2*i)")); super.load(); } 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 2e8160a8..ef4f85a7 100644 --- a/app/src/main/java/org/solovyev/android/calculator/LiteNumberBuilder.java +++ b/app/src/main/java/org/solovyev/android/calculator/LiteNumberBuilder.java @@ -38,7 +38,7 @@ import jscl.NumeralBase; public class LiteNumberBuilder extends BaseNumberBuilder { - public LiteNumberBuilder(@Nonnull CalculatorEngine engine) { + public LiteNumberBuilder(@Nonnull Engine engine) { super(engine); this.nb = engine.getNumeralBase(); } diff --git a/app/src/main/java/org/solovyev/android/calculator/Locator.java b/app/src/main/java/org/solovyev/android/calculator/Locator.java index a996d949..2fe51e7f 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Locator.java +++ b/app/src/main/java/org/solovyev/android/calculator/Locator.java @@ -31,7 +31,7 @@ public class Locator implements CalculatorLocator { @Nonnull private static final Locator instance = new Locator(); @Nonnull - private CalculatorEngine calculatorEngine; + private Engine engine; @Nonnull private Calculator calculator; @Nonnull @@ -58,7 +58,7 @@ public class Locator implements CalculatorLocator { @Override public void init(@Nonnull Calculator calculator, - @Nonnull CalculatorEngine engine, + @Nonnull Engine engine, @Nonnull CalculatorClipboard clipboard, @Nonnull CalculatorNotifier notifier, @Nonnull ErrorReporter errorReporter, @@ -67,7 +67,7 @@ public class Locator implements CalculatorLocator { @Nonnull CalculatorPlotter plotter) { this.calculator = calculator; - this.calculatorEngine = engine; + this.engine = engine; this.calculatorClipboard = clipboard; this.calculatorNotifier = notifier; this.errorReporter = errorReporter; @@ -79,8 +79,8 @@ public class Locator implements CalculatorLocator { @Nonnull @Override - public CalculatorEngine getEngine() { - return calculatorEngine; + public Engine getEngine() { + return engine; } @Nonnull 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 a010a224..946b33a4 100644 --- a/app/src/main/java/org/solovyev/android/calculator/NumberBuilder.java +++ b/app/src/main/java/org/solovyev/android/calculator/NumberBuilder.java @@ -42,7 +42,7 @@ import java.util.List; */ public class NumberBuilder extends BaseNumberBuilder { - public NumberBuilder(@Nonnull CalculatorEngine engine) { + public NumberBuilder(@Nonnull Engine engine) { super(engine); } @@ -183,7 +183,7 @@ public class NumberBuilder extends BaseNumberBuilder { } // check if number still valid - toDouble(number, getNumeralBase(), engine.getEngine()); + toDouble(number, getNumeralBase(), engine.getMathEngine()); } catch (NumberFormatException e) { // number is not valid => stop @@ -196,6 +196,6 @@ public class NumberBuilder extends BaseNumberBuilder { nb = engine.getNumeralBase(); } - return replaceNumberInText(sb, number, trimmedChars, localNb, engine.getEngine()); + return replaceNumberInText(sb, number, trimmedChars, localNb, engine.getMathEngine()); } } diff --git a/app/src/main/java/org/solovyev/android/calculator/NumeralBaseButtons.java b/app/src/main/java/org/solovyev/android/calculator/NumeralBaseButtons.java index 5a8ebe82..117728d2 100644 --- a/app/src/main/java/org/solovyev/android/calculator/NumeralBaseButtons.java +++ b/app/src/main/java/org/solovyev/android/calculator/NumeralBaseButtons.java @@ -24,14 +24,13 @@ package org.solovyev.android.calculator; import android.app.Activity; import android.content.SharedPreferences; +import jscl.NumeralBase; import javax.annotation.Nonnull; -import jscl.NumeralBase; - import static jscl.NumeralBase.hex; +import static org.solovyev.android.calculator.Engine.Preferences.numeralBase; import static org.solovyev.android.calculator.Preferences.Gui.hideNumeralBaseDigits; -import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.numeralBase; /** * User: serso diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorOperatorsMathRegistry.java b/app/src/main/java/org/solovyev/android/calculator/OperatorsRegistry.java similarity index 88% rename from app/src/main/java/org/solovyev/android/calculator/CalculatorOperatorsMathRegistry.java rename to app/src/main/java/org/solovyev/android/calculator/OperatorsRegistry.java index a56b0380..cc1f96ac 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorOperatorsMathRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/OperatorsRegistry.java @@ -22,22 +22,15 @@ package org.solovyev.android.calculator; +import jscl.math.operator.Operator; import org.solovyev.common.JBuilder; import org.solovyev.common.math.MathRegistry; +import javax.annotation.Nonnull; import java.util.HashMap; import java.util.Map; -import javax.annotation.Nonnull; - -import jscl.math.operator.Operator; - -/** - * User: serso - * Date: 11/17/11 - * Time: 11:29 PM - */ -public class CalculatorOperatorsMathRegistry extends BaseEntitiesRegistry { +public class OperatorsRegistry extends BaseEntitiesRegistry { @Nonnull private static final Map substitutes = new HashMap(); @@ -53,8 +46,8 @@ public class CalculatorOperatorsMathRegistry extends BaseEntitiesRegistry functionsRegistry, - @Nonnull MathEntityDao mathEntityDao) { + public OperatorsRegistry(@Nonnull MathRegistry functionsRegistry, + @Nonnull MathEntityDao mathEntityDao) { super(functionsRegistry, OPERATOR_DESCRIPTION_PREFIX, mathEntityDao); } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorPostfixFunctionsRegistry.java b/app/src/main/java/org/solovyev/android/calculator/PostfixFunctionsRegistry.java similarity index 87% rename from app/src/main/java/org/solovyev/android/calculator/CalculatorPostfixFunctionsRegistry.java rename to app/src/main/java/org/solovyev/android/calculator/PostfixFunctionsRegistry.java index c42055ff..cb2cfae0 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorPostfixFunctionsRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/PostfixFunctionsRegistry.java @@ -22,22 +22,15 @@ package org.solovyev.android.calculator; +import jscl.math.operator.Operator; import org.solovyev.common.JBuilder; import org.solovyev.common.math.MathRegistry; +import javax.annotation.Nonnull; import java.util.HashMap; import java.util.Map; -import javax.annotation.Nonnull; - -import jscl.math.operator.Operator; - -/** - * User: serso - * Date: 11/19/11 - * Time: 1:48 PM - */ -public class CalculatorPostfixFunctionsRegistry extends BaseEntitiesRegistry { +public class PostfixFunctionsRegistry extends BaseEntitiesRegistry { @Nonnull private static final Map substitutes = new HashMap(); @@ -51,8 +44,8 @@ public class CalculatorPostfixFunctionsRegistry extends BaseEntitiesRegistry functionsRegistry, - @Nonnull MathEntityDao mathEntityDao) { + public PostfixFunctionsRegistry(@Nonnull MathRegistry functionsRegistry, + @Nonnull MathEntityDao mathEntityDao) { super(functionsRegistry, POSTFIX_FUNCTION_DESCRIPTION_PREFIX, mathEntityDao); } diff --git a/app/src/main/java/org/solovyev/android/calculator/Preferences.java b/app/src/main/java/org/solovyev/android/calculator/Preferences.java index 26790a1e..91b458b1 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Preferences.java +++ b/app/src/main/java/org/solovyev/android/calculator/Preferences.java @@ -31,31 +31,22 @@ import android.support.annotation.LayoutRes; import android.support.annotation.StyleRes; import android.util.SparseArray; import android.view.ContextThemeWrapper; - +import jscl.AngleUnit; +import jscl.NumeralBase; import org.solovyev.android.Check; import org.solovyev.android.calculator.language.Languages; import org.solovyev.android.calculator.math.MathType; -import org.solovyev.android.calculator.model.AndroidCalculatorEngine; import org.solovyev.android.calculator.preferences.PurchaseDialogActivity; import org.solovyev.android.calculator.wizard.WizardActivity; -import org.solovyev.android.prefs.BooleanPreference; -import org.solovyev.android.prefs.IntegerPreference; -import org.solovyev.android.prefs.LongPreference; -import org.solovyev.android.prefs.NumberToStringPreference; -import org.solovyev.android.prefs.Preference; -import org.solovyev.android.prefs.StringPreference; +import org.solovyev.android.prefs.*; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.text.DecimalFormatSymbols; import java.util.EnumMap; import java.util.Locale; import java.util.Map; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import jscl.AngleUnit; -import jscl.NumeralBase; - import static org.solovyev.android.Android.isPhoneModel; import static org.solovyev.android.DeviceModel.samsung_galaxy_s; import static org.solovyev.android.DeviceModel.samsung_galaxy_s_2; @@ -70,7 +61,7 @@ public final class Preferences { static void setDefaultValues(@Nonnull SharedPreferences preferences) { - if (!AndroidCalculatorEngine.Preferences.groupingSeparator.isSet(preferences)) { + if (!Engine.Preferences.groupingSeparator.isSet(preferences)) { final Locale locale = Locale.getDefault(); if (locale != null) { final DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(locale); @@ -82,22 +73,22 @@ public final class Preferences { groupingSeparator = " "; } - AndroidCalculatorEngine.Preferences.groupingSeparator.putPreference(preferences, groupingSeparator); + Engine.Preferences.groupingSeparator.putPreference(preferences, groupingSeparator); } } - if (!AndroidCalculatorEngine.Preferences.angleUnit.isSet(preferences)) { - AndroidCalculatorEngine.Preferences.angleUnit.putDefault(preferences); + if (!Engine.Preferences.angleUnit.isSet(preferences)) { + Engine.Preferences.angleUnit.putDefault(preferences); } - if (!AndroidCalculatorEngine.Preferences.numeralBase.isSet(preferences)) { - AndroidCalculatorEngine.Preferences.numeralBase.putDefault(preferences); + if (!Engine.Preferences.numeralBase.isSet(preferences)) { + Engine.Preferences.numeralBase.putDefault(preferences); } - if (!AndroidCalculatorEngine.Preferences.multiplicationSign.isSet(preferences)) { + if (!Engine.Preferences.multiplicationSign.isSet(preferences)) { if (isPhoneModel(samsung_galaxy_s) || isPhoneModel(samsung_galaxy_s_2)) { // workaround ofr samsung galaxy s phones - AndroidCalculatorEngine.Preferences.multiplicationSign.putPreference(preferences, "*"); + Engine.Preferences.multiplicationSign.putPreference(preferences, "*"); } } @@ -247,8 +238,8 @@ public final class Preferences { public static final Preference calculateOnFly = BooleanPreference.of("calculations_calculate_on_fly", true); public static final Preference showCalculationMessagesDialog = BooleanPreference.of("show_calculation_messages_dialog", true); - public static final Preference preferredNumeralBase = StringPreference.ofEnum("preferred_numeral_base", AndroidCalculatorEngine.Preferences.numeralBase.getDefaultValue(), NumeralBase.class); - public static final Preference preferredAngleUnits = StringPreference.ofEnum("preferred_angle_units", AndroidCalculatorEngine.Preferences.angleUnit.getDefaultValue(), AngleUnit.class); + public static final Preference preferredNumeralBase = StringPreference.ofEnum("preferred_numeral_base", Engine.Preferences.numeralBase.getDefaultValue(), NumeralBase.class); + public static final Preference preferredAngleUnits = StringPreference.ofEnum("preferred_angle_units", Engine.Preferences.angleUnit.getDefaultValue(), AngleUnit.class); public static final Preference lastPreferredPreferencesCheck = LongPreference.of("preferred_preferences_check_time", 0L); } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorVarsRegistry.java b/app/src/main/java/org/solovyev/android/calculator/VarsRegistry.java similarity index 89% rename from app/src/main/java/org/solovyev/android/calculator/CalculatorVarsRegistry.java rename to app/src/main/java/org/solovyev/android/calculator/VarsRegistry.java index c73670c6..24cdda63 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorVarsRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/VarsRegistry.java @@ -22,6 +22,7 @@ package org.solovyev.android.calculator; +import jscl.math.function.IConstant; import org.solovyev.android.calculator.model.MathEntityBuilder; import org.solovyev.android.calculator.model.Var; import org.solovyev.android.calculator.model.Vars; @@ -29,20 +30,12 @@ import org.solovyev.common.JBuilder; import org.solovyev.common.math.MathEntity; import org.solovyev.common.math.MathRegistry; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.HashMap; import java.util.Map; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import jscl.math.function.IConstant; - -/** - * User: serso - * Date: 9/29/11 - * Time: 4:57 PM - */ -public class CalculatorVarsRegistry extends BaseEntitiesRegistry { +public class VarsRegistry extends BaseEntitiesRegistry { @Nonnull public static final String ANS = "ans"; @@ -58,8 +51,8 @@ public class CalculatorVarsRegistry extends BaseEntitiesRegistry substitutes.put("NaN", "nan"); } - public CalculatorVarsRegistry(@Nonnull MathRegistry mathRegistry, - @Nonnull MathEntityDao mathEntityDao) { + public VarsRegistry(@Nonnull MathRegistry mathRegistry, + @Nonnull MathEntityDao mathEntityDao) { super(mathRegistry, "c_var_description_", mathEntityDao); } @@ -93,12 +86,6 @@ public class CalculatorVarsRegistry extends BaseEntitiesRegistry tryToAddAuxVar("y"); tryToAddAuxVar("t"); tryToAddAuxVar("j"); - - - /*Log.d(AndroidVarsRegistry.class.getName(), vars.size() + " variables registered!"); - for (Var var : vars) { - Log.d(AndroidVarsRegistry.class.getName(), var.toString()); - }*/ } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/History.java b/app/src/main/java/org/solovyev/android/calculator/history/History.java index fc6b8a1a..e619a6db 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/History.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/History.java @@ -34,7 +34,7 @@ import org.json.JSONArray; import org.json.JSONException; import org.solovyev.android.Check; import org.solovyev.android.calculator.*; -import org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences; +import org.solovyev.android.calculator.Engine.Preferences; import org.solovyev.android.io.FileLoader; import org.solovyev.android.io.FileSaver; diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/VarEditorSaver.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/VarEditorSaver.java index 972e9f99..8b646155 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/VarEditorSaver.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/VarEditorSaver.java @@ -28,7 +28,7 @@ import jscl.text.Identifier; import jscl.text.MutableInt; import jscl.text.ParseException; import jscl.text.Parser; -import org.solovyev.android.calculator.CalculatorVarsRegistry; +import org.solovyev.android.calculator.VarsRegistry; import org.solovyev.android.calculator.EntitiesRegistry; import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.R; @@ -76,7 +76,7 @@ public class VarEditorSaver implements View.OnClickListene if (!Strings.isEmpty(name)) { try { if (name == null) throw new AssertionError(); - Identifier.parser.parse(Parser.Parameters.newInstance(name, new MutableInt(0), Locator.getInstance().getEngine().getEngine()), null); + Identifier.parser.parse(Parser.Parameters.newInstance(name, new MutableInt(0), Locator.getInstance().getEngine().getMathEngine()), null); result = true; } catch (ParseException e) { // not valid name; @@ -147,7 +147,7 @@ public class VarEditorSaver implements View.OnClickListene if (error != null) { Locator.getInstance().getNotifier().showMessage(error, MessageType.error); } else { - CalculatorVarsRegistry.saveVariable(mathRegistry, varBuilder, editedInstance, source, true); + VarsRegistry.saveVariable(mathRegistry, varBuilder, editedInstance, source, true); } } } diff --git a/app/src/main/java/org/solovyev/android/calculator/plot/AbstractCalculatorPlotFragment.java b/app/src/main/java/org/solovyev/android/calculator/plot/AbstractCalculatorPlotFragment.java index 570eb77a..a7171f7a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/plot/AbstractCalculatorPlotFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/plot/AbstractCalculatorPlotFragment.java @@ -59,7 +59,7 @@ import java.util.List; import java.util.concurrent.Executor; import java.util.concurrent.Executors; -import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences; +import static org.solovyev.android.calculator.Engine.Preferences; /** * User: serso 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 44c4aea2..61f6e32a 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 @@ -22,8 +22,8 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.List; -import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.precision; -import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.roundResult; +import static org.solovyev.android.calculator.Engine.Preferences.precision; +import static org.solovyev.android.calculator.Engine.Preferences.roundResult; import static org.solovyev.android.calculator.wizard.CalculatorWizards.DEFAULT_WIZARD_FLOW; import static org.solovyev.android.wizard.WizardUi.startWizard; 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 index e4212d56..421db060 100644 --- a/app/src/main/java/org/solovyev/android/calculator/view/CalculatorAdditionalTitle.java +++ b/app/src/main/java/org/solovyev/android/calculator/view/CalculatorAdditionalTitle.java @@ -27,8 +27,8 @@ 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 org.solovyev.android.calculator.model.AndroidCalculatorEngine; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -58,8 +58,8 @@ public class CalculatorAdditionalTitle extends TextView implements SharedPrefere @Override public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String key) { - setText(((AndroidCalculatorEngine) Locator.getInstance().getEngine()).getNumeralBaseFromPrefs(preferences) + setText(((Engine) Locator.getInstance().getEngine()).getNumeralBaseFromPrefs(preferences) + " / " + - ((AndroidCalculatorEngine) Locator.getInstance().getEngine()).getAngleUnitsFromPrefs(preferences)); + ((Engine) Locator.getInstance().getEngine()).getAngleUnitsFromPrefs(preferences)); } } diff --git a/app/src/main/java/org/solovyev/android/calculator/view/TextHighlighter.java b/app/src/main/java/org/solovyev/android/calculator/view/TextHighlighter.java index 8ebd55a0..bc5e45f9 100644 --- a/app/src/main/java/org/solovyev/android/calculator/view/TextHighlighter.java +++ b/app/src/main/java/org/solovyev/android/calculator/view/TextHighlighter.java @@ -26,24 +26,17 @@ import android.graphics.Typeface; import android.text.SpannableStringBuilder; import android.text.style.ForegroundColorSpan; import android.text.style.StyleSpan; - import com.google.common.collect.Lists; - import org.solovyev.android.Check; -import org.solovyev.android.calculator.BaseNumberBuilder; -import org.solovyev.android.calculator.CalculatorEngine; -import org.solovyev.android.calculator.LiteNumberBuilder; -import org.solovyev.android.calculator.Locator; -import org.solovyev.android.calculator.NumberBuilder; +import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.text.TextProcessor; import org.solovyev.android.calculator.text.TextProcessorEditorResult; +import javax.annotation.Nonnull; import java.util.ArrayList; import java.util.List; -import javax.annotation.Nonnull; - public class TextHighlighter implements TextProcessor { private final int red; @@ -84,7 +77,7 @@ public class TextHighlighter implements TextProcessor0x:FF33233FFE", textHighlighter.process("0x:FF33233FFE").toString()); assertEquals("0x:FF33 233 FFE", textHighlighter.process("0x:FF33 233 FFE").toString()); - final MathEngine me = Locator.getInstance().getEngine().getMathEngine0(); + final MathEngine me = Locator.getInstance().getEngine().getMathEngine(); try { me.setNumeralBase(NumeralBase.hex); assertEquals("E", textHighlighter.process("E").toString()); diff --git a/app/src/test/java/org/solovyev/android/calculator/history/HistoryTest.java b/app/src/test/java/org/solovyev/android/calculator/history/HistoryTest.java index 31b33034..f510f4ed 100644 --- a/app/src/test/java/org/solovyev/android/calculator/history/HistoryTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/history/HistoryTest.java @@ -24,9 +24,7 @@ package org.solovyev.android.calculator.history; import android.content.SharedPreferences; import android.os.Handler; - import com.squareup.otto.Bus; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -34,27 +32,17 @@ import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; import org.solovyev.android.CalculatorTestRunner; -import org.solovyev.android.calculator.BuildConfig; -import org.solovyev.android.calculator.Display; -import org.solovyev.android.calculator.DisplayState; -import org.solovyev.android.calculator.Editor; -import org.solovyev.android.calculator.EditorState; +import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.jscl.JsclOperation; +import javax.annotation.Nonnull; import java.io.File; import java.util.List; -import javax.annotation.Nonnull; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; +import static org.junit.Assert.*; import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.groupingSeparator; +import static org.mockito.Mockito.*; +import static org.solovyev.android.calculator.Engine.Preferences.groupingSeparator; @Config(constants = BuildConfig.class, sdk = CalculatorTestRunner.SUPPORTED_SDK) @RunWith(RobolectricGradleTestRunner.class) diff --git a/app/src/test/java/org/solovyev/android/calculator/model/AndroidCalculatorEngineTest.java b/app/src/test/java/org/solovyev/android/calculator/model/AndroidEngineTest.java similarity index 97% rename from app/src/test/java/org/solovyev/android/calculator/model/AndroidCalculatorEngineTest.java rename to app/src/test/java/org/solovyev/android/calculator/model/AndroidEngineTest.java index 47e3d8c3..6ab1c8b2 100644 --- a/app/src/test/java/org/solovyev/android/calculator/model/AndroidCalculatorEngineTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/model/AndroidEngineTest.java @@ -53,7 +53,7 @@ import static org.junit.Assert.fail; */ @SuppressWarnings("deprecation") -public class AndroidCalculatorEngineTest extends AbstractCalculatorTest { +public class AndroidEngineTest extends AbstractCalculatorTest { @BeforeClass public static void staticSetUp() throws Exception { @@ -64,7 +64,7 @@ public class AndroidCalculatorEngineTest extends AbstractCalculatorTest { @Test public void testDegrees() throws Exception { - final MathEngine cm = Locator.getInstance().getEngine().getMathEngine0(); + final MathEngine cm = Locator.getInstance().getEngine().getMathEngine(); final AngleUnit defaultAngleUnit = cm.getAngleUnits(); try { @@ -85,7 +85,7 @@ public class AndroidCalculatorEngineTest extends AbstractCalculatorTest { @Test public void testFormatting() throws Exception { - final MathEngine ce = Locator.getInstance().getEngine().getMathEngine0(); + final MathEngine ce = Locator.getInstance().getEngine().getMathEngine(); CalculatorTestUtils.assertEval("12 345", ce.simplify("12345")); @@ -93,7 +93,7 @@ public class AndroidCalculatorEngineTest extends AbstractCalculatorTest { @Test public void testI() throws ParseException, CalculatorEvalException { - final MathEngine cm = Locator.getInstance().getEngine().getMathEngine0(); + final MathEngine cm = Locator.getInstance().getEngine().getMathEngine(); CalculatorTestUtils.assertEval("-i", cm.evaluate("i^3")); for (int i = 0; i < 1000; i++) { @@ -118,7 +118,7 @@ public class AndroidCalculatorEngineTest extends AbstractCalculatorTest { @Test public void testEmptyFunction() throws Exception { - final MathEngine cm = Locator.getInstance().getEngine().getMathEngine0(); + final MathEngine cm = Locator.getInstance().getEngine().getMathEngine(); try { cm.evaluate("cos(cos(cos(cos(acos(acos(acos(acos(acos(acos(acos(acos(cos(cos(cos(cos(cosh(acos(cos(cos(cos(cos(cos(acos(acos(acos(acos(acos(acos(acos(acos(cos(cos(cos(cos(cosh(acos(cos())))))))))))))))))))))))))))))))))))))"); Assert.fail(); @@ -147,7 +147,7 @@ public class AndroidCalculatorEngineTest extends AbstractCalculatorTest { @Test public void testRounding() throws Exception { - final MathEngine cm = Locator.getInstance().getEngine().getMathEngine0(); + final MathEngine cm = Locator.getInstance().getEngine().getMathEngine(); try { DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(Locale.getDefault()); @@ -171,7 +171,7 @@ public class AndroidCalculatorEngineTest extends AbstractCalculatorTest { @Test public void testNumeralSystems() throws Exception { - final MathEngine cm = Locator.getInstance().getEngine().getMathEngine0(); + final MathEngine cm = Locator.getInstance().getEngine().getMathEngine(); CalculatorTestUtils.assertEval("11 259 375", "0x:ABCDEF"); CalculatorTestUtils.assertEval("30 606 154.462", "0x:ABCDEF*e"); @@ -200,7 +200,7 @@ public class AndroidCalculatorEngineTest extends AbstractCalculatorTest { @Test public void testLog() throws Exception { - final MathEngine cm = Locator.getInstance().getEngine().getMathEngine0(); + final MathEngine cm = Locator.getInstance().getEngine().getMathEngine(); CalculatorTestUtils.assertEval("∞", Expression.valueOf("1/0").numeric().toString()); CalculatorTestUtils.assertEval("∞", Expression.valueOf("ln(10)/ln(1)").numeric().toString()); 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 ec1ae944..f958573b 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 @@ -54,7 +54,7 @@ public class EvaluateTest extends AbstractCalculatorTest { @Test public void testEvaluate() throws Exception { - final MathEngine cm = Locator.getInstance().getEngine().getMathEngine0(); + final MathEngine cm = Locator.getInstance().getEngine().getMathEngine(); final AngleUnit defaultAngleUnit = cm.getAngleUnits(); 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 c74c6ea9..1e0f84fc 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 @@ -58,11 +58,11 @@ public class NumeralBaseTest extends AbstractCalculatorTest { final String bin = "0b:" + line[2].toUpperCase(); final String decExpression = converter.convert(dec); - final String decResult = Locator.getInstance().getEngine().getEngine().evaluate(decExpression); + final String decResult = Locator.getInstance().getEngine().getMathEngine().evaluate(decExpression); final String hexExpression = converter.convert(hex); - final String hexResult = Locator.getInstance().getEngine().getEngine().evaluate(hexExpression); + final String hexResult = Locator.getInstance().getEngine().getMathEngine().evaluate(hexExpression); final String binExpression = converter.convert(bin); - final String binResult = Locator.getInstance().getEngine().getEngine().evaluate(binExpression); + final String binResult = Locator.getInstance().getEngine().getMathEngine().evaluate(binExpression); Assert.assertEquals("dec-hex: " + decExpression + " : " + hexExpression, decResult, hexResult); Assert.assertEquals("dec-bin: " + decExpression + " : " + binExpression, decResult, binResult);