This commit is contained in:
serso 2011-10-17 23:53:50 +04:00
parent 2e67a00b46
commit 8305b7ed10
10 changed files with 33 additions and 33 deletions

View File

@ -23,7 +23,7 @@ import android.widget.TextView;
import bsh.EvalError; import bsh.EvalError;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.model.CalculatorModel; import org.solovyev.android.calculator.model.CalculatorEngine;
import org.solovyev.android.view.FontSizeAdjuster; import org.solovyev.android.view.FontSizeAdjuster;
import org.solovyev.android.view.widgets.*; import org.solovyev.android.view.widgets.*;
import org.solovyev.common.BooleanMapper; import org.solovyev.common.BooleanMapper;
@ -73,7 +73,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
firstTimeInit(preferences); firstTimeInit(preferences);
calculatorView = CalculatorView.instance.init(this, preferences, CalculatorModel.instance); calculatorView = CalculatorView.instance.init(this, preferences, CalculatorEngine.instance);
dpclRegister.clear(); dpclRegister.clear();
@ -90,7 +90,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
((DragButton) findViewById(R.id.leftButton)).setOnDragListener(toPositionOnDragListener); ((DragButton) findViewById(R.id.leftButton)).setOnDragListener(toPositionOnDragListener);
dpclRegister.addListener(toPositionOnDragListener); dpclRegister.addListener(toPositionOnDragListener);
CalculatorModel.instance.reset(this, preferences); CalculatorEngine.instance.reset(this, preferences);
preferences.registerOnSharedPreferenceChangeListener(this); preferences.registerOnSharedPreferenceChangeListener(this);
} }
@ -157,7 +157,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
private synchronized void firstTimeInit(@NotNull SharedPreferences preferences) { private synchronized void firstTimeInit(@NotNull SharedPreferences preferences) {
if (!initialized) { if (!initialized) {
try { try {
CalculatorModel.instance.init(this, preferences); CalculatorEngine.instance.init(this, preferences);
} catch (EvalError evalError) { } catch (EvalError evalError) {
throw new RuntimeException("Could not initialize interpreter!"); throw new RuntimeException("Could not initialize interpreter!");
} }
@ -362,7 +362,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
restart(); restart();
} }
calculatorView = CalculatorView.instance.init(this, preferences, CalculatorModel.instance); calculatorView = CalculatorView.instance.init(this, preferences, CalculatorEngine.instance);
this.calculatorView.evaluate(); this.calculatorView.evaluate();
} }
@ -371,7 +371,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String s) { public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String s) {
dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(preferences, this)); dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(preferences, this));
CalculatorModel.instance.reset(this, preferences); CalculatorEngine.instance.reset(this, preferences);
final Boolean colorExpressionsInBracketsDefault = new BooleanMapper().parseValue(this.getString(R.string.p_calc_color_display)); final Boolean colorExpressionsInBracketsDefault = new BooleanMapper().parseValue(this.getString(R.string.p_calc_color_display));
assert colorExpressionsInBracketsDefault != null; assert colorExpressionsInBracketsDefault != null;

View File

