This commit is contained in:
serso 2016-01-22 23:36:36 +01:00
parent bbaedc71c9
commit 81510d18ea
24 changed files with 124 additions and 211 deletions

View File

@ -109,7 +109,7 @@ public class AndroidCalculator implements Calculator, CalculatorEventListener, S
@Nonnull @Nonnull
@Override @Override
public PreparedExpression prepareExpression(@Nonnull String expression) throws CalculatorParseException { public PreparedExpression prepareExpression(@Nonnull String expression) throws ParseException {
return calculator.prepareExpression(expression); return calculator.prepareExpression(expression);
} }

View File

@ -22,15 +22,13 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import jscl.NumeralBase;
import jscl.math.Generic;
import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.jscl.JsclOperation;
import java.util.concurrent.Executor;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.concurrent.Executor;
import jscl.NumeralBase;
import jscl.math.Generic;
/** /**
* User: Solovyev_S * User: Solovyev_S
@ -98,5 +96,5 @@ public interface Calculator extends CalculatorEventContainer {
CalculatorEventData fireCalculatorEvent(@Nonnull CalculatorEventType calculatorEventType, @Nullable Object data, @Nonnull Long sequenceId); CalculatorEventData fireCalculatorEvent(@Nonnull CalculatorEventType calculatorEventType, @Nullable Object data, @Nonnull Long sequenceId);
@Nonnull @Nonnull
PreparedExpression prepareExpression(@Nonnull String expression) throws CalculatorParseException; PreparedExpression prepareExpression(@Nonnull String expression) throws ParseException;
} }

View File

@ -36,7 +36,7 @@ public interface CalculatorFailure {
Exception getException(); Exception getException();
@Nullable @Nullable
CalculatorParseException getCalculationParseException(); ParseException getCalculationParseException();
@Nullable @Nullable
CalculatorEvalException getCalculationEvalException(); CalculatorEvalException getCalculationEvalException();

View File

@ -45,8 +45,8 @@ public class CalculatorFailureImpl implements CalculatorFailure {
} }
@Override @Override
public CalculatorParseException getCalculationParseException() { public ParseException getCalculationParseException() {
return exception instanceof CalculatorParseException ? (CalculatorParseException) exception : null; return exception instanceof ParseException ? (ParseException) exception : null;
} }
@Override @Override

View File

@ -24,13 +24,18 @@ package org.solovyev.android.calculator;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe; 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.jscl.JsclOperation;
import org.solovyev.android.calculator.model.Var; import org.solovyev.android.calculator.model.Var;
import org.solovyev.android.calculator.text.TextProcessor;
import org.solovyev.android.calculator.units.CalculatorNumeralBase; import org.solovyev.android.calculator.units.CalculatorNumeralBase;
import org.solovyev.common.msg.ListMessageRegistry; import org.solovyev.common.msg.ListMessageRegistry;
import org.solovyev.common.msg.Message; 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.ConversionException;
import org.solovyev.common.units.Conversions; import org.solovyev.common.units.Conversions;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong; 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 { public class CalculatorImpl implements Calculator, CalculatorEventListener {
// one minute // one minute
@ -70,7 +65,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
private final AtomicLong counter = new AtomicLong(CalculatorUtils.FIRST_ID); private final AtomicLong counter = new AtomicLong(CalculatorUtils.FIRST_ID);
@Nonnull @Nonnull
private final TextProcessor<PreparedExpression, String> preprocessor = ToJsclTextProcessor.getInstance(); private final ToJsclTextProcessor preprocessor = ToJsclTextProcessor.getInstance();
@Nonnull @Nonnull
private final Executor calculationsExecutor = Executors.newFixedThreadPool(10); private final Executor calculationsExecutor = Executors.newFixedThreadPool(10);
@ -101,7 +96,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
if (!Strings.isEmpty(fromString)) { if (!Strings.isEmpty(fromString)) {
try { try {
fromString = ToJsclTextProcessor.getInstance().process(fromString).getExpression(); fromString = ToJsclTextProcessor.getInstance().process(fromString).getExpression();
} catch (CalculatorParseException e) { } catch (ParseException e) {
// ok, problems while processing occurred // ok, problems while processing occurred
} }
} }
@ -272,17 +267,17 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
} }
} catch (ArithmeticException e) { } 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) { } 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) { } 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) { } catch (ParseInterruptedException e) {
// do nothing - we ourselves interrupt the calculations // do nothing - we ourselves interrupt the calculations
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_cancelled, null); fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_cancelled, null);
} catch (CalculatorParseException e) { } catch (ParseException e) {
handleException(sequenceId, operation, expression, mr, preparedExpression, e); handleException(sequenceId, operation, expression, mr, preparedExpression, e);
} }
} }
@ -298,7 +293,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
@Nonnull @Nonnull
@Override @Override
public PreparedExpression prepareExpression(@Nonnull String expression) throws CalculatorParseException { public PreparedExpression prepareExpression(@Nonnull String expression) throws ParseException {
return preprocessor.process(expression); return preprocessor.process(expression);
} }
@ -314,7 +309,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
@Nonnull String expression, @Nonnull String expression,
@Nullable MessageRegistry mr, @Nullable MessageRegistry mr,
@Nullable PreparedExpression preparedExpression, @Nullable PreparedExpression preparedExpression,
@Nonnull CalculatorParseException parseException) { @Nonnull ParseException parseException) {
if (operation == JsclOperation.numeric if (operation == JsclOperation.numeric
&& preparedExpression != null && preparedExpression != null

View File

@ -24,22 +24,14 @@ package org.solovyev.android.calculator;
import org.solovyev.common.msg.MessageType; import org.solovyev.common.msg.MessageType;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Locale; import java.util.Locale;
import java.util.MissingResourceException; import java.util.MissingResourceException;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import javax.annotation.Nonnull; import static org.solovyev.common.msg.MessageType.*;
import javax.annotation.Nullable;
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 { public final class CalculatorMessages {

View File

@ -130,7 +130,7 @@ public class Display implements CalculatorEventListener {
if (calculatorEvalException != null) { if (calculatorEvalException != null) {
errorMessage = CalculatorMessages.getBundle().getString(CalculatorMessages.syntax_error); errorMessage = CalculatorMessages.getBundle().getString(CalculatorMessages.syntax_error);
} else { } else {
final CalculatorParseException calculationParseException = data.getCalculationParseException(); final ParseException calculationParseException = data.getCalculationParseException();
if (calculationParseException != null) { if (calculationParseException != null) {
errorMessage = calculationParseException.getLocalizedMessage(); errorMessage = calculationParseException.getLocalizedMessage();
} else { } else {

View File

@ -106,7 +106,7 @@ public class DisplayView extends AutoResizeTextView {
} }
try { try {
return textHighlighter.process(text).getCharSequence(); return textHighlighter.process(text).getCharSequence();
} catch (CalculatorParseException e) { } catch (ParseException e) {
return text; return text;
} }
} }

View File

@ -25,18 +25,12 @@ package org.solovyev.android.calculator;
import org.solovyev.common.msg.Message; import org.solovyev.common.msg.Message;
import org.solovyev.common.msg.MessageLevel; import org.solovyev.common.msg.MessageLevel;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import javax.annotation.Nonnull; public class ParseException extends RuntimeException implements Message {
import javax.annotation.Nullable;
/**
* User: serso
* Date: 10/6/11
* Time: 9:25 PM
*/
public class CalculatorParseException extends Exception implements Message {
@Nonnull @Nonnull
private final Message message; private final Message message;
@ -47,13 +41,13 @@ public class CalculatorParseException extends Exception implements Message {
@Nullable @Nullable
private final Integer position; private final Integer position;
public CalculatorParseException(@Nonnull jscl.text.ParseException jsclParseException) { public ParseException(@Nonnull jscl.text.ParseException jsclParseException) {
this.message = jsclParseException; this.message = jsclParseException;
this.expression = jsclParseException.getExpression(); this.expression = jsclParseException.getExpression();
this.position = jsclParseException.getPosition(); this.position = jsclParseException.getPosition();
} }
public CalculatorParseException(@Nullable Integer position, public ParseException(@Nullable Integer position,
@Nonnull String expression, @Nonnull String expression,
@Nonnull Message message) { @Nonnull Message message) {
this.message = message; this.message = message;
@ -61,7 +55,7 @@ public class CalculatorParseException extends Exception implements Message {
this.position = position; this.position = position;
} }
public CalculatorParseException(@Nonnull String expression, public ParseException(@Nonnull String expression,
@Nonnull Message message) { @Nonnull Message message) {
this(null, expression, message); this(null, expression, message);
} }

View File

@ -22,17 +22,12 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import java.util.List; import android.support.annotation.NonNull;
import javax.annotation.Nonnull;
import jscl.math.function.IConstant; import jscl.math.function.IConstant;
/** import javax.annotation.Nonnull;
* User: serso import java.util.List;
* Date: 10/18/11
* Time: 10:07 PM
*/
public class PreparedExpression implements CharSequence { public class PreparedExpression implements CharSequence {
@Nonnull @Nonnull
@ -75,6 +70,7 @@ public class PreparedExpression implements CharSequence {
return expression.subSequence(i, i1); return expression.subSequence(i, i1);
} }
@NonNull
@Override @Override
public String toString() { public String toString() {
return this.expression; return this.expression;

View File

@ -22,43 +22,40 @@
package org.solovyev.android.calculator; 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.math.MathType;
import org.solovyev.android.calculator.text.TextProcessor; import org.solovyev.android.calculator.text.TextProcessor;
import org.solovyev.common.collections.Collections; import org.solovyev.common.collections.Collections;
import org.solovyev.common.msg.MessageType; import org.solovyev.common.msg.MessageType;
import org.solovyev.common.search.StartsWithFinder; import org.solovyev.common.search.StartsWithFinder;
import javax.annotation.Nonnull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.annotation.Nonnull;
import jscl.math.function.Function;
import jscl.math.function.IConstant;
public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, String> { public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, String> {
@Nonnull @Nonnull
private static final Integer MAX_DEPTH = 20; private static final Integer MAX_DEPTH = 20;
@Nonnull @Nonnull
private static final TextProcessor<PreparedExpression, String> instance = new ToJsclTextProcessor(); private static final ToJsclTextProcessor instance = new ToJsclTextProcessor();
private ToJsclTextProcessor() { private ToJsclTextProcessor() {
} }
@Nonnull @Nonnull
public static TextProcessor<PreparedExpression, String> getInstance() { public static ToJsclTextProcessor getInstance() {
return instance; return instance;
} }
private static PreparedExpression processWithDepth(@Nonnull String s, int depth, @Nonnull List<IConstant> undefinedVars) throws CalculatorParseException { private static PreparedExpression processWithDepth(@Nonnull String s, int depth, @Nonnull List<IConstant> undefinedVars) throws ParseException {
return replaceVariables(processExpression(s).toString(), depth, undefinedVars); return replaceVariables(processExpression(s).toString(), depth, undefinedVars);
} }
@Nonnull @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 StartsWithFinder startsWithFinder = StartsWithFinder.newInstance(s);
final StringBuilder result = new StringBuilder(); final StringBuilder result = new StringBuilder();
@ -91,7 +88,7 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
final String functionName = mathTypeBefore.match; final String functionName = mathTypeBefore.match;
final Function function = Locator.getInstance().getEngine().getFunctionsRegistry().get(functionName); final Function function = Locator.getInstance().getEngine().getFunctionsRegistry().get(functionName);
if (function == null || function.getMinParameters() > 0) { if (function == null || function.getMinParameters() > 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<PreparedExpression, St
} }
@Nonnull @Nonnull
private static PreparedExpression replaceVariables(@Nonnull final String s, int depth, @Nonnull List<IConstant> undefinedVars) throws CalculatorParseException { private static PreparedExpression replaceVariables(@Nonnull final String s, int depth, @Nonnull List<IConstant> undefinedVars) throws ParseException {
if (depth >= MAX_DEPTH) { 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 { } else {
depth++; depth++;
} }
@ -164,7 +161,7 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
@Override @Override
@Nonnull @Nonnull
public PreparedExpression process(@Nonnull String s) throws CalculatorParseException { public PreparedExpression process(@Nonnull String s) throws ParseException {
return processWithDepth(s, 0, new ArrayList<IConstant>()); return processWithDepth(s, 0, new ArrayList<IConstant>());
} }
} }

View File

@ -36,49 +36,26 @@ import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.text.Editable; import android.text.Editable;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.ContextMenu; import android.view.*;
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.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; 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.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import jscl.math.function.CustomFunction; import jscl.math.function.CustomFunction;
import jscl.math.function.Function; import jscl.math.function.Function;
import jscl.math.function.IConstant; 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; 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() { private void applyData() {
final CppFunction newFunction = CppFunction.builder(nameView.getText().toString(), bodyView.getText().toString()) 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) .withId(function == null ? NO_ID : function.id)
.withParameters(collectParameters()) .withParameters(collectParameters())
.withDescription(descriptionView.getText().toString()).build(); .withDescription(descriptionView.getText().toString()).build();
final Function oldFunction = (function == null || function.id == NO_ID) ? null : registry.getById(function.id); final Function oldFunction = (function == null || function.id == NO_ID) ? null : registry.getById(function.id);
registry.add(newFunction.toCustomFunctionBuilder(), oldFunction); registry.add(newFunction.toCustomFunctionBuilder(), oldFunction);
} catch (ParseException e) {
setError(bodyLabel, e.getLocalizedMessage());
}
} }
private boolean validate() { private boolean validate() {
@ -345,8 +328,14 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC
setError(bodyLabel, getString(R.string.function_is_empty)); setError(bodyLabel, getString(R.string.function_is_empty));
return false; return false;
} }
try {
calculator.prepareExpression(body);
clearError(bodyLabel); clearError(bodyLabel);
return true; return true;
} catch (ParseException e) {
setError(bodyLabel, e.getLocalizedMessage());
return false;
}
} }
private boolean validateParameters() { private boolean validateParameters() {

View File

@ -22,25 +22,18 @@
package org.solovyev.android.calculator.jscl; package org.solovyev.android.calculator.jscl;
import org.solovyev.android.calculator.CalculatorParseException; import jscl.math.Generic;
import org.solovyev.android.calculator.text.TextProcessor; import org.solovyev.android.calculator.text.TextProcessor;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import jscl.math.Generic;
/**
* User: serso
* Date: 10/6/11
* Time: 9:48 PM
*/
class FromJsclNumericTextProcessor implements TextProcessor<String, Generic> { class FromJsclNumericTextProcessor implements TextProcessor<String, Generic> {
public static final FromJsclNumericTextProcessor instance = new FromJsclNumericTextProcessor(); public static final FromJsclNumericTextProcessor instance = new FromJsclNumericTextProcessor();
@Nonnull @Nonnull
@Override @Override
public String process(@Nonnull Generic numeric) throws CalculatorParseException { public String process(@Nonnull Generic numeric) {
return numeric.toString().replace("*", ""); return numeric.toString().replace("*", "");
} }
} }

View File

@ -25,7 +25,7 @@ package org.solovyev.android.calculator.math;
import jscl.JsclMathEngine; import jscl.JsclMathEngine;
import jscl.NumeralBase; import jscl.NumeralBase;
import jscl.math.function.Constants; 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.android.calculator.Locator;
import org.solovyev.common.JPredicate; import org.solovyev.common.JPredicate;
import org.solovyev.common.collections.Collections; import org.solovyev.common.collections.Collections;
@ -65,7 +65,7 @@ public enum MathType {
grouping_separator(250, false, false, MathGroupType.number, "'", " ") { grouping_separator(250, false, false, MathGroupType.number, "'", " ") {
@Override @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; return i;
} }
}, },
@ -347,7 +347,7 @@ public enum MathType {
return needMultiplicationSignBefore && mathTypeBefore.isNeedMultiplicationSignAfter(); 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); final String substitute = getSubstituteToJscl(match);
result.append(substitute == null ? match : substitute); result.append(substitute == null ? match : substitute);
return returnI(i, match); return returnI(i, match);
@ -400,7 +400,7 @@ public enum MathType {
this.match = match; 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); return type.processToJscl(result, i, match);
} }
} }

View File

@ -28,32 +28,18 @@ import android.support.v4.app.FragmentActivity;
import android.view.ContextMenu; import android.view.ContextMenu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import com.melnykov.fab.FloatingActionButton; import com.melnykov.fab.FloatingActionButton;
import jscl.math.function.IConstant;
import org.solovyev.android.calculator.CalculatorEventData; import org.solovyev.android.calculator.*;
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 org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.math.MathType;
import org.solovyev.common.JPredicate; import org.solovyev.common.JPredicate;
import org.solovyev.common.collections.Collections; import org.solovyev.common.collections.Collections;
import org.solovyev.common.text.Strings; import org.solovyev.common.text.Strings;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.ArrayList;
import jscl.math.function.IConstant; import java.util.List;
public class VarsFragment extends BaseEntitiesFragment<IConstant>implements CalculatorEventListener { public class VarsFragment extends BaseEntitiesFragment<IConstant>implements CalculatorEventListener {
@ -72,8 +58,6 @@ public class VarsFragment extends BaseEntitiesFragment<IConstant>implements Calc
return constants.isEmpty(); return constants.isEmpty();
} catch (RuntimeException e) { } catch (RuntimeException e) {
return true; return true;
} catch (CalculatorParseException e) {
return true;
} }
} }

