diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java index d6d50ec3..7f3af56b 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java @@ -101,7 +101,7 @@ public enum CalculatorEngine { private MathEngine engine = JsclMathEngine.instance; @NotNull - public final TextProcessor preprocessor = new ToJsclTextProcessor(); + public final TextProcessor preprocessor = ToJsclTextProcessor.getInstance(); @NotNull private final AndroidMathRegistry varsRegistry = new AndroidVarsRegistryImpl(engine.getConstantsRegistry()); diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/ToJsclTextProcessor.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/model/ToJsclTextProcessor.java index 9fd64ac4..5c5df0a4 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/model/ToJsclTextProcessor.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/model/ToJsclTextProcessor.java @@ -15,12 +15,24 @@ import org.solovyev.common.utils.CollectionsUtils; import java.util.ArrayList; import java.util.List; -class ToJsclTextProcessor implements TextProcessor { +public class ToJsclTextProcessor implements TextProcessor { @NotNull private static final Integer MAX_DEPTH = 20; - @Override + @NotNull + private static final TextProcessor instance = new ToJsclTextProcessor(); + + private ToJsclTextProcessor() { + } + + + @NotNull + public static TextProcessor getInstance() { + return instance; + } + + @Override @NotNull public PreparedExpression process(@NotNull String s) throws CalculatorParseException { return processWithDepth(s, 0, new ArrayList()); diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/plot/CalculatorPlotActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/plot/CalculatorPlotActivity.java index 4e83ad13..4f610f8a 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/plot/CalculatorPlotActivity.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/plot/CalculatorPlotActivity.java @@ -35,6 +35,9 @@ import org.achartengine.tools.ZoomListener; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.model.CalculatorParseException; +import org.solovyev.android.calculator.model.PreparedExpression; +import org.solovyev.android.calculator.model.ToJsclTextProcessor; import org.solovyev.common.utils.MutableObject; import java.io.Serializable; @@ -79,7 +82,8 @@ public class CalculatorPlotActivity extends Activity { final Input input = (Input) extras.getSerializable(INPUT); try { - this.expression = Expression.valueOf(input.getExpression()); + final PreparedExpression preparedExpression = ToJsclTextProcessor.getInstance().process(input.getExpression()); + this.expression = Expression.valueOf(preparedExpression.getExpression()); this.variable = new Constant(input.getVariableName()); String title = extras.getString(ChartFactory.TITLE); @@ -100,8 +104,11 @@ public class CalculatorPlotActivity extends Activity { } catch (ArithmeticException e) { Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show(); finish(); - } - } + } catch (CalculatorParseException e) { + Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show(); + finish(); + } + } private void setGraphicalView(@Nullable PlotBoundaries plotBoundaries) { double minValue = plotBoundaries == null ? DEFAULT_MIN_NUMBER : plotBoundaries.xMin; diff --git a/calculatorpp/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java b/calculatorpp/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java index 68756933..e1f92a65 100644 --- a/calculatorpp/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java +++ b/calculatorpp/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java @@ -26,13 +26,13 @@ public class ToJsclTextProcessorTest { @Test public void testSpecialCases() throws CalculatorParseException { - final ToJsclTextProcessor preprocessor = new ToJsclTextProcessor(); + final TextProcessor preprocessor = ToJsclTextProcessor.getInstance(); Assert.assertEquals( "3^E10", preprocessor.process("3^E10").toString()); } @Test public void testProcess() throws Exception { - final ToJsclTextProcessor preprocessor = new ToJsclTextProcessor(); + final TextProcessor preprocessor = ToJsclTextProcessor.getInstance(); Assert.assertEquals( "", preprocessor.process("").toString()); Assert.assertEquals( "()", preprocessor.process("[]").toString()); @@ -113,7 +113,7 @@ public class ToJsclTextProcessorTest { @Test public void testDegrees() throws Exception { - final ToJsclTextProcessor preprocessor = new ToJsclTextProcessor(); + final TextProcessor preprocessor = ToJsclTextProcessor.getInstance(); Assert.assertEquals( "", preprocessor.process("").toString()); /* try { @@ -145,7 +145,7 @@ public class ToJsclTextProcessorTest { @Test public void testNumeralBases() throws Exception { - final ToJsclTextProcessor processor = new ToJsclTextProcessor(); + final TextProcessor processor = ToJsclTextProcessor.getInstance(); final NumeralBase defaultNumeralBase = JsclMathEngine.instance.getNumeralBase(); try{