This commit is contained in:
serso 2011-10-17 23:53:50 +04:00
parent 14f8abab81
commit 5d0833e35f
10 changed files with 33 additions and 33 deletions

View File

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

View File

@ -18,7 +18,7 @@ import android.widget.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
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.VarsRegister;
import org.solovyev.common.utils.StringUtils;
@ -45,7 +45,7 @@ public class CalculatorVarsActivity extends ListActivity {
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);
final ListView lv = getListView();
@ -191,7 +191,7 @@ public class CalculatorVarsActivity extends ListActivity {
String description = editDescription.getText().toString();
final VarsRegister varsRegister = CalculatorModel.instance.getVarsRegister();
final VarsRegister varsRegister = CalculatorEngine.instance.getVarsRegister();
if (!StringUtils.isEmpty(name)) {
final Var varFromRegister = varsRegister.getVar(name);
if (varFromRegister == null || varFromRegister == editedInstance) {
@ -334,7 +334,7 @@ public class CalculatorVarsActivity extends ListActivity {
builder.create().show();
} else {
adapter.remove(var);
final VarsRegister varsRegister = CalculatorModel.instance.getVarsRegister();
final VarsRegister varsRegister = CalculatorEngine.instance.getVarsRegister();
varsRegister.remove(var);
varsRegister.save(CalculatorVarsActivity.this);
CalculatorVarsActivity.this.adapter.notifyDataSetChanged();

View File

@ -19,7 +19,7 @@ import bsh.EvalError;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
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.view.CursorControl;
import org.solovyev.android.view.HistoryControl;
@ -47,10 +47,10 @@ public enum CalculatorView implements CursorControl, HistoryControl<CalculatorHi
private CalculatorDisplay display;
@NotNull
private CalculatorModel calculatorModel;
private CalculatorEngine calculatorEngine;
public CalculatorView init(@NotNull final Activity activity, @NotNull SharedPreferences preferences, @NotNull CalculatorModel calculator) {
this.calculatorModel = calculator;
public CalculatorView init(@NotNull final Activity activity, @NotNull SharedPreferences preferences, @NotNull CalculatorEngine calculator) {
this.calculatorEngine = calculator;
this.editor = (CalculatorEditor) activity.findViewById(R.id.calculatorEditor);
@ -174,7 +174,7 @@ public enum CalculatorView implements CursorControl, HistoryControl<CalculatorHi
if (!StringUtils.isEmpty(expression)) {
try {
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) {
handleEvaluationException(expression, localDisplay, e);
} catch (ParseException e) {

View File

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

View File

@ -20,7 +20,7 @@ import org.solovyev.common.NumberMapper;
* Time: 11:38 PM
*/
public enum CalculatorModel {
public enum CalculatorEngine {
instance;
@ -50,7 +50,7 @@ public enum CalculatorModel {
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()));

View File

@ -80,6 +80,6 @@ class FromJsclTextProcessor implements TextProcessor {
private Double round(@NotNull String 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;
String functionName = CollectionsUtils.get(MathType.prefixFunctions, startsWithFinder);
if (functionName == null) {
String varName = CollectionsUtils.get(CalculatorModel.instance.getVarsRegister().getVarNames(), startsWithFinder);
String varName = CollectionsUtils.get(CalculatorEngine.instance.getVarsRegister().getVarNames(), startsWithFinder);
if (varName != null) {
final Var var = CalculatorModel.instance.getVarsRegister().getVar(varName);
final Var var = CalculatorEngine.instance.getVarsRegister().getVar(varName);
if (var != null) {
result.append(var.getValue());
offset = varName.length();
@ -96,7 +96,7 @@ class ToJsclTextProcessor implements TextProcessor {
}
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 (s.startsWith(var.getName(), i)) {
if (CollectionsUtils.get(MathType.prefixFunctions, startsWithFinder) == null) {

View File

@ -9,7 +9,7 @@ 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;
import org.solovyev.android.calculator.model.CalculatorEngine;
/**
* User: serso
@ -20,7 +20,7 @@ public class MathTypeTest {
@BeforeClass
public static void setUp() throws Exception {
CalculatorModel.instance.init(null, null);
CalculatorEngine.instance.init(null, null);
}
@Test

View File

@ -17,16 +17,16 @@ import org.solovyev.android.calculator.JsclOperation;
* Date: 9/17/11
* Time: 9:47 PM
*/
public class CalculatorModelTest {
public class CalculatorEngineTest {
@BeforeClass
public static void setUp() throws Exception {
CalculatorModel.instance.init(null, null);
CalculatorEngine.instance.init(null, null);
}
@Test
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("-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))))))))))))))))))))))))))))))))))))"));
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"));
try {
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("-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"));
CalculatorModel.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("k", 3.5d));
CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("k1", 4d));
Assert.assertEquals("4.0", cm.evaluate(JsclOperation.numeric, "k11"));
}

View File

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