From 67a386f573b97369a904d6d2c7c38a0d09923ed3 Mon Sep 17 00:00:00 2001 From: serso Date: Tue, 19 Jan 2016 22:48:35 +0100 Subject: [PATCH] Engine interface removed --- .../calculator/CalculatorApplication.java | 3 +- .../android/calculator/CalculatorEngine.java | 5 +- .../calculator/CalculatorEngineImpl.java | 72 +------------------ .../android/calculator/CalculatorImpl.java | 29 ++++---- .../calculator/CalculatorMathEngine.java | 54 -------------- .../android/calculator/NumberBuilder.java | 25 +++---- .../calculator/jscl/JsclOperation.java | 11 ++- .../calculator/math/edit/VarEditorSaver.java | 19 ++--- .../model/AndroidCalculatorEngine.java | 10 +-- .../model/AndroidMathEntityDao.java | 9 +-- .../calculator/model/NumeralBaseTest.java | 32 ++++----- 11 files changed, 55 insertions(+), 214 deletions(-) delete mode 100644 app/src/main/java/org/solovyev/android/calculator/CalculatorMathEngine.java 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 8c18473c..eb219795 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java @@ -28,6 +28,7 @@ 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; @@ -143,7 +144,7 @@ public class CalculatorApplication extends android.app.Application implements Sh private void warmUpEngine() { try { // warm-up engine - CalculatorMathEngine mathEngine = Locator.getInstance().getEngine().getMathEngine(); + MathEngine mathEngine = Locator.getInstance().getEngine().getEngine(); mathEngine.evaluate("1+1"); mathEngine.evaluate("1*1"); } catch (Throwable e) { diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorEngine.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorEngine.java index 319cd936..89b67838 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorEngine.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorEngine.java @@ -47,10 +47,7 @@ public interface CalculatorEngine { @Nonnull EntitiesRegistry getPostfixFunctionsRegistry(); @Nonnull - CalculatorMathEngine getMathEngine(); - @Deprecated - @Nonnull - MathEngine getMathEngine0(); + MathEngine getEngine(); @Nonnull String getMultiplicationSign(); diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorEngineImpl.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorEngineImpl.java index d2116be0..6f2d6141 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorEngineImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorEngineImpl.java @@ -26,11 +26,9 @@ import jscl.AngleUnit; import jscl.JsclMathEngine; import jscl.MathEngine; import jscl.NumeralBase; -import jscl.math.Generic; import jscl.math.function.Function; import jscl.math.function.IConstant; import jscl.math.operator.Operator; -import jscl.text.ParseException; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -58,9 +56,6 @@ public class CalculatorEngineImpl implements CalculatorEngine { @Nonnull private final MathEngine engine; - @Nonnull - private final CalculatorMathEngine mathEngine; - @Nonnull private final EntitiesRegistry varsRegistry; @@ -96,7 +91,6 @@ public class CalculatorEngineImpl implements CalculatorEngine { @Nullable Object lock) { this.engine = engine; - this.mathEngine = new JsclCalculatorMathEngine(engine); this.engine.setRoundResult(true); this.engine.setUseGroupingSeparator(true); @@ -141,17 +135,11 @@ public class CalculatorEngineImpl implements CalculatorEngine { @Nonnull @Override - public CalculatorMathEngine getMathEngine() { - return this.mathEngine; + public MathEngine getEngine() { + return engine; } - @Nonnull - @Override - public MathEngine getMathEngine0() { - return this.engine; - } - - /* + /* ********************************************************************** * * INIT @@ -271,58 +259,4 @@ public class CalculatorEngineImpl implements CalculatorEngine { this.engine.setDecimalGroupSymbols(decimalGroupSymbols); } } - - /* - ********************************************************************** - * - * STATIC CLASSES - * - ********************************************************************** - */ - - private static final class JsclCalculatorMathEngine implements CalculatorMathEngine { - - @Nonnull - private final MathEngine mathEngine; - - private JsclCalculatorMathEngine(@Nonnull MathEngine mathEngine) { - this.mathEngine = mathEngine; - } - - @Nonnull - @Override - public String evaluate(@Nonnull String expression) throws ParseException { - return this.mathEngine.evaluate(expression); - } - - @Nonnull - @Override - public String simplify(@Nonnull String expression) throws ParseException { - return this.mathEngine.simplify(expression); - } - - @Nonnull - @Override - public String elementary(@Nonnull String expression) throws ParseException { - return this.mathEngine.elementary(expression); - } - - @Nonnull - @Override - public Generic evaluateGeneric(@Nonnull String expression) throws ParseException { - return this.mathEngine.evaluateGeneric(expression); - } - - @Nonnull - @Override - public Generic simplifyGeneric(@Nonnull String expression) throws ParseException { - return this.mathEngine.simplifyGeneric(expression); - } - - @Nonnull - @Override - public Generic elementaryGeneric(@Nonnull String expression) throws ParseException { - return this.mathEngine.elementaryGeneric(expression); - } - } } 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 f7c25ef6..15ba9747 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java @@ -24,10 +24,17 @@ 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; @@ -40,24 +47,14 @@ 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; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import jscl.AbstractJsclArithmeticException; -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; - /** * User: Solovyev_S * Date: 20.09.12 @@ -258,10 +255,10 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { try { - final CalculatorMathEngine mathEngine = Locator.getInstance().getEngine().getMathEngine(); + final MathEngine mathEngine = Locator.getInstance().getEngine().getEngine(); final MessageRegistry messageRegistry = new ListMessageRegistry(); - Locator.getInstance().getEngine().getMathEngine0().setMessageRegistry(messageRegistry); + Locator.getInstance().getEngine().getEngine().setMessageRegistry(messageRegistry); final Generic result = operation.evaluateGeneric(jsclExpression, mathEngine); diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorMathEngine.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorMathEngine.java deleted file mode 100644 index 17ce17ce..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorMathEngine.java +++ /dev/null @@ -1,54 +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 javax.annotation.Nonnull; - -import jscl.math.Generic; -import jscl.text.ParseException; - -/** - * User: serso - * Date: 9/23/12 - * Time: 6:05 PM - */ -public interface CalculatorMathEngine { - - @Nonnull - String evaluate(@Nonnull String expression) throws ParseException; - - @Nonnull - String simplify(@Nonnull String expression) throws ParseException; - - @Nonnull - String elementary(@Nonnull String expression) throws ParseException; - - @Nonnull - Generic evaluateGeneric(@Nonnull String expression) throws ParseException; - - @Nonnull - Generic simplifyGeneric(@Nonnull String expression) throws ParseException; - - @Nonnull - Generic elementaryGeneric(@Nonnull String expression) throws ParseException; -} 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 def678de..a010a224 100644 --- a/app/src/main/java/org/solovyev/android/calculator/NumberBuilder.java +++ b/app/src/main/java/org/solovyev/android/calculator/NumberBuilder.java @@ -23,24 +23,17 @@ 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.DoubleParser; -import jscl.text.JsclIntegerParser; -import jscl.text.MutableInt; -import jscl.text.ParseException; -import jscl.text.Parser; +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; /** * User: serso @@ -190,7 +183,7 @@ public class NumberBuilder extends BaseNumberBuilder { } // check if number still valid - toDouble(number, getNumeralBase(), engine.getMathEngine0()); + toDouble(number, getNumeralBase(), engine.getEngine()); } catch (NumberFormatException e) { // number is not valid => stop @@ -203,6 +196,6 @@ public class NumberBuilder extends BaseNumberBuilder { nb = engine.getNumeralBase(); } - return replaceNumberInText(sb, number, trimmedChars, localNb, engine.getMathEngine0()); + return replaceNumberInText(sb, number, trimmedChars, localNb, engine.getEngine()); } } diff --git a/app/src/main/java/org/solovyev/android/calculator/jscl/JsclOperation.java b/app/src/main/java/org/solovyev/android/calculator/jscl/JsclOperation.java index d7b6f5b2..214a5dc1 100644 --- a/app/src/main/java/org/solovyev/android/calculator/jscl/JsclOperation.java +++ b/app/src/main/java/org/solovyev/android/calculator/jscl/JsclOperation.java @@ -23,16 +23,15 @@ package org.solovyev.android.calculator.jscl; -import org.solovyev.android.calculator.CalculatorMathEngine; +import jscl.MathEngine; +import jscl.math.Generic; +import jscl.text.ParseException; import org.solovyev.android.calculator.text.DummyTextProcessor; import org.solovyev.android.calculator.text.FromJsclSimplifyTextProcessor; import org.solovyev.android.calculator.text.TextProcessor; import javax.annotation.Nonnull; -import jscl.math.Generic; -import jscl.text.ParseException; - public enum JsclOperation { simplify, @@ -58,7 +57,7 @@ public enum JsclOperation { } @Nonnull - public final String evaluate(@Nonnull String expression, @Nonnull CalculatorMathEngine engine) throws ParseException { + public final String evaluate(@Nonnull String expression, @Nonnull MathEngine engine) throws ParseException { switch (this) { case simplify: return engine.simplify(expression); @@ -72,7 +71,7 @@ public enum JsclOperation { } @Nonnull - public final Generic evaluateGeneric(@Nonnull String expression, @Nonnull CalculatorMathEngine engine) throws ParseException { + public final Generic evaluateGeneric(@Nonnull String expression, @Nonnull MathEngine engine) throws ParseException { switch (this) { case simplify: return engine.simplifyGeneric(expression); 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 f1596c77..972e9f99 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 @@ -24,9 +24,12 @@ package org.solovyev.android.calculator.math.edit; import android.view.View; import android.widget.EditText; - -import org.solovyev.android.calculator.EntitiesRegistry; +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.EntitiesRegistry; import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.math.MathType; @@ -38,16 +41,6 @@ import org.solovyev.common.text.Strings; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import jscl.text.Identifier; -import jscl.text.MutableInt; -import jscl.text.ParseException; -import jscl.text.Parser; - -/** - * User: serso - * Date: 12/22/11 - * Time: 9:52 PM - */ public class VarEditorSaver implements View.OnClickListener { @Nonnull @@ -83,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().getMathEngine0()), null); + Identifier.parser.parse(Parser.Parameters.newInstance(name, new MutableInt(0), Locator.getInstance().getEngine().getEngine()), null); result = true; } catch (ParseException e) { // not valid name; diff --git a/app/src/main/java/org/solovyev/android/calculator/model/AndroidCalculatorEngine.java b/app/src/main/java/org/solovyev/android/calculator/model/AndroidCalculatorEngine.java index 806d17a8..66c1ba42 100644 --- a/app/src/main/java/org/solovyev/android/calculator/model/AndroidCalculatorEngine.java +++ b/app/src/main/java/org/solovyev/android/calculator/model/AndroidCalculatorEngine.java @@ -133,14 +133,8 @@ public class AndroidCalculatorEngine implements CalculatorEngine, SharedPreferen @Override @Nonnull - public CalculatorMathEngine getMathEngine() { - return calculatorEngine.getMathEngine(); - } - - @Nonnull - @Override - public MathEngine getMathEngine0() { - return calculatorEngine.getMathEngine0(); + public MathEngine getEngine() { + return calculatorEngine.getEngine(); } @Nonnull diff --git a/app/src/main/java/org/solovyev/android/calculator/model/AndroidMathEntityDao.java b/app/src/main/java/org/solovyev/android/calculator/model/AndroidMathEntityDao.java index 038398cf..23a9ec89 100644 --- a/app/src/main/java/org/solovyev/android/calculator/model/AndroidMathEntityDao.java +++ b/app/src/main/java/org/solovyev/android/calculator/model/AndroidMathEntityDao.java @@ -27,7 +27,6 @@ import android.content.Context; import android.content.SharedPreferences; import android.content.res.Resources; import android.preference.PreferenceManager; - import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; import org.solovyev.android.calculator.CalculatorApplication; @@ -35,16 +34,10 @@ import org.solovyev.android.calculator.MathEntityDao; import org.solovyev.android.calculator.MathEntityPersistenceContainer; import org.solovyev.android.calculator.MathPersistenceEntity; -import java.io.StringWriter; - import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.io.StringWriter; -/** - * User: serso - * Date: 10/7/12 - * Time: 6:46 PM - */ public class AndroidMathEntityDao implements MathEntityDao { @Nonnull 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 f1b2241e..c74c6ea9 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,28 +22,22 @@ 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 @@ -64,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().getMathEngine().evaluate(decExpression); + final String decResult = Locator.getInstance().getEngine().getEngine().evaluate(decExpression); final String hexExpression = converter.convert(hex); - final String hexResult = Locator.getInstance().getEngine().getMathEngine().evaluate(hexExpression); + final String hexResult = Locator.getInstance().getEngine().getEngine().evaluate(hexExpression); final String binExpression = converter.convert(bin); - final String binResult = Locator.getInstance().getEngine().getMathEngine().evaluate(binExpression); + final String binResult = Locator.getInstance().getEngine().getEngine().evaluate(binExpression); Assert.assertEquals("dec-hex: " + decExpression + " : " + hexExpression, decResult, hexResult); Assert.assertEquals("dec-bin: " + decExpression + " : " + binExpression, decResult, binResult);