@ -18,7 +18,7 @@ import android.widget.*;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.calculator.model.CalculatorModel; import org.solovyev.android.calculator.model.CalculatorEngine;
import org.solovyev.android.calculator.model.Var; import org.solovyev.android.calculator.model.Var;
import org.solovyev.android.calculator.model.VarsRegister; import org.solovyev.android.calculator.model.VarsRegister;
import org.solovyev.common.utils.StringUtils; import org.solovyev.common.utils.StringUtils;
@ -45,7 +45,7 @@ public class CalculatorVarsActivity extends ListActivity {
setContentView(R.layout.vars); setContentView(R.layout.vars);
adapter = new VarsArrayAdapter(this, R.layout.var, R.id.var_text, new ArrayList<Var>(CalculatorModel.instance.getVarsRegister().getVars())); adapter = new VarsArrayAdapter(this, R.layout.var, R.id.var_text, new ArrayList<Var>(CalculatorEngine.instance.getVarsRegister().getVars()));
setListAdapter(adapter); setListAdapter(adapter);
final ListView lv = getListView(); final ListView lv = getListView();
@ -191,7 +191,7 @@ public class CalculatorVarsActivity extends ListActivity {
String description = editDescription.getText().toString(); String description = editDescription.getText().toString();
final VarsRegister varsRegister = CalculatorModel.instance.getVarsRegister(); final VarsRegister varsRegister = CalculatorEngine.instance.getVarsRegister();
if (!StringUtils.isEmpty(name)) { if (!StringUtils.isEmpty(name)) {
final Var varFromRegister = varsRegister.getVar(name); final Var varFromRegister = varsRegister.getVar(name);
if (varFromRegister == null || varFromRegister == editedInstance) { if (varFromRegister == null || varFromRegister == editedInstance) {
@ -334,7 +334,7 @@ public class CalculatorVarsActivity extends ListActivity {
builder.create().show(); builder.create().show();
} else { } else {
adapter.remove(var); adapter.remove(var);
final VarsRegister varsRegister = CalculatorModel.instance.getVarsRegister(); final VarsRegister varsRegister = CalculatorEngine.instance.getVarsRegister();
varsRegister.remove(var); varsRegister.remove(var);
varsRegister.save(CalculatorVarsActivity.this); varsRegister.save(CalculatorVarsActivity.this);
CalculatorVarsActivity.this.adapter.notifyDataSetChanged(); CalculatorVarsActivity.this.adapter.notifyDataSetChanged();

View File

@ -19,7 +19,7 @@ import bsh.EvalError;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.calculator.model.CalculatorModel; import org.solovyev.android.calculator.model.CalculatorEngine;
import org.solovyev.android.calculator.model.ParseException; import org.solovyev.android.calculator.model.ParseException;
import org.solovyev.android.view.CursorControl; import org.solovyev.android.view.CursorControl;
import org.solovyev.android.view.HistoryControl; import org.solovyev.android.view.HistoryControl;
@ -47,10 +47,10 @@ public enum CalculatorView implements CursorControl, HistoryControl<CalculatorHi
private CalculatorDisplay display; private CalculatorDisplay display;
@NotNull @NotNull
private CalculatorModel calculatorModel; private CalculatorEngine calculatorEngine;
public CalculatorView init(@NotNull final Activity activity, @NotNull SharedPreferences preferences, @NotNull CalculatorModel calculator) { public CalculatorView init(@NotNull final Activity activity, @NotNull SharedPreferences preferences, @NotNull CalculatorEngine calculator) {
this.calculatorModel = calculator; this.calculatorEngine = calculator;
this.editor = (CalculatorEditor) activity.findViewById(R.id.calculatorEditor); this.editor = (CalculatorEditor) activity.findViewById(R.id.calculatorEditor);
@ -174,7 +174,7 @@ public enum CalculatorView implements CursorControl, HistoryControl<CalculatorHi
if (!StringUtils.isEmpty(expression)) { if (!StringUtils.isEmpty(expression)) {
try { try {
Log.d(CalculatorView.class.getName(), "Trying to evaluate: " + expression /*+ StringUtils.fromStackTrace(Thread.currentThread().getStackTrace())*/); Log.d(CalculatorView.class.getName(), "Trying to evaluate: " + expression /*+ StringUtils.fromStackTrace(Thread.currentThread().getStackTrace())*/);
localDisplay.setText(calculatorModel.evaluate(JsclOperation.numeric, expression)); localDisplay.setText(calculatorEngine.evaluate(JsclOperation.numeric, expression));
} catch (EvalError e) { } catch (EvalError e) {
handleEvaluationException(expression, localDisplay, e); handleEvaluationException(expression, localDisplay, e);
} catch (ParseException e) { } catch (ParseException e) {

View File

@ -8,7 +8,7 @@ package org.solovyev.android.calculator.math;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.CharacterAtPositionFinder; import org.solovyev.android.calculator.CharacterAtPositionFinder;
import org.solovyev.android.calculator.StartsWithFinder; import org.solovyev.android.calculator.StartsWithFinder;
import org.solovyev.android.calculator.model.CalculatorModel; import org.solovyev.android.calculator.model.CalculatorEngine;
import org.solovyev.common.utils.Finder; import org.solovyev.common.utils.Finder;
import java.util.Arrays; import java.util.Arrays;
@ -123,7 +123,7 @@ public enum MathType {
return new Result(MathType.function, foundString); return new Result(MathType.function, foundString);
} }
foundString = get(CalculatorModel.instance.getVarsRegister().getVarNames(), stringStartWithFinder); foundString = get(CalculatorEngine.instance.getVarsRegister().getVarNames(), stringStartWithFinder);
if (foundString != null) { if (foundString != null) {
return new Result(MathType.constant, foundString); return new Result(MathType.constant, foundString);
} }

View File

@ -20,7 +20,7 @@ import org.solovyev.common.NumberMapper;
* Time: 11:38 PM * Time: 11:38 PM
*/ */
public enum CalculatorModel { public enum CalculatorEngine {
instance; instance;
@ -50,7 +50,7 @@ public enum CalculatorModel {
sb.append(preprocessor.process(expression)); sb.append(preprocessor.process(expression));
//Log.d(CalculatorModel.class.getName(), "Preprocessed expression: " + preprocessedExpression); //Log.d(CalculatorEngine.class.getName(), "Preprocessed expression: " + preprocessedExpression);
final Object evaluationObject = interpreter.eval(ToJsclTextProcessor.wrap(operation, sb.toString())); final Object evaluationObject = interpreter.eval(ToJsclTextProcessor.wrap(operation, sb.toString()));

View File

@ -80,6 +80,6 @@ class FromJsclTextProcessor implements TextProcessor {
private Double round(@NotNull String result) { private Double round(@NotNull String result) {
final Double dResult = Double.valueOf(result); final Double dResult = Double.valueOf(result);
return MathUtils.round(dResult, CalculatorModel.instance.getNumberOfFractionDigits()); return MathUtils.round(dResult, CalculatorEngine.instance.getNumberOfFractionDigits());
} }
} }

View File

@ -71,9 +71,9 @@ class ToJsclTextProcessor implements TextProcessor {
int offset = 0; int offset = 0;
String functionName = CollectionsUtils.get(MathType.prefixFunctions, startsWithFinder); String functionName = CollectionsUtils.get(MathType.prefixFunctions, startsWithFinder);
if (functionName == null) { if (functionName == null) {
String varName = CollectionsUtils.get(CalculatorModel.instance.getVarsRegister().getVarNames(), startsWithFinder); String varName = CollectionsUtils.get(CalculatorEngine.instance.getVarsRegister().getVarNames(), startsWithFinder);
if (varName != null) { if (varName != null) {
final Var var = CalculatorModel.instance.getVarsRegister().getVar(varName); final Var var = CalculatorEngine.instance.getVarsRegister().getVar(varName);
if (var != null) { if (var != null) {
result.append(var.getValue()); result.append(var.getValue());
offset = varName.length(); offset = varName.length();
@ -96,7 +96,7 @@ class ToJsclTextProcessor implements TextProcessor {
} }
private void replaceVariables(StringBuilder sb, String s, int i, @NotNull StartsWithFinder startsWithFinder) { private void replaceVariables(StringBuilder sb, String s, int i, @NotNull StartsWithFinder startsWithFinder) {
for (Var var : CalculatorModel.instance.getVarsRegister().getVars()) { for (Var var : CalculatorEngine.instance.getVarsRegister().getVars()) {
if (!var.isSystem()) { if (!var.isSystem()) {
if (s.startsWith(var.getName(), i)) { if (s.startsWith(var.getName(), i)) {
if (CollectionsUtils.get(MathType.prefixFunctions, startsWithFinder) == null) { if (CollectionsUtils.get(MathType.prefixFunctions, startsWithFinder) == null) {

View File

@ -9,7 +9,7 @@ package org.solovyev.android.calculator.math;
import junit.framework.Assert; import junit.framework.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.solovyev.android.calculator.model.CalculatorModel; import org.solovyev.android.calculator.model.CalculatorEngine;
/** /**
* User: serso * User: serso
@ -20,7 +20,7 @@ public class MathTypeTest {
@BeforeClass @BeforeClass
public static void setUp() throws Exception { public static void setUp() throws Exception {
CalculatorModel.instance.init(null, null); CalculatorEngine.instance.init(null, null);
} }
@Test @Test

View File

@ -17,16 +17,16 @@ import org.solovyev.android.calculator.JsclOperation;
* Date: 9/17/11 * Date: 9/17/11
* Time: 9:47 PM * Time: 9:47 PM
*/ */
public class CalculatorModelTest { public class CalculatorEngineTest {
@BeforeClass @BeforeClass
public static void setUp() throws Exception { public static void setUp() throws Exception {
CalculatorModel.instance.init(null, null); CalculatorEngine.instance.init(null, null);
} }
@Test @Test
public void testEvaluate() throws Exception { public void testEvaluate() throws Exception {
final CalculatorModel cm = CalculatorModel.instance; final CalculatorEngine cm = CalculatorEngine.instance;
Assert.assertEquals("4.0", cm.evaluate(JsclOperation.numeric, "2+2")); Assert.assertEquals("4.0", cm.evaluate(JsclOperation.numeric, "2+2"));
Assert.assertEquals("-0.7568", cm.evaluate(JsclOperation.numeric, "sin(4)")); Assert.assertEquals("-0.7568", cm.evaluate(JsclOperation.numeric, "sin(4)"));
@ -62,7 +62,7 @@ public class CalculatorModelTest {
} }
Assert.assertEquals("0.73909", cm.evaluate(JsclOperation.numeric, "cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(1))))))))))))))))))))))))))))))))))))")); Assert.assertEquals("0.73909", cm.evaluate(JsclOperation.numeric, "cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(1))))))))))))))))))))))))))))))))))))"));
CalculatorModel.instance.getVarsRegister().addVar(null, new Var.Builder("si", 5d)); CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("si", 5d));
Assert.assertEquals("5.0", cm.evaluate(JsclOperation.numeric, "si")); Assert.assertEquals("5.0", cm.evaluate(JsclOperation.numeric, "si"));
try { try {
cm.evaluate(JsclOperation.numeric, "sin"); cm.evaluate(JsclOperation.numeric, "sin");
@ -75,11 +75,11 @@ public class CalculatorModelTest {
Assert.assertEquals("-23.97311", cm.evaluate(JsclOperation.numeric, "si*sin(5)si")); Assert.assertEquals("-23.97311", cm.evaluate(JsclOperation.numeric, "si*sin(5)si"));
Assert.assertEquals("-3.30879", cm.evaluate(JsclOperation.numeric, "sisin(5si)si")); Assert.assertEquals("-3.30879", cm.evaluate(JsclOperation.numeric, "sisin(5si)si"));
CalculatorModel.instance.getVarsRegister().addVar(null, new Var.Builder("s", 1d)); CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("s", 1d));
Assert.assertEquals("5.0", cm.evaluate(JsclOperation.numeric, "si")); Assert.assertEquals("5.0", cm.evaluate(JsclOperation.numeric, "si"));
CalculatorModel.instance.getVarsRegister().addVar(null, new Var.Builder("k", 3.5d)); CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("k", 3.5d));
CalculatorModel.instance.getVarsRegister().addVar(null, new Var.Builder("k1", 4d)); CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("k1", 4d));
Assert.assertEquals("4.0", cm.evaluate(JsclOperation.numeric, "k11")); Assert.assertEquals("4.0", cm.evaluate(JsclOperation.numeric, "k11"));
} }

View File

@ -19,7 +19,7 @@ public class ToJsclTextProcessorTest {
@BeforeClass @BeforeClass
public static void setUp() throws Exception { public static void setUp() throws Exception {
CalculatorModel.instance.init(null, null); CalculatorEngine.instance.init(null, null);
} }
@Test @Test