From 81510d18ea86841474a01a93dbeaa08f70e2759b Mon Sep 17 00:00:00 2001 From: serso Date: Fri, 22 Jan 2016 23:36:36 +0100 Subject: [PATCH] Changes --- .../android/calculator/AndroidCalculator.java | 2 +- .../android/calculator/Calculator.java | 10 +-- .../android/calculator/CalculatorFailure.java | 2 +- .../calculator/CalculatorFailureImpl.java | 4 +- .../android/calculator/CalculatorImpl.java | 41 +++++----- .../calculator/CalculatorMessages.java | 14 +--- .../solovyev/android/calculator/Display.java | 2 +- .../android/calculator/DisplayView.java | 2 +- ...arseException.java => ParseException.java} | 24 +++--- .../calculator/PreparedExpression.java | 14 ++-- .../calculator/ToJsclTextProcessor.java | 25 +++---- .../function/EditFunctionFragment.java | 75 ++++++++----------- .../jscl/FromJsclNumericTextProcessor.java | 11 +-- .../android/calculator/math/MathType.java | 8 +- .../calculator/math/edit/VarsFragment.java | 24 +----- .../android/calculator/model/OldFunction.java | 8 +- .../calculator/text/DummyTextProcessor.java | 11 +-- .../text/FromJsclSimplifyTextProcessor.java | 16 +--- .../calculator/text/TextProcessor.java | 9 +-- .../view/NumeralBaseConverterDialog.java | 8 +- .../calculator/CalculatorTestUtils.java | 2 +- .../calculator/history/HistoryTest.java | 4 +- .../calculator/model/NumeralBaseTest.java | 5 +- .../model/ToJsclTextProcessorTest.java | 14 ++-- 24 files changed, 124 insertions(+), 211 deletions(-) rename app/src/main/java/org/solovyev/android/calculator/{CalculatorParseException.java => ParseException.java} (80%) diff --git a/app/src/main/java/org/solovyev/android/calculator/AndroidCalculator.java b/app/src/main/java/org/solovyev/android/calculator/AndroidCalculator.java index bab9f656..1bb2b73c 100644 --- a/app/src/main/java/org/solovyev/android/calculator/AndroidCalculator.java +++ b/app/src/main/java/org/solovyev/android/calculator/AndroidCalculator.java @@ -109,7 +109,7 @@ public class AndroidCalculator implements Calculator, CalculatorEventListener, S @Nonnull @Override - public PreparedExpression prepareExpression(@Nonnull String expression) throws CalculatorParseException { + public PreparedExpression prepareExpression(@Nonnull String expression) throws ParseException { return calculator.prepareExpression(expression); } diff --git a/app/src/main/java/org/solovyev/android/calculator/Calculator.java b/app/src/main/java/org/solovyev/android/calculator/Calculator.java index 6c9ce478..77d811a9 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Calculator.java +++ b/app/src/main/java/org/solovyev/android/calculator/Calculator.java @@ -22,15 +22,13 @@ package org.solovyev.android.calculator; +import jscl.NumeralBase; +import jscl.math.Generic; import org.solovyev.android.calculator.jscl.JsclOperation; -import java.util.concurrent.Executor; - import javax.annotation.Nonnull; import javax.annotation.Nullable; - -import jscl.NumeralBase; -import jscl.math.Generic; +import java.util.concurrent.Executor; /** * User: Solovyev_S @@ -98,5 +96,5 @@ public interface Calculator extends CalculatorEventContainer { CalculatorEventData fireCalculatorEvent(@Nonnull CalculatorEventType calculatorEventType, @Nullable Object data, @Nonnull Long sequenceId); @Nonnull - PreparedExpression prepareExpression(@Nonnull String expression) throws CalculatorParseException; + PreparedExpression prepareExpression(@Nonnull String expression) throws ParseException; } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorFailure.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorFailure.java index 43b728f0..1296ef1f 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorFailure.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorFailure.java @@ -36,7 +36,7 @@ public interface CalculatorFailure { Exception getException(); @Nullable - CalculatorParseException getCalculationParseException(); + ParseException getCalculationParseException(); @Nullable CalculatorEvalException getCalculationEvalException(); diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorFailureImpl.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorFailureImpl.java index c98f0c88..9b19d9be 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorFailureImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorFailureImpl.java @@ -45,8 +45,8 @@ public class CalculatorFailureImpl implements CalculatorFailure { } @Override - public CalculatorParseException getCalculationParseException() { - return exception instanceof CalculatorParseException ? (CalculatorParseException) exception : null; + public ParseException getCalculationParseException() { + return exception instanceof ParseException ? (ParseException) exception : null; } @Override 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 8c79ed19..da4a6273 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java @@ -24,13 +24,18 @@ 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.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; import org.solovyev.android.calculator.units.CalculatorNumeralBase; import org.solovyev.common.msg.ListMessageRegistry; import org.solovyev.common.msg.Message; @@ -40,24 +45,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.MathEngine; -import jscl.NumeralBase; -import jscl.NumeralBaseException; -import jscl.math.Generic; -import jscl.math.function.IConstant; -import jscl.math.operator.Operator; -import jscl.text.ParseInterruptedException; - public class CalculatorImpl implements Calculator, CalculatorEventListener { // one minute @@ -70,7 +65,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { private final AtomicLong counter = new AtomicLong(CalculatorUtils.FIRST_ID); @Nonnull - private final TextProcessor preprocessor = ToJsclTextProcessor.getInstance(); + private final ToJsclTextProcessor preprocessor = ToJsclTextProcessor.getInstance(); @Nonnull private final Executor calculationsExecutor = Executors.newFixedThreadPool(10); @@ -101,7 +96,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { if (!Strings.isEmpty(fromString)) { try { fromString = ToJsclTextProcessor.getInstance().process(fromString).getExpression(); - } catch (CalculatorParseException e) { + } catch (ParseException e) { // ok, problems while processing occurred } } @@ -272,17 +267,17 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { } } catch (ArithmeticException e) { - handleException(sequenceId, operation, expression, mr, preparedExpression, new CalculatorParseException(expression, new CalculatorMessage(CalculatorMessages.msg_001, MessageType.error, e.getMessage()))); + handleException(sequenceId, operation, expression, mr, preparedExpression, new ParseException(expression, new CalculatorMessage(CalculatorMessages.msg_001, MessageType.error, e.getMessage()))); } catch (StackOverflowError e) { - handleException(sequenceId, operation, expression, mr, preparedExpression, new CalculatorParseException(expression, new CalculatorMessage(CalculatorMessages.msg_002, MessageType.error))); + handleException(sequenceId, operation, expression, mr, preparedExpression, new ParseException(expression, new CalculatorMessage(CalculatorMessages.msg_002, MessageType.error))); } catch (jscl.text.ParseException e) { - handleException(sequenceId, operation, expression, mr, preparedExpression, new CalculatorParseException(e)); + handleException(sequenceId, operation, expression, mr, preparedExpression, new ParseException(e)); } catch (ParseInterruptedException e) { // do nothing - we ourselves interrupt the calculations fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_cancelled, null); - } catch (CalculatorParseException e) { + } catch (ParseException e) { handleException(sequenceId, operation, expression, mr, preparedExpression, e); } } @@ -298,7 +293,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { @Nonnull @Override - public PreparedExpression prepareExpression(@Nonnull String expression) throws CalculatorParseException { + public PreparedExpression prepareExpression(@Nonnull String expression) throws ParseException { return preprocessor.process(expression); } @@ -314,7 +309,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { @Nonnull String expression, @Nullable MessageRegistry mr, @Nullable PreparedExpression preparedExpression, - @Nonnull CalculatorParseException parseException) { + @Nonnull ParseException parseException) { if (operation == JsclOperation.numeric && preparedExpression != null diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorMessages.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorMessages.java index 09e44754..b5169a46 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorMessages.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorMessages.java @@ -24,22 +24,14 @@ package org.solovyev.android.calculator; import org.solovyev.common.msg.MessageType; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.Locale; import java.util.MissingResourceException; import java.util.ResourceBundle; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; +import static org.solovyev.common.msg.MessageType.*; -import static org.solovyev.common.msg.MessageType.error; -import static org.solovyev.common.msg.MessageType.info; -import static org.solovyev.common.msg.MessageType.warning; - -/** - * User: serso - * Date: 9/20/12 - * Time: 8:10 PM - */ public final class CalculatorMessages { diff --git a/app/src/main/java/org/solovyev/android/calculator/Display.java b/app/src/main/java/org/solovyev/android/calculator/Display.java index a9f1318f..8f773815 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Display.java +++ b/app/src/main/java/org/solovyev/android/calculator/Display.java @@ -130,7 +130,7 @@ public class Display implements CalculatorEventListener { if (calculatorEvalException != null) { errorMessage = CalculatorMessages.getBundle().getString(CalculatorMessages.syntax_error); } else { - final CalculatorParseException calculationParseException = data.getCalculationParseException(); + final ParseException calculationParseException = data.getCalculationParseException(); if (calculationParseException != null) { errorMessage = calculationParseException.getLocalizedMessage(); } else { diff --git a/app/src/main/java/org/solovyev/android/calculator/DisplayView.java b/app/src/main/java/org/solovyev/android/calculator/DisplayView.java index 2f89de3c..27bf758b 100644 --- a/app/src/main/java/org/solovyev/android/calculator/DisplayView.java +++ b/app/src/main/java/org/solovyev/android/calculator/DisplayView.java @@ -106,7 +106,7 @@ public class DisplayView extends AutoResizeTextView { } try { return textHighlighter.process(text).getCharSequence(); - } catch (CalculatorParseException e) { + } catch (ParseException e) { return text; } } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorParseException.java b/app/src/main/java/org/solovyev/android/calculator/ParseException.java similarity index 80% rename from app/src/main/java/org/solovyev/android/calculator/CalculatorParseException.java rename to app/src/main/java/org/solovyev/android/calculator/ParseException.java index eb0b6821..17e43d9a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorParseException.java +++ b/app/src/main/java/org/solovyev/android/calculator/ParseException.java @@ -25,18 +25,12 @@ package org.solovyev.android.calculator; import org.solovyev.common.msg.Message; import org.solovyev.common.msg.MessageLevel; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.List; import java.util.Locale; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * User: serso - * Date: 10/6/11 - * Time: 9:25 PM - */ -public class CalculatorParseException extends Exception implements Message { +public class ParseException extends RuntimeException implements Message { @Nonnull private final Message message; @@ -47,22 +41,22 @@ public class CalculatorParseException extends Exception implements Message { @Nullable private final Integer position; - public CalculatorParseException(@Nonnull jscl.text.ParseException jsclParseException) { + public ParseException(@Nonnull jscl.text.ParseException jsclParseException) { this.message = jsclParseException; this.expression = jsclParseException.getExpression(); this.position = jsclParseException.getPosition(); } - public CalculatorParseException(@Nullable Integer position, - @Nonnull String expression, - @Nonnull Message message) { + public ParseException(@Nullable Integer position, + @Nonnull String expression, + @Nonnull Message message) { this.message = message; this.expression = expression; this.position = position; } - public CalculatorParseException(@Nonnull String expression, - @Nonnull Message message) { + public ParseException(@Nonnull String expression, + @Nonnull Message message) { this(null, expression, message); } diff --git a/app/src/main/java/org/solovyev/android/calculator/PreparedExpression.java b/app/src/main/java/org/solovyev/android/calculator/PreparedExpression.java index a145e2d1..b81f5a41 100644 --- a/app/src/main/java/org/solovyev/android/calculator/PreparedExpression.java +++ b/app/src/main/java/org/solovyev/android/calculator/PreparedExpression.java @@ -22,17 +22,12 @@ package org.solovyev.android.calculator; -import java.util.List; - -import javax.annotation.Nonnull; - +import android.support.annotation.NonNull; import jscl.math.function.IConstant; -/** - * User: serso - * Date: 10/18/11 - * Time: 10:07 PM - */ +import javax.annotation.Nonnull; +import java.util.List; + public class PreparedExpression implements CharSequence { @Nonnull @@ -75,6 +70,7 @@ public class PreparedExpression implements CharSequence { return expression.subSequence(i, i1); } + @NonNull @Override public String toString() { return this.expression; diff --git a/app/src/main/java/org/solovyev/android/calculator/ToJsclTextProcessor.java b/app/src/main/java/org/solovyev/android/calculator/ToJsclTextProcessor.java index 68772526..85318efa 100644 --- a/app/src/main/java/org/solovyev/android/calculator/ToJsclTextProcessor.java +++ b/app/src/main/java/org/solovyev/android/calculator/ToJsclTextProcessor.java @@ -22,43 +22,40 @@ package org.solovyev.android.calculator; +import jscl.math.function.Function; +import jscl.math.function.IConstant; import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.text.TextProcessor; import org.solovyev.common.collections.Collections; import org.solovyev.common.msg.MessageType; import org.solovyev.common.search.StartsWithFinder; +import javax.annotation.Nonnull; import java.util.ArrayList; import java.util.List; -import javax.annotation.Nonnull; - -import jscl.math.function.Function; -import jscl.math.function.IConstant; - public class ToJsclTextProcessor implements TextProcessor { @Nonnull private static final Integer MAX_DEPTH = 20; @Nonnull - private static final TextProcessor instance = new ToJsclTextProcessor(); + private static final ToJsclTextProcessor instance = new ToJsclTextProcessor(); private ToJsclTextProcessor() { } - @Nonnull - public static TextProcessor getInstance() { + public static ToJsclTextProcessor getInstance() { return instance; } - private static PreparedExpression processWithDepth(@Nonnull String s, int depth, @Nonnull List undefinedVars) throws CalculatorParseException { + private static PreparedExpression processWithDepth(@Nonnull String s, int depth, @Nonnull List undefinedVars) throws ParseException { return replaceVariables(processExpression(s).toString(), depth, undefinedVars); } @Nonnull - private static StringBuilder processExpression(@Nonnull String s) throws CalculatorParseException { + private static StringBuilder processExpression(@Nonnull String s) throws ParseException { final StartsWithFinder startsWithFinder = StartsWithFinder.newInstance(s); final StringBuilder result = new StringBuilder(); @@ -91,7 +88,7 @@ public class ToJsclTextProcessor implements TextProcessor 0) { - throw new CalculatorParseException(i, s, new CalculatorMessage(CalculatorMessages.msg_005, MessageType.error, mathTypeBefore.match)); + throw new ParseException(i, s, new CalculatorMessage(CalculatorMessages.msg_005, MessageType.error, mathTypeBefore.match)); } } @@ -101,9 +98,9 @@ public class ToJsclTextProcessor implements TextProcessor undefinedVars) throws CalculatorParseException { + private static PreparedExpression replaceVariables(@Nonnull final String s, int depth, @Nonnull List undefinedVars) throws ParseException { if (depth >= MAX_DEPTH) { - throw new CalculatorParseException(s, new CalculatorMessage(CalculatorMessages.msg_006, MessageType.error)); + throw new ParseException(s, new CalculatorMessage(CalculatorMessages.msg_006, MessageType.error)); } else { depth++; } @@ -164,7 +161,7 @@ public class ToJsclTextProcessor implements TextProcessor()); } } diff --git a/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java b/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java index 0c067dd4..9fe4d587 100644 --- a/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java @@ -36,49 +36,26 @@ import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.text.Editable; import android.text.TextUtils; -import android.view.ContextMenu; -import android.view.KeyEvent; -import android.view.LayoutInflater; -import android.view.Menu; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; -import android.view.ViewParent; +import android.view.*; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; - -import org.solovyev.android.Check; -import org.solovyev.android.calculator.App; -import org.solovyev.android.calculator.AppComponent; -import org.solovyev.android.calculator.BaseDialogFragment; -import org.solovyev.android.calculator.Calculator; -import org.solovyev.android.calculator.CalculatorEventType; -import org.solovyev.android.calculator.FunctionsRegistry; -import org.solovyev.android.calculator.KeyboardUi; -import org.solovyev.android.calculator.KeyboardWindow; -import org.solovyev.android.calculator.Locator; -import org.solovyev.android.calculator.R; -import org.solovyev.android.calculator.math.edit.CalculatorFunctionsActivity; -import org.solovyev.android.calculator.math.edit.FunctionsFragment; -import org.solovyev.android.calculator.math.edit.VarEditorSaver; -import org.solovyev.common.math.MathRegistry; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import javax.inject.Inject; - import butterknife.Bind; import butterknife.ButterKnife; import jscl.math.function.CustomFunction; import jscl.math.function.Function; import jscl.math.function.IConstant; +import org.solovyev.android.Check; +import org.solovyev.android.calculator.*; +import org.solovyev.android.calculator.math.edit.CalculatorFunctionsActivity; +import org.solovyev.android.calculator.math.edit.FunctionsFragment; +import org.solovyev.android.calculator.math.edit.VarEditorSaver; +import org.solovyev.common.math.MathRegistry; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.inject.Inject; +import java.util.*; import static org.solovyev.android.calculator.function.CppFunction.NO_ID; @@ -305,12 +282,18 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC } private void applyData() { - final CppFunction newFunction = CppFunction.builder(nameView.getText().toString(), bodyView.getText().toString()) - .withId(function == null ? NO_ID : function.id) - .withParameters(collectParameters()) - .withDescription(descriptionView.getText().toString()).build(); - final Function oldFunction = (function == null || function.id == NO_ID) ? null : registry.getById(function.id); - registry.add(newFunction.toCustomFunctionBuilder(), oldFunction); + try { + final String body = calculator.prepareExpression(bodyView.getText().toString()).getExpression(); + + final CppFunction newFunction = CppFunction.builder(nameView.getText().toString(), body) + .withId(function == null ? NO_ID : function.id) + .withParameters(collectParameters()) + .withDescription(descriptionView.getText().toString()).build(); + final Function oldFunction = (function == null || function.id == NO_ID) ? null : registry.getById(function.id); + registry.add(newFunction.toCustomFunctionBuilder(), oldFunction); + } catch (ParseException e) { + setError(bodyLabel, e.getLocalizedMessage()); + } } private boolean validate() { @@ -345,8 +328,14 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC setError(bodyLabel, getString(R.string.function_is_empty)); return false; } - clearError(bodyLabel); - return true; + try { + calculator.prepareExpression(body); + clearError(bodyLabel); + return true; + } catch (ParseException e) { + setError(bodyLabel, e.getLocalizedMessage()); + return false; + } } private boolean validateParameters() { diff --git a/app/src/main/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessor.java b/app/src/main/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessor.java index d0229f55..d3e318fb 100644 --- a/app/src/main/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessor.java +++ b/app/src/main/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessor.java @@ -22,25 +22,18 @@ package org.solovyev.android.calculator.jscl; -import org.solovyev.android.calculator.CalculatorParseException; +import jscl.math.Generic; import org.solovyev.android.calculator.text.TextProcessor; import javax.annotation.Nonnull; -import jscl.math.Generic; - -/** - * User: serso - * Date: 10/6/11 - * Time: 9:48 PM - */ class FromJsclNumericTextProcessor implements TextProcessor { public static final FromJsclNumericTextProcessor instance = new FromJsclNumericTextProcessor(); @Nonnull @Override - public String process(@Nonnull Generic numeric) throws CalculatorParseException { + public String process(@Nonnull Generic numeric) { return numeric.toString().replace("*", ""); } } diff --git a/app/src/main/java/org/solovyev/android/calculator/math/MathType.java b/app/src/main/java/org/solovyev/android/calculator/math/MathType.java index 99c97530..98177f9c 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/MathType.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/MathType.java @@ -25,7 +25,7 @@ package org.solovyev.android.calculator.math; import jscl.JsclMathEngine; import jscl.NumeralBase; import jscl.math.function.Constants; -import org.solovyev.android.calculator.CalculatorParseException; +import org.solovyev.android.calculator.ParseException; import org.solovyev.android.calculator.Locator; import org.solovyev.common.JPredicate; import org.solovyev.common.collections.Collections; @@ -65,7 +65,7 @@ public enum MathType { grouping_separator(250, false, false, MathGroupType.number, "'", " ") { @Override - public int processToJscl(@Nonnull StringBuilder result, int i, @Nonnull String match) throws CalculatorParseException { + public int processToJscl(@Nonnull StringBuilder result, int i, @Nonnull String match) throws ParseException { return i; } }, @@ -347,7 +347,7 @@ public enum MathType { return needMultiplicationSignBefore && mathTypeBefore.isNeedMultiplicationSignAfter(); } - public int processToJscl(@Nonnull StringBuilder result, int i, @Nonnull String match) throws CalculatorParseException { + public int processToJscl(@Nonnull StringBuilder result, int i, @Nonnull String match) throws ParseException { final String substitute = getSubstituteToJscl(match); result.append(substitute == null ? match : substitute); return returnI(i, match); @@ -400,7 +400,7 @@ public enum MathType { this.match = match; } - public int processToJscl(@Nonnull StringBuilder result, int i) throws CalculatorParseException { + public int processToJscl(@Nonnull StringBuilder result, int i) throws ParseException { return type.processToJscl(result, i, match); } } diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/VarsFragment.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/VarsFragment.java index 28ece235..3ac0d2d8 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/VarsFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/VarsFragment.java @@ -28,32 +28,18 @@ import android.support.v4.app.FragmentActivity; import android.view.ContextMenu; import android.view.MenuItem; import android.view.View; - import com.melnykov.fab.FloatingActionButton; - -import org.solovyev.android.calculator.CalculatorEventData; -import org.solovyev.android.calculator.CalculatorEventListener; -import org.solovyev.android.calculator.CalculatorEventType; -import org.solovyev.android.calculator.CalculatorFragmentType; -import org.solovyev.android.calculator.CalculatorParseException; -import org.solovyev.android.calculator.Change; -import org.solovyev.android.calculator.EntitiesRegistry; -import org.solovyev.android.calculator.Locator; -import org.solovyev.android.calculator.PreparedExpression; -import org.solovyev.android.calculator.R; -import org.solovyev.android.calculator.ToJsclTextProcessor; +import jscl.math.function.IConstant; +import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.math.MathType; import org.solovyev.common.JPredicate; import org.solovyev.common.collections.Collections; import org.solovyev.common.text.Strings; -import java.util.ArrayList; -import java.util.List; - import javax.annotation.Nonnull; import javax.annotation.Nullable; - -import jscl.math.function.IConstant; +import java.util.ArrayList; +import java.util.List; public class VarsFragment extends BaseEntitiesFragmentimplements CalculatorEventListener { @@ -72,8 +58,6 @@ public class VarsFragment extends BaseEntitiesFragmentimplements Calc return constants.isEmpty(); } catch (RuntimeException e) { return true; - } catch (CalculatorParseException e) { - return true; } } diff --git a/app/src/main/java/org/solovyev/android/calculator/model/OldFunction.java b/app/src/main/java/org/solovyev/android/calculator/model/OldFunction.java index 5f2380dc..6a8e1376 100644 --- a/app/src/main/java/org/solovyev/android/calculator/model/OldFunction.java +++ b/app/src/main/java/org/solovyev/android/calculator/model/OldFunction.java @@ -27,7 +27,7 @@ import org.simpleframework.xml.Element; import org.simpleframework.xml.ElementList; import org.simpleframework.xml.Root; import org.simpleframework.xml.Transient; -import org.solovyev.android.calculator.CalculatorParseException; +import org.solovyev.android.calculator.ParseException; import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.PersistedEntity; import org.solovyev.common.math.MathEntity; @@ -252,7 +252,7 @@ public class OldFunction implements IFunction, PersistedEntity, Serializable { result.name = name; try { result.content = Locator.getInstance().getCalculator().prepareExpression(value).toString(); - } catch (CalculatorParseException e) { + } catch (ParseException e) { throw new CreationException(e); } result.system = system; @@ -265,9 +265,9 @@ public class OldFunction implements IFunction, PersistedEntity, Serializable { public static class CreationException extends RuntimeException implements Message { @Nonnull - private final CalculatorParseException message; + private final ParseException message; - public CreationException(@Nonnull CalculatorParseException cause) { + public CreationException(@Nonnull ParseException cause) { super(cause); message = cause; } diff --git a/app/src/main/java/org/solovyev/android/calculator/text/DummyTextProcessor.java b/app/src/main/java/org/solovyev/android/calculator/text/DummyTextProcessor.java index 4cf72fdb..833fec3a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/text/DummyTextProcessor.java +++ b/app/src/main/java/org/solovyev/android/calculator/text/DummyTextProcessor.java @@ -22,24 +22,17 @@ package org.solovyev.android.calculator.text; -import org.solovyev.android.calculator.CalculatorParseException; +import jscl.math.Generic; import javax.annotation.Nonnull; -import jscl.math.Generic; - -/** - * User: serso - * Date: 10/18/11 - * Time: 10:39 PM - */ public enum DummyTextProcessor implements TextProcessor { instance; @Nonnull @Override - public String process(@Nonnull Generic s) throws CalculatorParseException { + public String process(@Nonnull Generic s) { return s.toString(); } } diff --git a/app/src/main/java/org/solovyev/android/calculator/text/FromJsclSimplifyTextProcessor.java b/app/src/main/java/org/solovyev/android/calculator/text/FromJsclSimplifyTextProcessor.java index 336c1242..874e706b 100644 --- a/app/src/main/java/org/solovyev/android/calculator/text/FromJsclSimplifyTextProcessor.java +++ b/app/src/main/java/org/solovyev/android/calculator/text/FromJsclSimplifyTextProcessor.java @@ -22,23 +22,15 @@ package org.solovyev.android.calculator.text; -import org.solovyev.android.calculator.CalculatorParseException; +import jscl.math.Generic; import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.math.MathType; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.Arrays; import java.util.List; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import jscl.math.Generic; - -/** - * User: serso - * Date: 10/20/11 - * Time: 2:59 PM - */ public class FromJsclSimplifyTextProcessor implements TextProcessor { public static final FromJsclSimplifyTextProcessor instance = new FromJsclSimplifyTextProcessor(); @@ -49,7 +41,7 @@ public class FromJsclSimplifyTextProcessor implements TextProcessor { @Nonnull - TO process(@Nonnull FROM from) throws CalculatorParseException; + TO process(@Nonnull FROM from); } diff --git a/app/src/main/java/org/solovyev/android/calculator/view/NumeralBaseConverterDialog.java b/app/src/main/java/org/solovyev/android/calculator/view/NumeralBaseConverterDialog.java index 59f10d5d..b6cc45cc 100644 --- a/app/src/main/java/org/solovyev/android/calculator/view/NumeralBaseConverterDialog.java +++ b/app/src/main/java/org/solovyev/android/calculator/view/NumeralBaseConverterDialog.java @@ -26,9 +26,8 @@ import android.app.AlertDialog; import android.content.Context; import android.view.View; import android.view.WindowManager; - -import org.solovyev.android.calculator.CalculatorParseException; import org.solovyev.android.calculator.Locator; +import org.solovyev.android.calculator.ParseException; import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.ToJsclTextProcessor; import org.solovyev.android.calculator.units.CalculatorNumeralBase; @@ -37,10 +36,9 @@ import org.solovyev.common.text.Strings; import org.solovyev.common.units.Unit; import org.solovyev.common.units.UnitImpl; -import java.util.Arrays; - import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.util.Arrays; /** * User: serso @@ -66,7 +64,7 @@ public class NumeralBaseConverterDialog { try { value = ToJsclTextProcessor.getInstance().process(value).getExpression(); b.setFromValue(UnitImpl.newInstance(value, CalculatorNumeralBase.valueOf(Locator.getInstance().getEngine().getMathEngine().getNumeralBase()))); - } catch (CalculatorParseException e) { + } catch (ParseException e) { b.setFromValue(UnitImpl.newInstance(value, CalculatorNumeralBase.valueOf(Locator.getInstance().getEngine().getMathEngine().getNumeralBase()))); } } else { diff --git a/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java b/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java index 83d4e5af..4f54dd70 100644 --- a/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java +++ b/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java @@ -95,7 +95,7 @@ public class CalculatorTestUtils { final JsclMathEngine jsclEngine = JsclMathEngine.getInstance(); final VarsRegistry varsRegistry = new VarsRegistry(jsclEngine.getConstantsRegistry(), entityDao); - final FunctionsRegistry functionsRegistry = new FunctionsRegistry(jsclEngine.getFunctionsRegistry()); + final FunctionsRegistry functionsRegistry = new FunctionsRegistry(jsclEngine); final OperatorsRegistry operatorsRegistry = new OperatorsRegistry(jsclEngine.getOperatorsRegistry()); final PostfixFunctionsRegistry postfixFunctionsRegistry = new PostfixFunctionsRegistry(jsclEngine.getPostfixFunctionsRegistry()); 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 7105749b..35ffb3bc 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 @@ -132,7 +132,7 @@ public class HistoryTest { } private void addState(@Nonnull String text) { - history.addRecent(HistoryState.builder(EditorState.create(text, 3), DisplayState.empty())); + history.addRecent(HistoryState.builder(EditorState.create(text, 3), DisplayState.empty()).build()); } private static final String oldXml1 = "\n" + @@ -281,7 +281,7 @@ public class HistoryTest { @Test public void testShouldLoadStates() throws Exception { - final List states = Json.load(new File(HistoryTest.class.getResource("recent-history.json").getFile())); + final List states = Json.load(new File(HistoryTest.class.getResource("recent-history.json").getFile()), HistoryState.JSON_CREATOR); assertEquals(8, states.size()); HistoryState state = states.get(0); 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 c5ff3af5..440345a2 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 @@ -27,7 +27,7 @@ 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.ParseException; import org.solovyev.android.calculator.CalculatorTestUtils; import org.solovyev.android.calculator.Locator; import org.solovyev.common.Converter; @@ -42,7 +42,6 @@ import au.com.bytecode.opencsv.CSVReader; import jscl.JsclMathEngine; import jscl.MathEngine; import jscl.math.Expression; -import jscl.text.ParseException; import jscl.util.ExpressionGeneratorWithInput; /** @@ -58,7 +57,7 @@ public class NumeralBaseTest extends AbstractCalculatorTest { Locator.getInstance().getEngine().getMathEngine().setPrecision(3); } - public static void testExpression(@Nonnull String[] line, @Nonnull Converter converter) throws ParseException, CalculatorEvalException, CalculatorParseException { + public static void testExpression(@Nonnull String[] line, @Nonnull Converter converter) throws jscl.text.ParseException, CalculatorEvalException, ParseException { final String dec = line[0].toUpperCase(); final String hex = "0x:" + line[1].toUpperCase(); final String bin = "0b:" + line[2].toUpperCase(); diff --git a/app/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java b/app/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java index fdc3b487..e29c804e 100644 --- a/app/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java @@ -26,7 +26,7 @@ import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.solovyev.android.calculator.AbstractCalculatorTest; -import org.solovyev.android.calculator.CalculatorParseException; +import org.solovyev.android.calculator.ParseException; import org.solovyev.android.calculator.CalculatorTestUtils; import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.PreparedExpression; @@ -49,7 +49,7 @@ public class ToJsclTextProcessorTest extends AbstractCalculatorTest { } @Test - public void testSpecialCases() throws CalculatorParseException { + public void testSpecialCases() throws ParseException { final TextProcessor preprocessor = ToJsclTextProcessor.getInstance(); Assert.assertEquals("3^E10", preprocessor.process("3^E10").toString()); } @@ -108,30 +108,30 @@ public class ToJsclTextProcessorTest extends AbstractCalculatorTest { try { preprocessor.process("ln()"); Assert.fail(); - } catch (CalculatorParseException e) { + } catch (ParseException e) { } try { preprocessor.process("ln()ln()"); Assert.fail(); - } catch (CalculatorParseException e) { + } catch (ParseException e) { } try { preprocessor.process("eln()eln()ln()ln()ln()e"); Assert.fail(); - } catch (CalculatorParseException e) { + } catch (ParseException e) { } try { preprocessor.process("ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln()))))))))))))))"); Assert.fail(); - } catch (CalculatorParseException e) { + } catch (ParseException e) { } try { preprocessor.process("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(); - } catch (CalculatorParseException e) { + } catch (ParseException e) { } }