View File

@ -27,7 +27,7 @@ import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList; import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root; import org.simpleframework.xml.Root;
import org.simpleframework.xml.Transient; 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.Locator;
import org.solovyev.android.calculator.PersistedEntity; import org.solovyev.android.calculator.PersistedEntity;
import org.solovyev.common.math.MathEntity; import org.solovyev.common.math.MathEntity;
@ -252,7 +252,7 @@ public class OldFunction implements IFunction, PersistedEntity, Serializable {
result.name = name; result.name = name;
try { try {
result.content = Locator.getInstance().getCalculator().prepareExpression(value).toString(); result.content = Locator.getInstance().getCalculator().prepareExpression(value).toString();
} catch (CalculatorParseException e) { } catch (ParseException e) {
throw new CreationException(e); throw new CreationException(e);
} }
result.system = system; result.system = system;
@ -265,9 +265,9 @@ public class OldFunction implements IFunction, PersistedEntity, Serializable {
public static class CreationException extends RuntimeException implements Message { public static class CreationException extends RuntimeException implements Message {
@Nonnull @Nonnull
private final CalculatorParseException message; private final ParseException message;
public CreationException(@Nonnull CalculatorParseException cause) { public CreationException(@Nonnull ParseException cause) {
super(cause); super(cause);
message = cause; message = cause;
} }

View File

@ -22,24 +22,17 @@
package org.solovyev.android.calculator.text; package org.solovyev.android.calculator.text;
import org.solovyev.android.calculator.CalculatorParseException; import jscl.math.Generic;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import jscl.math.Generic;
/**
* User: serso
* Date: 10/18/11
* Time: 10:39 PM
*/
public enum DummyTextProcessor implements TextProcessor<String, Generic> { public enum DummyTextProcessor implements TextProcessor<String, Generic> {
instance; instance;
@Nonnull @Nonnull
@Override @Override
public String process(@Nonnull Generic s) throws CalculatorParseException { public String process(@Nonnull Generic s) {
return s.toString(); return s.toString();
} }
} }

View File

@ -22,23 +22,15 @@
package org.solovyev.android.calculator.text; 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.Locator;
import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.math.MathType;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; 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<String, Generic> { public class FromJsclSimplifyTextProcessor implements TextProcessor<String, Generic> {
public static final FromJsclSimplifyTextProcessor instance = new FromJsclSimplifyTextProcessor(); public static final FromJsclSimplifyTextProcessor instance = new FromJsclSimplifyTextProcessor();
@ -49,7 +41,7 @@ public class FromJsclSimplifyTextProcessor implements TextProcessor<String, Gene
@Nonnull @Nonnull
@Override @Override
public String process(@Nonnull Generic from) throws CalculatorParseException { public String process(@Nonnull Generic from) {
return removeMultiplicationSigns(from.toString()); return removeMultiplicationSigns(from.toString());
} }

View File

@ -22,17 +22,10 @@
package org.solovyev.android.calculator.text; package org.solovyev.android.calculator.text;
import org.solovyev.android.calculator.CalculatorParseException;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
/**
* User: serso
* Date: 9/26/11
* Time: 12:12 PM
*/
public interface TextProcessor<TO extends CharSequence, FROM> { public interface TextProcessor<TO extends CharSequence, FROM> {
@Nonnull @Nonnull
TO process(@Nonnull FROM from) throws CalculatorParseException; TO process(@Nonnull FROM from);
} }

View File

@ -26,9 +26,8 @@ import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.view.View; import android.view.View;
import android.view.WindowManager; import android.view.WindowManager;
import org.solovyev.android.calculator.CalculatorParseException;
import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.Locator;
import org.solovyev.android.calculator.ParseException;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.ToJsclTextProcessor; import org.solovyev.android.calculator.ToJsclTextProcessor;
import org.solovyev.android.calculator.units.CalculatorNumeralBase; 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.Unit;
import org.solovyev.common.units.UnitImpl; import org.solovyev.common.units.UnitImpl;
import java.util.Arrays;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Arrays;
/** /**
* User: serso * User: serso
@ -66,7 +64,7 @@ public class NumeralBaseConverterDialog {
try { try {
value = ToJsclTextProcessor.getInstance().process(value).getExpression(); value = ToJsclTextProcessor.getInstance().process(value).getExpression();
b.setFromValue(UnitImpl.newInstance(value, CalculatorNumeralBase.valueOf(Locator.getInstance().getEngine().getMathEngine().getNumeralBase()))); 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()))); b.setFromValue(UnitImpl.newInstance(value, CalculatorNumeralBase.valueOf(Locator.getInstance().getEngine().getMathEngine().getNumeralBase())));
} }
} else { } else {

View File

@ -95,7 +95,7 @@ public class CalculatorTestUtils {
final JsclMathEngine jsclEngine = JsclMathEngine.getInstance(); final JsclMathEngine jsclEngine = JsclMathEngine.getInstance();
final VarsRegistry varsRegistry = new VarsRegistry(jsclEngine.getConstantsRegistry(), entityDao); 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 OperatorsRegistry operatorsRegistry = new OperatorsRegistry(jsclEngine.getOperatorsRegistry());
final PostfixFunctionsRegistry postfixFunctionsRegistry = new PostfixFunctionsRegistry(jsclEngine.getPostfixFunctionsRegistry()); final PostfixFunctionsRegistry postfixFunctionsRegistry = new PostfixFunctionsRegistry(jsclEngine.getPostfixFunctionsRegistry());

View File

@ -132,7 +132,7 @@ public class HistoryTest {
} }
private void addState(@Nonnull String text) { 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 = "<history>\n" + private static final String oldXml1 = "<history>\n" +
@ -281,7 +281,7 @@ public class HistoryTest {
@Test @Test
public void testShouldLoadStates() throws Exception { public void testShouldLoadStates() throws Exception {
final List<HistoryState> states = Json.load(new File(HistoryTest.class.getResource("recent-history.json").getFile())); final List<HistoryState> states = Json.load(new File(HistoryTest.class.getResource("recent-history.json").getFile()), HistoryState.JSON_CREATOR);
assertEquals(8, states.size()); assertEquals(8, states.size());
HistoryState state = states.get(0); HistoryState state = states.get(0);

View File

@ -27,7 +27,7 @@ import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.solovyev.android.calculator.AbstractCalculatorTest; import org.solovyev.android.calculator.AbstractCalculatorTest;
import org.solovyev.android.calculator.CalculatorEvalException; 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.CalculatorTestUtils;
import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.Locator;
import org.solovyev.common.Converter; import org.solovyev.common.Converter;
@ -42,7 +42,6 @@ import au.com.bytecode.opencsv.CSVReader;
import jscl.JsclMathEngine; import jscl.JsclMathEngine;
import jscl.MathEngine; import jscl.MathEngine;
import jscl.math.Expression; import jscl.math.Expression;
import jscl.text.ParseException;
import jscl.util.ExpressionGeneratorWithInput; import jscl.util.ExpressionGeneratorWithInput;
/** /**
@ -58,7 +57,7 @@ public class NumeralBaseTest extends AbstractCalculatorTest {
Locator.getInstance().getEngine().getMathEngine().setPrecision(3); Locator.getInstance().getEngine().getMathEngine().setPrecision(3);
} }
public static void testExpression(@Nonnull String[] line, @Nonnull Converter<String, String> converter) throws ParseException, CalculatorEvalException, CalculatorParseException { public static void testExpression(@Nonnull String[] line, @Nonnull Converter<String, String> converter) throws jscl.text.ParseException, CalculatorEvalException, ParseException {
final String dec = line[0].toUpperCase(); final String dec = line[0].toUpperCase();
final String hex = "0x:" + line[1].toUpperCase(); final String hex = "0x:" + line[1].toUpperCase();
final String bin = "0b:" + line[2].toUpperCase(); final String bin = "0b:" + line[2].toUpperCase();

View File

@ -26,7 +26,7 @@ import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.solovyev.android.calculator.AbstractCalculatorTest; 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.CalculatorTestUtils;
import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.Locator;
import org.solovyev.android.calculator.PreparedExpression; import org.solovyev.android.calculator.PreparedExpression;
@ -49,7 +49,7 @@ public class ToJsclTextProcessorTest extends AbstractCalculatorTest {
} }
@Test @Test
public void testSpecialCases() throws CalculatorParseException { public void testSpecialCases() throws ParseException {
final TextProcessor<PreparedExpression, String> preprocessor = ToJsclTextProcessor.getInstance(); final TextProcessor<PreparedExpression, String> preprocessor = ToJsclTextProcessor.getInstance();
Assert.assertEquals("3^E10", preprocessor.process("3^E10").toString()); Assert.assertEquals("3^E10", preprocessor.process("3^E10").toString());
} }
@ -108,30 +108,30 @@ public class ToJsclTextProcessorTest extends AbstractCalculatorTest {
try { try {
preprocessor.process("ln()"); preprocessor.process("ln()");
Assert.fail(); Assert.fail();
} catch (CalculatorParseException e) { } catch (ParseException e) {
} }
try { try {
preprocessor.process("ln()ln()"); preprocessor.process("ln()ln()");
Assert.fail(); Assert.fail();
} catch (CalculatorParseException e) { } catch (ParseException e) {
} }
try { try {
preprocessor.process("eln()eln()ln()ln()ln()e"); preprocessor.process("eln()eln()ln()ln()ln()e");
Assert.fail(); Assert.fail();
} catch (CalculatorParseException e) { } catch (ParseException e) {
} }
try { try {
preprocessor.process("ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln()))))))))))))))"); preprocessor.process("ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln()))))))))))))))");
Assert.fail(); Assert.fail();
} catch (CalculatorParseException e) { } catch (ParseException e) {
} }
try { 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())))))))))))))))))))))))))))))))))))))"); 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(); Assert.fail();
} catch (CalculatorParseException e) { } catch (ParseException e) {
} }
} }