diff --git a/res/drawable/paste.png b/res/drawable/paste.png
new file mode 100644
index 00000000..910bed20
Binary files /dev/null and b/res/drawable/paste.png differ
diff --git a/res/layout-land/main.xml b/res/layout-land/main.xml
index e1979bd5..ad76b96a 100644
--- a/res/layout-land/main.xml
+++ b/res/layout-land/main.xml
@@ -100,10 +100,10 @@
style="@style/control_button_style"
a:onClick="numericButtonClickHandler"/>
-
+
@@ -185,7 +185,8 @@
diff --git a/res/layout-port/main.xml b/res/layout-port/main.xml
index ba5b02b1..8c1eb2bc 100644
--- a/res/layout-port/main.xml
+++ b/res/layout-port/main.xml
@@ -130,10 +130,10 @@
style="@style/digit_button_style"
a:onClick="digitButtonClickHandler"/>
-
+
@@ -198,7 +198,8 @@
diff --git a/res/values-ru/strings.xml b/res/values-ru/strings.xml
index 33d8c13c..e910cafc 100644
--- a/res/values-ru/strings.xml
+++ b/res/values-ru/strings.xml
@@ -1,4 +1,58 @@
Калькулятор
+ Калькулятор
+ Ошибка
+ Результат скопирован в буфер!
+ Настройки
+ Помощь
+ О программе
+ Переменные и константы
+
+
+ Основные настройки
+ Настройки кнопок
+
+
+ Вниз
+ Вверх
+ Перезапуск
+
+
+ Copyright (c) 2009-2011.\nПрограмма создана serso aka se.solovyev.\n
+ За подробной информацией, пожалуйста,\nобращайтесь на почту\nse.solovyev@gmail.com
+ \nили посетите\nhttp://se.solovyev.org
+
+
+ назад
+ вперёд
+ вставить
+ переменные
+
+ Подсветка выражений
+ Точность результата
+ Выход
+ Добавить
+ Отмена
+ Сохранить
+ Удалить
+ Да
+ Нет
+ Подтверждение удаления
+ Вы действительно хотите удалить переменную \'%s\'?
+ Имя
+ Значение
+ Описание
+ Создать переменную
+ Редактировать переменную
+
+ Значение - не число!
+ Имя переменной не может быть зарезервированным системным именем!
+ Переменная с таким именем уже существует!
+ Имя не может быть пустым!
+ Системная переменная не может быть изменена!
+
+ Отношение длины окружности к диаметру
+ Вещесвтенное число, такое что производная функции f(x) = e^x в точке x = 0 равно 1
+ Мнимая единица, определённая как i^2 = −1
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 88ea3c15..105168f3 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -27,7 +27,7 @@
undo
redo
- clear
+ clr
paste
vars
@@ -47,4 +47,14 @@
Description
Create variable
Edit variable
+
+ Value is not a number!
+ Variable name clashes with function name!
+ Variable with same name already exists!
+ Name is empty!
+ System variable cannot be changed!
+
+ Ratio of any circle\'s circumference to its diameter
+ Unique real number such that the value of the derivative (slope of the tangent line) of the function f(x) = e^x at the point x = 0 is equal to 1
+ Imaginary unit, defined such that i^2 = −1
diff --git a/res/xml/main_preferences.xml b/res/xml/main_preferences.xml
index 351ab219..d0730155 100644
--- a/res/xml/main_preferences.xml
+++ b/res/xml/main_preferences.xml
@@ -2,7 +2,7 @@
-
+
");
i = processBracketGroup(result, s, i + 1, numberOfOpenings + 1, maxNumberOfGroups);
result.append("");
- if (i < s.length() && MathEntityType.closeGroupSymbols.contains(s.charAt(i))) {
+ if (i < s.length() && MathType.closeGroupSymbols.contains(s.charAt(i))) {
result.append(s.charAt(i));
}
- } else if (MathEntityType.closeGroupSymbols.contains(ch)) {
+ } else if (MathType.closeGroupSymbols.contains(ch)) {
break;
} else {
result.append(ch);
diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorModel.java b/src/main/java/org/solovyev/android/calculator/CalculatorModel.java
deleted file mode 100644
index 892c5306..00000000
--- a/src/main/java/org/solovyev/android/calculator/CalculatorModel.java
+++ /dev/null
@@ -1,212 +0,0 @@
-/*
- * Copyright (c) 2009-2011. Created by serso aka se.solovyev.
- * For more information, please, contact se.solovyev@gmail.com
- */
-
-package org.solovyev.android.calculator;
-
-import android.content.Context;
-import android.content.SharedPreferences;
-import android.preference.PreferenceManager;
-import bsh.EvalError;
-import bsh.Interpreter;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-import org.solovyev.common.NumberMapper;
-import org.solovyev.common.exceptions.SersoException;
-import org.solovyev.common.utils.MathUtils;
-import org.solovyev.util.math.Complex;
-
-/**
- * User: serso
- * Date: 9/12/11
- * Time: 11:38 PM
- */
-
-public class CalculatorModel {
-
- @NotNull
- private Interpreter interpreter;
-
- @NotNull
- private final Object interpreterMonitor = new Object();
-
- private int numberOfFractionDigits = 5;
-
- @NotNull
- public final Preprocessor preprocessor = new ToJsclPreprocessor();
-
- @NotNull
- private final VarsRegister varsRegister = new VarsRegister();
-
- private static CalculatorModel instance;
-
- public CalculatorModel(@Nullable Context context) {
- load(context);
-
- reset();
- }
-
- public void reset() {
- synchronized (interpreterMonitor) {
- try {
- interpreter = new Interpreter();
- interpreter.eval(ToJsclPreprocessor.wrap(JsclOperation.importCommands, "/jscl/editorengine/commands"));
-
- /*for (Var var : varsRegister.getVars()) {
- if (!var.isSystem()) {
- exec(var.getName() + "=" + var.getValue() + ";");
- }
- }*/
- } catch (EvalError evalError) {
- throw new RuntimeException(evalError);
- }
- }
- }
-
- public String evaluate(@NotNull JsclOperation operation, @NotNull String expression) throws EvalError, ParseException {
-
- final StringBuilder sb = new StringBuilder();
-
- sb.append(preprocessor.process(expression));
-
- //Log.d(CalculatorModel.class.getName(), "Preprocessed expression: " + preprocessedExpression);
-
- final Object evaluationObject;
- synchronized (interpreterMonitor) {
- evaluationObject = interpreter.eval(ToJsclPreprocessor.wrap(operation, sb.toString()));
- }
- String result = String.valueOf(evaluationObject).trim();
-
- try {
- result = String.valueOf(round(result));
- } catch (NumberFormatException e) {
- if (result.contains("sqrt(-1)")) {
- try {
- result = createResultForComplexNumber(result.replace("sqrt(-1)", "i"));
- } catch (NumberFormatException e1) {
- // throw original one
- throw new ParseException(e);
- }
-
- } else {
- throw new ParseException(e);
- }
- }
-
- return result;
- }
-
- public String createResultForComplexNumber(@NotNull final String s) {
- final Complex complex = new Complex();
-
- String result = "";
- // may be it's just complex number
- int plusIndex = s.lastIndexOf("+");
- if (plusIndex >= 0) {
- complex.setReal(round(s.substring(0, plusIndex)));
- result += complex.getReal();
- result += "+";
- } else {
- plusIndex = s.lastIndexOf("-");
- if (plusIndex >= 0) {
- complex.setReal(round(s.substring(0, plusIndex)));
- result += complex.getReal();
- result += "-";
- }
- }
-
-
- int multiplyIndex = s.indexOf("*");
- if (multiplyIndex >= 0) {
- complex.setImaginary(round(s.substring(plusIndex >= 0 ? plusIndex + 1 : 0, multiplyIndex)));
- result += complex.getImaginary();
-
- }
-
- result += "i";
-
- return result;
- }
-
- private Double round(@NotNull String result) {
- final Double dResult = Double.valueOf(result);
- return MathUtils.round(dResult, numberOfFractionDigits);
- }
-
- public synchronized void load(@Nullable Context context) {
- if (context != null) {
- final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
-
- final NumberMapper integerNumberMapper = new NumberMapper(Integer.class);
- this.setNumberOfFractionDigits(integerNumberMapper.parseValue(preferences.getString(context.getString(R.string.p_calc_result_precision_key), context.getString(R.string.p_calc_result_precision))));
- }
-
- varsRegister.load(context);
- }
-
- public static class ParseException extends SersoException {
- public ParseException(Throwable cause) {
- super(cause);
- }
- }
-
- public int getNumberOfFractionDigits() {
- return numberOfFractionDigits;
- }
-
- public void setNumberOfFractionDigits(int numberOfFractionDigits) {
- this.numberOfFractionDigits = numberOfFractionDigits;
- }
-
- public static synchronized void init(@Nullable Context context) throws EvalError {
- if (!isLoaded()) {
- instance = new CalculatorModel(context);
- } else {
- throw new RuntimeException("Calculator model already instantiated!");
- }
- }
-
- public static CalculatorModel getInstance() {
- if (!isLoaded()) {
- throw new RuntimeException("CalculatorModel must be instantiated!");
- }
-
- return instance;
- }
-
- public static boolean isLoaded() {
- return instance != null;
- }
-
-
- private void exec(String str) throws EvalError {
- interpreter.eval(str);
- }
-
- private String eval(String str) throws EvalError {
- return interpreter.eval(commands(str)).toString();
- }
-
-
- @NotNull
- public VarsRegister getVarsRegister() {
- return varsRegister;
- }
-
- String commands(String str) {
- return commands(str, false);
- }
-
- String commands(String str, boolean found) {
- for (int i = 0; i < cmds.length; i++) {
- int n = str.length() - cmds[i].length() - 1;
- if (n >= 0 && (" " + cmds[i].toLowerCase()).equals(str.substring(n)))
- return commands(str.substring(0, n), true) + "." + cmds[i] + "()";
- }
- str = str.replaceAll("\n", "");
- return found ? "jscl.math.Expression.valueOf(\"" + str + "\")" : str;
- }
-
- static final String cmds[] = new String[]{"expand", "factorize", "elementary", "simplify", "numeric", "toMathML", "toJava"};
-}
diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java
index 4b90a37c..df9eb7ef 100644
--- a/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java
+++ b/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java
@@ -16,7 +16,10 @@ import android.view.*;
import android.widget.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
-import org.solovyev.android.calculator.math.MathEntityType;
+import org.solovyev.android.calculator.math.MathType;
+import org.solovyev.android.calculator.model.CalculatorModel;
+import org.solovyev.android.calculator.model.Var;
+import org.solovyev.android.calculator.model.VarsRegister;
import org.solovyev.common.utils.StringUtils;
import java.util.ArrayList;
@@ -36,7 +39,7 @@ public class CalculatorVarsActivity extends ListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- adapter = new VarsArrayAdapter(this, R.layout.var, R.id.var_text, new ArrayList(CalculatorModel.getInstance().getVarsRegister().getVars()));
+ adapter = new VarsArrayAdapter(this, R.layout.var, R.id.var_text, new ArrayList(CalculatorModel.instance.getVarsRegister().getVars()));
setListAdapter(adapter);
final ListView lv = getListView();
@@ -111,7 +114,7 @@ public class CalculatorVarsActivity extends ListActivity {
builder.create().show();
} else {
- Toast.makeText(this, "System variable cannot be changed!", Toast.LENGTH_LONG).show();
+ Toast.makeText(this, getString(R.string.c_sys_var_cannot_be_changed), Toast.LENGTH_LONG).show();
}
}
@@ -135,7 +138,7 @@ public class CalculatorVarsActivity extends ListActivity {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
- final String error;
+ final Integer error;
final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name);
String name = editName.getText().toString();
@@ -147,13 +150,13 @@ public class CalculatorVarsActivity extends ListActivity {
String description = editDescription.getText().toString();
- final VarsRegister varsRegister = CalculatorModel.getInstance().getVarsRegister();
+ final VarsRegister varsRegister = CalculatorModel.instance.getVarsRegister();
if (!StringUtils.isEmpty(name)) {
final Var varFromRegister = varsRegister.getVar(name);
if (varFromRegister == null || varFromRegister == editedInstance) {
- final MathEntityType.Result mathType = MathEntityType.getType(name, 0);
+ final MathType.Result mathType = MathType.getType(name, 0);
- if (mathType.getMathEntityType() == MathEntityType.text || mathType.getMathEntityType() == MathEntityType.constant) {
+ if (mathType.getMathType() == MathType.text || mathType.getMathType() == MathType.constant) {
boolean correctDouble = true;
try {
Double.valueOf(value);
@@ -167,20 +170,20 @@ public class CalculatorVarsActivity extends ListActivity {
varBuilder.setDescription(description);
error = null;
} else {
- error = "Value is not a number!";
+ error = R.string.c_value_is_not_a_number;
}
} else {
- error = "Variable name clashes with function name!";
+ error = R.string.c_var_name_clashes;
}
} else {
- error = "Variable with same name already exist!";
+ error = R.string.c_var_already_exists;
}
} else {
- error = "Name is empty!";
+ error = R.string.c_name_is_empty;
}
if (error != null) {
- Toast.makeText(CalculatorVarsActivity.this, error, Toast.LENGTH_LONG).show();
+ Toast.makeText(CalculatorVarsActivity.this, getString(error), Toast.LENGTH_LONG).show();
createEditVariableDialog(editedInstance, name, value, description);
} else {
if ( editedInstance == null ) {
@@ -290,7 +293,7 @@ public class CalculatorVarsActivity extends ListActivity {
builder.create().show();
} else {
adapter.remove(var);
- final VarsRegister varsRegister = CalculatorModel.getInstance().getVarsRegister();
+ final VarsRegister varsRegister = CalculatorModel.instance.getVarsRegister();
varsRegister.remove(var);
varsRegister.save(CalculatorVarsActivity.this);
CalculatorVarsActivity.this.adapter.notifyDataSetChanged();
diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorView.java b/src/main/java/org/solovyev/android/calculator/CalculatorView.java
index 875edb72..1e96d7d1 100644
--- a/src/main/java/org/solovyev/android/calculator/CalculatorView.java
+++ b/src/main/java/org/solovyev/android/calculator/CalculatorView.java
@@ -19,7 +19,9 @@ import android.widget.Toast;
import bsh.EvalError;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
-import org.solovyev.android.calculator.math.MathEntityType;
+import org.solovyev.android.calculator.math.MathType;
+import org.solovyev.android.calculator.model.CalculatorModel;
+import org.solovyev.android.calculator.model.ParseException;
import org.solovyev.android.view.CursorControl;
import org.solovyev.android.view.HistoryControl;
import org.solovyev.common.utils.MutableObject;
@@ -36,7 +38,7 @@ import org.solovyev.common.utils.history.SimpleHistoryHelper;
public class CalculatorView implements CursorControl, HistoryControl {
// millis to wait before evaluation after user edit action
- public static final int EVAL_DELAY_MILLIS = 500;
+ public static final int EVAL_DELAY_MILLIS = 700;
@NotNull
private final CalculatorEditor editor;
@@ -44,9 +46,6 @@ public class CalculatorView implements CursorControl, HistoryControl history;
public CalculatorView(@NotNull final Activity activity, @NotNull CalculatorModel calculator) {
- this.activity = activity;
this.calculatorModel = calculator;
final InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
@@ -159,7 +157,7 @@ public class CalculatorView implements CursorControl, HistoryControl allPostfix = Arrays.asList(FACT, DEGREE);
-
- private Functions() {
- throw new AssertionError("Not allowed!");
- }
}
diff --git a/src/main/java/org/solovyev/android/calculator/math/MathEntityType.java b/src/main/java/org/solovyev/android/calculator/math/MathType.java
similarity index 78%
rename from src/main/java/org/solovyev/android/calculator/math/MathEntityType.java
rename to src/main/java/org/solovyev/android/calculator/math/MathType.java
index 95b1b398..80c2ab40 100644
--- a/src/main/java/org/solovyev/android/calculator/math/MathEntityType.java
+++ b/src/main/java/org/solovyev/android/calculator/math/MathType.java
@@ -6,9 +6,9 @@
package org.solovyev.android.calculator.math;
import org.jetbrains.annotations.NotNull;
-import org.solovyev.android.calculator.CalculatorModel;
import org.solovyev.android.calculator.CharacterAtPositionFinder;
import org.solovyev.android.calculator.StartsWithFinder;
+import org.solovyev.android.calculator.model.CalculatorModel;
import org.solovyev.common.utils.Finder;
import java.util.Arrays;
@@ -16,7 +16,7 @@ import java.util.List;
import static org.solovyev.common.utils.CollectionsUtils.get;
-public enum MathEntityType {
+public enum MathType {
digit,
constant,
@@ -30,7 +30,12 @@ public enum MathEntityType {
close_group_symbol,
text;
- public static final List constants = Arrays.asList("e", "π", "i");
+ public static final String IMAGINARY_NUMBER = "i";
+ public static final String IMAGINARY_NUMBER_DEF = "sqrt(-1)";
+ public static final String PI = "π";
+ public static final String E = "e";
+
+ public static final List constants = Arrays.asList(E, PI, IMAGINARY_NUMBER);
public static final List digits = Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
@@ -57,11 +62,6 @@ public enum MathEntityType {
* @param i index which points to start of substring
* @return math entity type of substring starting from ith index of specified text
*/
- @NotNull
- public static MathEntityType getMathEntityType(@NotNull String text, int i) {
- return getType(text, i).getMathEntityType();
- }
-
@NotNull
public static Result getType(@NotNull String text, int i) {
if (i < 0) {
@@ -69,7 +69,7 @@ public enum MathEntityType {
} else if (i >= text.length() && i != 0) {
throw new IllegalArgumentException("I must be less than size of text.");
} else if (i == 0 && text.length() == 0) {
- return new Result(MathEntityType.text, text);
+ return new Result(MathType.text, text);
}
final StartsWithFinder stringStartWithFinder = new StartsWithFinder(text, i);
@@ -77,7 +77,7 @@ public enum MathEntityType {
String foundString = get(digits, stringStartWithFinder);
if (foundString != null) {
- return new Result(MathEntityType.digit, foundString);
+ return new Result(MathType.digit, foundString);
}
Character foundCharacter = get(dots, characterStartWithFinder);
@@ -102,7 +102,7 @@ public enum MathEntityType {
foundString = get(groupSymbols, stringStartWithFinder);
if (foundString != null) {
- return new Result(MathEntityType.group_symbols, foundString);
+ return new Result(MathType.group_symbols, foundString);
}
foundCharacter = get(openGroupSymbols, characterStartWithFinder);
@@ -117,39 +117,39 @@ public enum MathEntityType {
foundString = get(prefixFunctions, stringStartWithFinder);
if (foundString != null) {
- return new Result(MathEntityType.function, foundString);
+ return new Result(MathType.function, foundString);
}
- foundString = get(CalculatorModel.getInstance().getVarsRegister().getVarNames(), stringStartWithFinder);
+ foundString = get(CalculatorModel.instance.getVarsRegister().getVarNames(), stringStartWithFinder);
if (foundString != null) {
- return new Result(MathEntityType.constant, foundString);
+ return new Result(MathType.constant, foundString);
}
- return new Result(MathEntityType.text, text.substring(i));
+ return new Result(MathType.text, text.substring(i));
}
public static class Result {
@NotNull
- private final MathEntityType mathEntityType;
+ private final MathType mathType;
@NotNull
- private final String s;
+ private final String match;
- private Result(@NotNull MathEntityType mathEntityType, @NotNull String s){
- this.mathEntityType = mathEntityType;
+ private Result(@NotNull MathType mathType, @NotNull String match){
+ this.mathType = mathType;
- this.s = s;
+ this.match = match;
}
@NotNull
- public String getS() {
- return s;
+ public String getMatch() {
+ return match;
}
@NotNull
- public MathEntityType getMathEntityType() {
- return mathEntityType;
+ public MathType getMathType() {
+ return mathType;
}
}
diff --git a/src/main/java/org/solovyev/android/calculator/model/CalculatorModel.java b/src/main/java/org/solovyev/android/calculator/model/CalculatorModel.java
new file mode 100644
index 00000000..354cde40
--- /dev/null
+++ b/src/main/java/org/solovyev/android/calculator/model/CalculatorModel.java
@@ -0,0 +1,131 @@
+/*
+ * Copyright (c) 2009-2011. Created by serso aka se.solovyev.
+ * For more information, please, contact se.solovyev@gmail.com
+ */
+
+package org.solovyev.android.calculator.model;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.preference.PreferenceManager;
+import bsh.EvalError;
+import bsh.Interpreter;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.solovyev.android.calculator.JsclOperation;
+import org.solovyev.common.NumberMapper;
+
+/**
+ * User: serso
+ * Date: 9/12/11
+ * Time: 11:38 PM
+ */
+
+public enum CalculatorModel {
+
+ instance;
+
+ private static final String RESULT_PRECISION_P_KEY = "org.solovyev.android.calculator.CalculatorModel_result_precision";
+ private static final String RESULT_PRECISION_DEFAULT = "5";
+
+ @NotNull
+ private Interpreter interpreter;
+
+ @NotNull
+ private final Object lock = new Object();
+
+ private int numberOfFractionDigits = 5;
+
+ @NotNull
+ public final TextProcessor preprocessor = new ToJsclTextProcessor();
+
+ @NotNull
+ public final TextProcessor postprocessor = new FromJsclTextProcessor();
+
+ @NotNull
+ private final VarsRegisterImpl varsRegister = new VarsRegisterImpl();
+
+ public String evaluate(@NotNull JsclOperation operation, @NotNull String expression) throws EvalError, ParseException {
+ synchronized (lock) {
+ final StringBuilder sb = new StringBuilder();
+
+ sb.append(preprocessor.process(expression));
+
+ //Log.d(CalculatorModel.class.getName(), "Preprocessed expression: " + preprocessedExpression);
+
+ final Object evaluationObject = interpreter.eval(ToJsclTextProcessor.wrap(operation, sb.toString()));
+
+ return postprocessor.process(String.valueOf(evaluationObject).trim());
+ }
+ }
+
+ public int getNumberOfFractionDigits() {
+ return numberOfFractionDigits;
+ }
+
+ public void setNumberOfFractionDigits(int numberOfFractionDigits) {
+ this.numberOfFractionDigits = numberOfFractionDigits;
+ }
+
+ public void init(@Nullable Context context) throws EvalError {
+ synchronized (lock) {
+ reset(context);
+ resetInterpreter();
+ }
+ }
+
+ public void reset(@Nullable Context context) {
+ synchronized (lock) {
+ if (context != null) {
+ final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
+
+ final NumberMapper integerNumberMapper = new NumberMapper(Integer.class);
+ //noinspection ConstantConditions
+ this.setNumberOfFractionDigits(integerNumberMapper.parseValue(preferences.getString(RESULT_PRECISION_P_KEY, RESULT_PRECISION_DEFAULT)));
+ }
+
+ varsRegister.init(context);
+ }
+ }
+
+ public void resetInterpreter() {
+ synchronized (lock) {
+ try {
+ interpreter = new Interpreter();
+ interpreter.eval(ToJsclTextProcessor.wrap(JsclOperation.importCommands, "/jscl/editorengine/commands"));
+ } catch (EvalError evalError) {
+ throw new RuntimeException(evalError);
+ }
+ }
+ }
+
+ @NotNull
+ public VarsRegister getVarsRegister() {
+ return varsRegister;
+ }
+
+ /* private String commands(String str) {
+ return commands(str, false);
+ }
+
+
+ private void exec(String str) throws EvalError {
+ interpreter.eval(str);
+ }
+
+ private String eval(String str) throws EvalError {
+ return interpreter.eval(commands(str)).toString();
+ }
+
+ private String commands(String str, boolean found) {
+ for (int i = 0; i < cmds.length; i++) {
+ int n = str.length() - cmds[i].length() - 1;
+ if (n >= 0 && (" " + cmds[i].toLowerCase()).equals(str.substring(n)))
+ return commands(str.substring(0, n), true) + "." + cmds[i] + "()";
+ }
+ str = str.replaceAll("\n", "");
+ return found ? "jscl.math.Expression.valueOf(\"" + str + "\")" : str;
+ }
+
+ private static final String cmds[] = new String[]{"expand", "factorize", "elementary", "simplify", "numeric", "toMathML", "toJava"};*/
+}
diff --git a/src/main/java/org/solovyev/android/calculator/model/FromJsclTextProcessor.java b/src/main/java/org/solovyev/android/calculator/model/FromJsclTextProcessor.java
new file mode 100644
index 00000000..0f5878b6
--- /dev/null
+++ b/src/main/java/org/solovyev/android/calculator/model/FromJsclTextProcessor.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2009-2011. Created by serso aka se.solovyev.
+ * For more information, please, contact se.solovyev@gmail.com
+ * or visit http://se.solovyev.org
+ */
+
+package org.solovyev.android.calculator.model;
+
+import org.jetbrains.annotations.NotNull;
+import org.solovyev.android.calculator.math.MathType;
+import org.solovyev.common.utils.MathUtils;
+import org.solovyev.util.math.Complex;
+
+/**
+ * User: serso
+ * Date: 10/6/11
+ * Time: 9:48 PM
+ */
+class FromJsclTextProcessor implements TextProcessor {
+
+ @NotNull
+ @Override
+ public String process(@NotNull String s) throws ParseException {
+ String result = null;
+
+ try {
+ result = String.valueOf(round(s));
+ } catch (NumberFormatException e) {
+ if (result.contains(MathType.IMAGINARY_NUMBER_DEF)) {
+ try {
+ result = createResultForComplexNumber(result.replace(MathType.IMAGINARY_NUMBER_DEF, MathType.IMAGINARY_NUMBER));
+ } catch (NumberFormatException e1) {
+ // throw original one
+ throw new ParseException(e);
+ }
+
+ } else {
+ throw new ParseException(e);
+ }
+ }
+
+ return result;
+ }
+
+ protected String createResultForComplexNumber(@NotNull final String s) {
+ final Complex complex = new Complex();
+
+ String result = "";
+ // may be it's just complex number
+ int plusIndex = s.lastIndexOf("+");
+ if (plusIndex >= 0) {
+ complex.setReal(round(s.substring(0, plusIndex)));
+ result += complex.getReal();
+ result += "+";
+ } else {
+ plusIndex = s.lastIndexOf("-");
+ if (plusIndex >= 0) {
+ complex.setReal(round(s.substring(0, plusIndex)));
+ result += complex.getReal();
+ result += "-";
+ }
+ }
+
+
+ int multiplyIndex = s.indexOf("*");
+ if (multiplyIndex >= 0) {
+ complex.setImaginary(round(s.substring(plusIndex >= 0 ? plusIndex + 1 : 0, multiplyIndex)));
+ result += complex.getImaginary();
+
+ }
+
+ result += MathType.IMAGINARY_NUMBER;
+
+ return result;
+ }
+
+ private Double round(@NotNull String result) {
+ final Double dResult = Double.valueOf(result);
+ return MathUtils.round(dResult, CalculatorModel.instance.getNumberOfFractionDigits());
+ }
+}
diff --git a/src/main/java/org/solovyev/android/calculator/model/ParseException.java b/src/main/java/org/solovyev/android/calculator/model/ParseException.java
new file mode 100644
index 00000000..33041bfe
--- /dev/null
+++ b/src/main/java/org/solovyev/android/calculator/model/ParseException.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2009-2011. Created by serso aka se.solovyev.
+ * For more information, please, contact se.solovyev@gmail.com
+ * or visit http://se.solovyev.org
+ */
+
+package org.solovyev.android.calculator.model;
+
+import org.solovyev.common.exceptions.SersoException;
+
+/**
+* User: serso
+* Date: 10/6/11
+* Time: 9:25 PM
+*/
+public class ParseException extends SersoException {
+ public ParseException(Throwable cause) {
+ super(cause);
+ }
+}
diff --git a/src/main/java/org/solovyev/android/calculator/model/TextProcessor.java b/src/main/java/org/solovyev/android/calculator/model/TextProcessor.java
new file mode 100644
index 00000000..082cd196
--- /dev/null
+++ b/src/main/java/org/solovyev/android/calculator/model/TextProcessor.java
@@ -0,0 +1,14 @@
+package org.solovyev.android.calculator.model;
+
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * User: serso
+ * Date: 9/26/11
+ * Time: 12:12 PM
+ */
+public interface TextProcessor {
+
+ @NotNull
+ String process(@NotNull String s) throws ParseException;
+}
diff --git a/src/main/java/org/solovyev/android/calculator/ToJsclPreprocessor.java b/src/main/java/org/solovyev/android/calculator/model/ToJsclTextProcessor.java
similarity index 57%
rename from src/main/java/org/solovyev/android/calculator/ToJsclPreprocessor.java
rename to src/main/java/org/solovyev/android/calculator/model/ToJsclTextProcessor.java
index 9ad0b02c..14d49cfe 100644
--- a/src/main/java/org/solovyev/android/calculator/ToJsclPreprocessor.java
+++ b/src/main/java/org/solovyev/android/calculator/model/ToJsclTextProcessor.java
@@ -4,17 +4,19 @@
* or visit http://se.solovyev.org
*/
-package org.solovyev.android.calculator;
+package org.solovyev.android.calculator.model;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import org.solovyev.android.calculator.JsclOperation;
+import org.solovyev.android.calculator.StartsWithFinder;
import org.solovyev.android.calculator.math.Functions;
-import org.solovyev.android.calculator.math.MathEntityType;
+import org.solovyev.android.calculator.math.MathType;
import org.solovyev.common.utils.CollectionsUtils;
import org.solovyev.common.utils.FilterType;
import org.solovyev.common.utils.Finder;
-public class ToJsclPreprocessor implements Preprocessor {
+class ToJsclTextProcessor implements TextProcessor {
@Override
@NotNull
@@ -23,26 +25,26 @@ public class ToJsclPreprocessor implements Preprocessor {
final StartsWithFinder startsWithFinder = new StartsWithFinder(s, 0);
final StringBuilder sb = new StringBuilder();
- MathEntityType.Result mathTypeResult = null;
+ MathType.Result mathTypeResult = null;
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
startsWithFinder.setI(i);
mathTypeResult = checkMultiplicationSignBeforeFunction(sb, s, i, mathTypeResult);
- final MathEntityType mathType = mathTypeResult.getMathEntityType();
- if (mathType == MathEntityType.open_group_symbol) {
+ final MathType mathType = mathTypeResult.getMathType();
+ if (mathType == MathType.open_group_symbol) {
sb.append('(');
- } else if (mathType == MathEntityType.close_group_symbol) {
+ } else if (mathType == MathType.close_group_symbol) {
sb.append(')');
} else if (ch == '×' || ch == '∙') {
sb.append("*");
- } else if ( mathType == MathEntityType.function ){
- sb.append(toJsclFunction(mathTypeResult.getS()));
- i += mathTypeResult.getS().length() - 1;
- } else if ( mathType == MathEntityType.constant ) {
- sb.append(mathTypeResult.getS());
- i += mathTypeResult.getS().length() - 1;
+ } else if ( mathType == MathType.function ){
+ sb.append(toJsclFunction(mathTypeResult.getMatch()));
+ i += mathTypeResult.getMatch().length() - 1;
+ } else if ( mathType == MathType.constant ) {
+ sb.append(mathTypeResult.getMatch());
+ i += mathTypeResult.getMatch().length() - 1;
} else {
sb.append(ch);
}
@@ -59,11 +61,11 @@ public class ToJsclPreprocessor implements Preprocessor {
startsWithFinder.setI(i);
int offset = 0;
- String functionName = CollectionsUtils.get(MathEntityType.prefixFunctions, startsWithFinder);
+ String functionName = CollectionsUtils.get(MathType.prefixFunctions, startsWithFinder);
if (functionName == null) {
- String varName = CollectionsUtils.get(CalculatorModel.getInstance().getVarsRegister().getVarNames(), startsWithFinder);
+ String varName = CollectionsUtils.get(CalculatorModel.instance.getVarsRegister().getVarNames(), startsWithFinder);
if (varName != null) {
- final Var var = CalculatorModel.getInstance().getVarsRegister().getVar(varName);
+ final Var var = CalculatorModel.instance.getVarsRegister().getVar(varName);
if (var != null) {
result.append(var.getValue());
offset = varName.length();
@@ -86,10 +88,10 @@ public class ToJsclPreprocessor implements Preprocessor {
}
private void replaceVariables(StringBuilder sb, String s, int i, @NotNull StartsWithFinder startsWithFinder) {
- for (Var var : CalculatorModel.getInstance().getVarsRegister().getVars()) {
+ for (Var var : CalculatorModel.instance.getVarsRegister().getVars()) {
if (!var.isSystem()) {
if (s.startsWith(var.getName(), i)) {
- if (CollectionsUtils.get(MathEntityType.prefixFunctions, startsWithFinder) == null) {
+ if (CollectionsUtils.get(MathType.prefixFunctions, startsWithFinder) == null) {
}
}
}
@@ -103,13 +105,13 @@ public class ToJsclPreprocessor implements Preprocessor {
int result = position;
for (; result >= 0; result--) {
- final MathEntityType mathEntityType = MathEntityType.getMathEntityType(s, result);
+ final MathType mathType = MathType.getType(s, result).getMathType();
- if (CollectionsUtils.contains(mathEntityType, MathEntityType.digit, MathEntityType.dot)) {
+ if (CollectionsUtils.contains(mathType, MathType.digit, MathType.dot)) {
// continue
- } else if (mathEntityType == MathEntityType.close_group_symbol) {
+ } else if (mathType == MathType.close_group_symbol) {
numberOfOpenGroups++;
- } else if (mathEntityType == MathEntityType.open_group_symbol) {
+ } else if (mathType == MathType.open_group_symbol) {
numberOfOpenGroups--;
} else {
if (stop(s, numberOfOpenGroups, result)) break;
@@ -124,9 +126,9 @@ public class ToJsclPreprocessor implements Preprocessor {
if (i > 0) {
final EndsWithFinder endsWithFinder = new EndsWithFinder(s);
endsWithFinder.setI(i + 1);
- if (!CollectionsUtils.contains(MathEntityType.prefixFunctions, FilterType.included, endsWithFinder)) {
- MathEntityType type = MathEntityType.getMathEntityType(s, i);
- if (type != MathEntityType.constant) {
+ if (!CollectionsUtils.contains(MathType.prefixFunctions, FilterType.included, endsWithFinder)) {
+ MathType type = MathType.getType(s, i).getMathType();
+ if (type != MathType.constant) {
return true;
}
}
@@ -175,31 +177,31 @@ public class ToJsclPreprocessor implements Preprocessor {
}
@NotNull
- private static MathEntityType.Result checkMultiplicationSignBeforeFunction(@NotNull StringBuilder sb,
+ private static MathType.Result checkMultiplicationSignBeforeFunction(@NotNull StringBuilder sb,
@NotNull String s,
int i,
- @Nullable MathEntityType.Result mathTypeBeforeResult) {
- MathEntityType.Result result = MathEntityType.getType(s, i);
+ @Nullable MathType.Result mathTypeBeforeResult) {
+ MathType.Result result = MathType.getType(s, i);
if (i > 0) {
- final MathEntityType mathType = result.getMathEntityType();
+ final MathType mathType = result.getMathType();
assert mathTypeBeforeResult != null;
- final MathEntityType mathTypeBefore = mathTypeBeforeResult.getMathEntityType();
+ final MathType mathTypeBefore = mathTypeBeforeResult.getMathType();
- if (mathTypeBefore == MathEntityType.constant || (mathTypeBefore != MathEntityType.binary_operation &&
- mathTypeBefore != MathEntityType.unary_operation &&
- mathTypeBefore != MathEntityType.function &&
- mathTypeBefore != MathEntityType.open_group_symbol)) {
+ if (mathTypeBefore == MathType.constant || (mathTypeBefore != MathType.binary_operation &&
+ mathTypeBefore != MathType.unary_operation &&
+ mathTypeBefore != MathType.function &&
+ mathTypeBefore != MathType.open_group_symbol)) {
- if (mathType == MathEntityType.constant) {
+ if (mathType == MathType.constant) {
sb.append("*");
- } else if (mathType == MathEntityType.open_group_symbol && mathTypeBefore != null) {
+ } else if (mathType == MathType.open_group_symbol && mathTypeBefore != null) {
sb.append("*");
- } else if (mathType == MathEntityType.digit && ((mathTypeBefore != MathEntityType.digit && mathTypeBefore != MathEntityType.dot) || mathTypeBefore == MathEntityType.constant)) {
+ } else if (mathType == MathType.digit && ((mathTypeBefore != MathType.digit && mathTypeBefore != MathType.dot) || mathTypeBefore == MathType.constant)) {
sb.append("*");
} else {
- for (String function : MathEntityType.prefixFunctions) {
+ for (String function : MathType.prefixFunctions) {
if (s.startsWith(function, i)) {
sb.append("*");
break;
diff --git a/src/main/java/org/solovyev/android/calculator/Var.java b/src/main/java/org/solovyev/android/calculator/model/Var.java
similarity index 98%
rename from src/main/java/org/solovyev/android/calculator/Var.java
rename to src/main/java/org/solovyev/android/calculator/model/Var.java
index dd0d3d56..8abe5137 100644
--- a/src/main/java/org/solovyev/android/calculator/Var.java
+++ b/src/main/java/org/solovyev/android/calculator/model/Var.java
@@ -4,7 +4,7 @@
* or visit http://se.solovyev.org
*/
-package org.solovyev.android.calculator;
+package org.solovyev.android.calculator.model;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
diff --git a/src/main/java/org/solovyev/android/calculator/Vars.java b/src/main/java/org/solovyev/android/calculator/model/Vars.java
similarity index 88%
rename from src/main/java/org/solovyev/android/calculator/Vars.java
rename to src/main/java/org/solovyev/android/calculator/model/Vars.java
index fbc8cc25..9e7b866a 100644
--- a/src/main/java/org/solovyev/android/calculator/Vars.java
+++ b/src/main/java/org/solovyev/android/calculator/model/Vars.java
@@ -1,4 +1,4 @@
-package org.solovyev.android.calculator;
+package org.solovyev.android.calculator.model;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
diff --git a/src/main/java/org/solovyev/android/calculator/model/VarsRegister.java b/src/main/java/org/solovyev/android/calculator/model/VarsRegister.java
new file mode 100644
index 00000000..27a9aa02
--- /dev/null
+++ b/src/main/java/org/solovyev/android/calculator/model/VarsRegister.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2009-2011. Created by serso aka se.solovyev.
+ * For more information, please, contact se.solovyev@gmail.com
+ * or visit http://se.solovyev.org
+ */
+
+package org.solovyev.android.calculator.model;
+
+import android.content.Context;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.List;
+
+/**
+ * User: serso
+ * Date: 10/6/11
+ * Time: 9:31 PM
+ */
+public interface VarsRegister {
+
+ @NotNull
+ List getVars();
+
+ @NotNull
+ List getSystemVars();
+
+ Var addVar(@Nullable String name, @NotNull Var.Builder builder);
+
+ void remove(@NotNull Var var);
+
+ @NotNull
+ List getVarNames();
+
+ @Nullable
+ Var getVar(@NotNull String name);
+
+ boolean contains(@NotNull String name);
+
+ void save(@NotNull Context context);
+}
diff --git a/src/main/java/org/solovyev/android/calculator/VarsRegister.java b/src/main/java/org/solovyev/android/calculator/model/VarsRegisterImpl.java
similarity index 76%
rename from src/main/java/org/solovyev/android/calculator/VarsRegister.java
rename to src/main/java/org/solovyev/android/calculator/model/VarsRegisterImpl.java
index 1962aab2..c07847fc 100644
--- a/src/main/java/org/solovyev/android/calculator/VarsRegister.java
+++ b/src/main/java/org/solovyev/android/calculator/model/VarsRegisterImpl.java
@@ -4,7 +4,7 @@
* or visit http://se.solovyev.org
*/
-package org.solovyev.android.calculator;
+package org.solovyev.android.calculator.model;
import android.content.Context;
import android.content.SharedPreferences;
@@ -13,8 +13,9 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
+import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.math.MathEntityComparator;
-import org.solovyev.android.calculator.math.MathEntityType;
+import org.solovyev.android.calculator.math.MathType;
import org.solovyev.common.utils.CollectionsUtils;
import org.solovyev.common.utils.Finder;
@@ -26,7 +27,7 @@ import java.util.*;
* Date: 9/29/11
* Time: 4:57 PM
*/
-public class VarsRegister {
+class VarsRegisterImpl implements VarsRegister {
@NotNull
private final List vars = new ArrayList();
@@ -34,16 +35,22 @@ public class VarsRegister {
@NotNull
private final List systemVars = new ArrayList();
+ protected VarsRegisterImpl() {
+ }
+
+ @Override
@NotNull
public List getVars() {
return Collections.unmodifiableList(vars);
}
+ @Override
@NotNull
public List getSystemVars() {
return Collections.unmodifiableList(systemVars);
}
+ @Override
public Var addVar(@Nullable String name, @NotNull Var.Builder builder) {
final Var var = builder.create();
@@ -58,12 +65,14 @@ public class VarsRegister {
return varFromRegister;
}
- public void remove (@NotNull Var var) {
+ @Override
+ public void remove(@NotNull Var var) {
this.vars.remove(var);
}
+ @Override
@NotNull
- public List getVarNames () {
+ public List getVarNames() {
final List result = new ArrayList();
for (Var var : vars) {
@@ -75,6 +84,7 @@ public class VarsRegister {
return result;
}
+ @Override
@Nullable
public Var getVar(@NotNull final String name) {
return CollectionsUtils.get(vars, new Finder() {
@@ -85,6 +95,7 @@ public class VarsRegister {
});
}
+ @Override
public boolean contains(@NotNull final String name) {
return CollectionsUtils.get(vars, new Finder() {
@Override
@@ -107,7 +118,7 @@ public class VarsRegister {
vars.addAll(result);
}
- public synchronized void load(@Nullable Context context) {
+ synchronized void init(@Nullable Context context) {
this.vars.clear();
this.systemVars.clear();
@@ -128,19 +139,32 @@ public class VarsRegister {
}
- for (String systemVarName : MathEntityType.constants) {
+ for (String systemVarName : MathType.constants) {
- final Var systemVar;
- if ( systemVarName.equals("e") ){
- systemVar = new Var.Builder(systemVarName, Math.E).setSystem(true).create();
- } else if (systemVarName.equals("π")) {
- systemVar = new Var.Builder(systemVarName, Math.PI).setSystem(true).create();
- } else if (systemVarName.equals("i")) {
- systemVar = new Var.Builder(systemVarName, "sqrt(-1)").setSystem(true).create();
+ final Var.Builder builder;
+ final Integer varDescription;
+
+ if ( systemVarName.equals(MathType.E) ){
+ builder = new Var.Builder(systemVarName, Math.E);
+ varDescription = R.string.c_e_description;
+ } else if (systemVarName.equals(MathType.PI)) {
+ builder = new Var.Builder(systemVarName, Math.PI);
+ varDescription = R.string.c_pi_description;
+ } else if (systemVarName.equals(MathType.IMAGINARY_NUMBER)) {
+ builder = new Var.Builder(systemVarName, MathType.IMAGINARY_NUMBER_DEF);
+ varDescription = R.string.c_i_description;
} else {
throw new IllegalArgumentException(systemVarName + " is not supported yet!");
}
+ builder.setSystem(true);
+
+ if (context != null) {
+ builder.setDescription(context.getString(varDescription));
+ }
+
+ final Var systemVar = builder.create();
+
systemVars.add(systemVar);
if (!vars.contains(systemVar)) {
vars.add(systemVar);
@@ -153,6 +177,7 @@ public class VarsRegister {
}*/
}
+ @Override
public synchronized void save(@NotNull Context context) {
final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
final SharedPreferences.Editor editor = settings.edit();
diff --git a/src/main/java/org/solovyev/android/view/AutoResizeTextView.java b/src/main/java/org/solovyev/android/view/AutoResizeTextView.java
index 207870a0..b64e1c97 100644
--- a/src/main/java/org/solovyev/android/view/AutoResizeTextView.java
+++ b/src/main/java/org/solovyev/android/view/AutoResizeTextView.java
@@ -80,12 +80,12 @@ public class AutoResizeTextView extends TextView {
}
/**
- * When text changes, set the force resize flag to true and reset the text size.
+ * When text changes, set the force resize flag to true and resetInterpreter the text size.
*/
@Override
protected void onTextChanged(final CharSequence text, final int start, final int before, final int after) {
mNeedsResize = true;
- // Since this view may be reused, it is good to reset the text size
+ // Since this view may be reused, it is good to resetInterpreter the text size
resetTextSize();
}
diff --git a/src/test/java/org/solovyev/android/calculator/math/MathEntityTypeTest.java b/src/test/java/org/solovyev/android/calculator/math/MathEntityTypeTest.java
deleted file mode 100644
index de91f9bf..00000000
--- a/src/test/java/org/solovyev/android/calculator/math/MathEntityTypeTest.java
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * Copyright (c) 2009-2011. Created by serso aka se.solovyev.
- * For more information, please, contact se.solovyev@gmail.com
- * or visit http://se.solovyev.org
- */
-
-package org.solovyev.android.calculator.math;
-
-import junit.framework.Assert;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.solovyev.android.calculator.CalculatorModel;
-
-/**
- * User: serso
- * Date: 10/5/11
- * Time: 1:25 AM
- */
-public class MathEntityTypeTest {
-
- @BeforeClass
- public static void setUp() throws Exception {
- CalculatorModel.init(null);
- }
-
- @Test
- public void testGetType() throws Exception {
- Assert.assertEquals(MathEntityType.function, MathEntityType.getType("sin", 0).getMathEntityType());
- Assert.assertEquals(MathEntityType.text, MathEntityType.getType("sn", 0).getMathEntityType());
- Assert.assertEquals(MathEntityType.text, MathEntityType.getType("s", 0).getMathEntityType());
- Assert.assertEquals(MathEntityType.text, MathEntityType.getType("", 0).getMathEntityType());
-
- try {
- Assert.assertEquals(MathEntityType.text, MathEntityType.getType("22", -1).getMathEntityType());
- Assert.fail();
- } catch (IllegalArgumentException e) {
- }
-
- try {
- Assert.assertEquals(MathEntityType.text, MathEntityType.getType("22", 2).getMathEntityType());
- Assert.fail();
- } catch (IllegalArgumentException e) {
- }
- }
-}
diff --git a/src/test/java/org/solovyev/android/calculator/math/MathTypeTest.java b/src/test/java/org/solovyev/android/calculator/math/MathTypeTest.java
new file mode 100644
index 00000000..9d128782
--- /dev/null
+++ b/src/test/java/org/solovyev/android/calculator/math/MathTypeTest.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2009-2011. Created by serso aka se.solovyev.
+ * For more information, please, contact se.solovyev@gmail.com
+ * or visit http://se.solovyev.org
+ */
+
+package org.solovyev.android.calculator.math;
+
+import junit.framework.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.solovyev.android.calculator.model.CalculatorModel;
+
+/**
+ * User: serso
+ * Date: 10/5/11
+ * Time: 1:25 AM
+ */
+public class MathTypeTest {
+
+ @BeforeClass
+ public static void setUp() throws Exception {
+ CalculatorModel.instance.init(null);
+ }
+
+ @Test
+ public void testGetType() throws Exception {
+ Assert.assertEquals(MathType.function, MathType.getType("sin", 0).getMathType());
+ Assert.assertEquals(MathType.text, MathType.getType("sn", 0).getMathType());
+ Assert.assertEquals(MathType.text, MathType.getType("s", 0).getMathType());
+ Assert.assertEquals(MathType.text, MathType.getType("", 0).getMathType());
+
+ try {
+ Assert.assertEquals(MathType.text, MathType.getType("22", -1).getMathType());
+ Assert.fail();
+ } catch (IllegalArgumentException e) {
+ }
+
+ try {
+ Assert.assertEquals(MathType.text, MathType.getType("22", 2).getMathType());
+ Assert.fail();
+ } catch (IllegalArgumentException e) {
+ }
+ }
+}
diff --git a/src/test/java/org/solovyev/android/calculator/CalculatorModelTest.java b/src/test/java/org/solovyev/android/calculator/model/CalculatorModelTest.java
similarity index 84%
rename from src/test/java/org/solovyev/android/calculator/CalculatorModelTest.java
rename to src/test/java/org/solovyev/android/calculator/model/CalculatorModelTest.java
index d11cb326..a069cc51 100644
--- a/src/test/java/org/solovyev/android/calculator/CalculatorModelTest.java
+++ b/src/test/java/org/solovyev/android/calculator/model/CalculatorModelTest.java
@@ -3,12 +3,13 @@
* For more information, please, contact se.solovyev@gmail.com
*/
-package org.solovyev.android.calculator;
+package org.solovyev.android.calculator.model;
import bsh.EvalError;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
+import org.solovyev.android.calculator.JsclOperation;
/**
* User: serso
@@ -19,12 +20,12 @@ public class CalculatorModelTest {
@BeforeClass
public static void setUp() throws Exception {
- CalculatorModel.init(null);
+ CalculatorModel.instance.init(null);
}
@Test
public void testEvaluate() throws Exception {
- final CalculatorModel cm = CalculatorModel.getInstance();
+ final CalculatorModel cm = CalculatorModel.instance;
Assert.assertEquals("4.0", cm.evaluate(JsclOperation.numeric, "2+2"));
Assert.assertEquals("-0.7568", cm.evaluate(JsclOperation.numeric, "sin(4)"));
@@ -47,7 +48,7 @@ public class CalculatorModelTest {
Assert.assertEquals("-3.41007+3.41007i", cm.evaluate(JsclOperation.numeric, "(5tan(2i)+2i)/(1-i)"));
Assert.assertEquals("-0.1-0.2i", cm.evaluate(JsclOperation.numeric, "(1-i)/(2+6i)"));
- CalculatorModel.getInstance().getVarsRegister().addVar(null, new Var.Builder("si", 5d));
+ CalculatorModel.instance.getVarsRegister().addVar(null, new Var.Builder("si", 5d));
Assert.assertEquals("5.0", cm.evaluate(JsclOperation.numeric, "si"));
try {
cm.evaluate(JsclOperation.numeric, "sin");
@@ -60,17 +61,17 @@ public class CalculatorModelTest {
Assert.assertEquals("-23.97311", cm.evaluate(JsclOperation.numeric, "si*sin(5)si"));
Assert.assertEquals("-3.30879", cm.evaluate(JsclOperation.numeric, "sisin(5si)si"));
- CalculatorModel.getInstance().getVarsRegister().addVar(null, new Var.Builder("s", 1d));
+ CalculatorModel.instance.getVarsRegister().addVar(null, new Var.Builder("s", 1d));
Assert.assertEquals("5.0", cm.evaluate(JsclOperation.numeric, "si"));
- CalculatorModel.getInstance().getVarsRegister().addVar(null, new Var.Builder("k", 3.5d));
- CalculatorModel.getInstance().getVarsRegister().addVar(null, new Var.Builder("k1", 4d));
+ CalculatorModel.instance.getVarsRegister().addVar(null, new Var.Builder("k", 3.5d));
+ CalculatorModel.instance.getVarsRegister().addVar(null, new Var.Builder("k1", 4d));
Assert.assertEquals("4.0", cm.evaluate(JsclOperation.numeric, "k11"));
}
@Test
public void testComplexNumbers() throws Exception {
- final CalculatorModel cm = CalculatorModel.getInstance();
+ final FromJsclTextProcessor cm = new FromJsclTextProcessor();
Assert.assertEquals("1.22133+23123.0i", cm.createResultForComplexNumber("1.22133232+23123*i"));
Assert.assertEquals("1.22133+1.2i", cm.createResultForComplexNumber("1.22133232+1.2*i"));
diff --git a/src/test/java/org/solovyev/android/calculator/ToJsclPreprocessorTest.java b/src/test/java/org/solovyev/android/calculator/model/ToJsclPreprocessorTest.java
similarity index 92%
rename from src/test/java/org/solovyev/android/calculator/ToJsclPreprocessorTest.java
rename to src/test/java/org/solovyev/android/calculator/model/ToJsclPreprocessorTest.java
index ac878822..977841dd 100644
--- a/src/test/java/org/solovyev/android/calculator/ToJsclPreprocessorTest.java
+++ b/src/test/java/org/solovyev/android/calculator/model/ToJsclPreprocessorTest.java
@@ -4,7 +4,7 @@
* or visit http://se.solovyev.org
*/
-package org.solovyev.android.calculator;
+package org.solovyev.android.calculator.model;
import org.junit.Assert;
import org.junit.Test;
@@ -18,7 +18,7 @@ public class ToJsclPreprocessorTest {
@Test
public void testProcess() throws Exception {
- final ToJsclPreprocessor preprocessor = new ToJsclPreprocessor();
+ final ToJsclTextProcessor preprocessor = new ToJsclTextProcessor();
Assert.assertEquals( "sin(4)*cos(5)", preprocessor.process("sin(4)cos(5)"));
Assert.assertEquals( "pi*sin(4)*pi*cos(sqrt(5))", preprocessor.process("πsin(4)πcos(√(5))"));
@@ -30,7 +30,7 @@ public class ToJsclPreprocessorTest {
@Test
public void testPostfixFunctionsProcessing() throws Exception {
- final ToJsclPreprocessor preprocessor = new ToJsclPreprocessor();
+ final ToJsclTextProcessor preprocessor = new ToJsclTextProcessor();
Assert.assertEquals(-1, preprocessor.getPostfixFunctionStart("5!", 0));
Assert.assertEquals(0, preprocessor.getPostfixFunctionStart("!", 0));