From fa86989f189544ba46ab681c47021bb61c8f7dc8 Mon Sep 17 00:00:00 2001 From: serso Date: Sat, 12 Nov 2011 14:28:34 +0400 Subject: [PATCH] changes --- .../calculator/CalculatorVarsActivity.java | 15 +- .../android/calculator/math/MathType.java | 6 + .../model/AndroidFunctionsRegistryImpl.java | 4 +- .../model/AndroidVarsRegistryImpl.java | 139 +++++++++--------- .../android/calculator/model/Var.java | 13 +- .../model/CalculatorEngineTest.java | 15 +- .../FromJsclSimplifyTextProcessorTest.java | 7 +- 7 files changed, 115 insertions(+), 84 deletions(-) diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java index 874b08db..f0f42bc5 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java @@ -25,6 +25,8 @@ import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.model.AndroidVarsRegistry; import org.solovyev.android.calculator.model.CalculatorEngine; import org.solovyev.android.calculator.model.Var; +import org.solovyev.common.utils.CollectionsUtils; +import org.solovyev.common.utils.Finder; import org.solovyev.common.utils.StringUtils; import java.util.ArrayList; @@ -52,7 +54,14 @@ public class CalculatorVarsActivity extends ListActivity { setContentView(R.layout.vars); - adapter = new VarsArrayAdapter(this, R.layout.var, R.id.var_text, new ArrayList(CalculatorEngine.instance.getVarsRegister().getEntities())); + final List vars = new ArrayList(CalculatorEngine.instance.getVarsRegister().getEntities()); + CollectionsUtils.removeAll(vars, new Finder() { + @Override + public boolean isFound(@Nullable Var var) { + return var != null && CollectionsUtils.contains(var.getName(), MathType.INFINITY_JSCL, MathType.NAN); + } + }); + adapter = new VarsArrayAdapter(this, R.layout.var, R.id.var_text, vars); setListAdapter(adapter); final ListView lv = getListView(); @@ -261,9 +270,9 @@ public class CalculatorVarsActivity extends ListActivity { createEditVariableDialog(editedInstance, name, value, description); } else { if ( editedInstance == null ) { - CalculatorVarsActivity.this.adapter.add(varsRegistry.add(null, varBuilder)); + CalculatorVarsActivity.this.adapter.add(varsRegistry.add(varBuilder)); } else { - final Var newInstance = varsRegistry.add(editedInstance.getName(), varBuilder); + final Var newInstance = varsRegistry.add(varBuilder); CalculatorVarsActivity.this.adapter.remove(editedInstance); CalculatorVarsActivity.this.adapter.add(newInstance); } diff --git a/src/main/java/org/solovyev/android/calculator/math/MathType.java b/src/main/java/org/solovyev/android/calculator/math/MathType.java index 871899e7..8021c6a1 100644 --- a/src/main/java/org/solovyev/android/calculator/math/MathType.java +++ b/src/main/java/org/solovyev/android/calculator/math/MathType.java @@ -5,6 +5,7 @@ package org.solovyev.android.calculator.math; +import jscl.math.function.Constant; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.StartsWithFinder; @@ -107,6 +108,11 @@ public enum MathType { public List getTokens() { return CalculatorEngine.instance.getVarsRegister().getNames(); } + + @Override + protected String getSubstituteFromJscl(@NotNull String match) { + return Constant.INF_CONST2.getName().equals(match) ? MathType.INFINITY : super.getSubstituteFromJscl(match); + } }, text(1200, false, false) { diff --git a/src/main/java/org/solovyev/android/calculator/model/AndroidFunctionsRegistryImpl.java b/src/main/java/org/solovyev/android/calculator/model/AndroidFunctionsRegistryImpl.java index 356e31f5..b4eb234c 100644 --- a/src/main/java/org/solovyev/android/calculator/model/AndroidFunctionsRegistryImpl.java +++ b/src/main/java/org/solovyev/android/calculator/model/AndroidFunctionsRegistryImpl.java @@ -71,8 +71,8 @@ public class AndroidFunctionsRegistryImpl implements AndroidFunctionsRegistry { } @Override - public Function add(@Nullable String name, @NotNull IBuilder IBuilder) { - return functionsRegistry.add(name, IBuilder); + public Function add(@NotNull IBuilder IBuilder) { + return functionsRegistry.add(IBuilder); } @Override diff --git a/src/main/java/org/solovyev/android/calculator/model/AndroidVarsRegistryImpl.java b/src/main/java/org/solovyev/android/calculator/model/AndroidVarsRegistryImpl.java index 47aa61c4..fd9a3e06 100644 --- a/src/main/java/org/solovyev/android/calculator/model/AndroidVarsRegistryImpl.java +++ b/src/main/java/org/solovyev/android/calculator/model/AndroidVarsRegistryImpl.java @@ -37,79 +37,71 @@ class AndroidVarsRegistryImpl implements AndroidVarsRegistry { this.mathRegistry = mathRegistry; } - private boolean initialized = false; - public synchronized void init(@Nullable Context context, @Nullable SharedPreferences preferences) { - if (!initialized) { - - if (context != null && preferences != null) { - final String value = preferences.getString(context.getString(R.string.p_calc_vars), null); - if (value != null) { - final Serializer serializer = new Persister(); - try { - final Vars vars = serializer.read(Vars.class, value); - for (Var var : vars.getVars()) { - if (!contains(var.getName())) { - add(null, new Var.Builder(var)); - } + if (context != null && preferences != null) { + final String value = preferences.getString(context.getString(R.string.p_calc_vars), null); + if (value != null) { + final Serializer serializer = new Persister(); + try { + final Vars vars = serializer.read(Vars.class, value); + for (Var var : vars.getVars()) { + if (!contains(var.getName())) { + add(new Var.Builder(var)); } - } catch (Exception e) { - throw new RuntimeException(e); } - } - } - - - for (String systemVarName : MathType.constants) { - - 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.C)) { - builder = new Var.Builder(systemVarName, MathType.C_VALUE); - varDescription = R.string.c_c_description; - } else if (systemVarName.equals(MathType.G)) { - builder = new Var.Builder(systemVarName, MathType.G_VALUE); - varDescription = R.string.c_g_description; - /* } else if (systemVarName.equals(MathType.H)) { - builder = new Var.Builder(systemVarName, MathType.H_VALUE); - varDescription = R.string.c_h_description;*/ - } else if (systemVarName.equals(MathType.H_REDUCED)) { - builder = new Var.Builder(systemVarName, MathType.H_REDUCED_VALUE); - varDescription = R.string.c_h_reduced_description; - } else if (systemVarName.equals(MathType.IMAGINARY_NUMBER)) { - builder = new Var.Builder(systemVarName, MathType.IMAGINARY_NUMBER_JSCL); - varDescription = R.string.c_i_description; - } else if (systemVarName.equals(MathType.NAN)) { - builder = new Var.Builder(systemVarName, MathType.NAN); - varDescription = R.string.c_nan_description; - } else if (systemVarName.equals(MathType.INFINITY)) { - builder = new Var.Builder(systemVarName, MathType.INFINITY_JSCL); - varDescription = R.string.c_infinity_description; - } else { - throw new IllegalArgumentException(systemVarName + " is not supported yet!"); - } - - builder.setSystem(true); - - if (context != null) { - builder.setDescription(context.getString(varDescription)); - } - - if (!contains(systemVarName)) { - add(null, builder); + } catch (Exception e) { + throw new RuntimeException(e); } } } - initialized = true; + + for (String systemVarName : MathType.constants) { + + final Var.Builder builder; + final Integer varDescription; + + if (systemVarName.equals(MathType.E)) { + builder = createBuilder(systemVarName, String.valueOf(Math.E)); + varDescription = R.string.c_e_description; + } else if (systemVarName.equals(MathType.PI)) { + builder = createBuilder(systemVarName, String.valueOf(Math.PI)); + varDescription = R.string.c_pi_description; + } else if (systemVarName.equals(MathType.C)) { + builder = createBuilder(systemVarName, String.valueOf(MathType.C_VALUE)); + varDescription = R.string.c_c_description; + } else if (systemVarName.equals(MathType.G)) { + builder = createBuilder(systemVarName, String.valueOf(MathType.G_VALUE)); + varDescription = R.string.c_g_description; + /* } else if (systemVarName.equals(MathType.H)) { + builder = new Var.Builder(systemVarName, MathType.H_VALUE); + varDescription = R.string.c_h_description;*/ + } else if (systemVarName.equals(MathType.H_REDUCED)) { + builder = createBuilder(systemVarName, String.valueOf(MathType.H_REDUCED_VALUE)); + varDescription = R.string.c_h_reduced_description; + } else if (systemVarName.equals(MathType.IMAGINARY_NUMBER)) { + builder = createBuilder(systemVarName, MathType.IMAGINARY_NUMBER_JSCL); + varDescription = R.string.c_i_description; + } else if (systemVarName.equals(MathType.NAN)) { + builder = createBuilder(systemVarName, MathType.NAN); + varDescription = R.string.c_nan_description; + } else if (systemVarName.equals(MathType.INFINITY)) { + builder = createBuilder(systemVarName, MathType.INFINITY_JSCL); + varDescription = R.string.c_infinity_description; + } else { + throw new IllegalArgumentException(systemVarName + " is not supported yet!"); + } + + builder.setSystem(true); + + if (context != null) { + builder.setDescription(context.getString(varDescription)); + } + + add(builder); + } + /*Log.d(AndroidVarsRegistry.class.getName(), vars.size() + " variables registered!"); for (Var var : vars) { @@ -117,6 +109,19 @@ class AndroidVarsRegistryImpl implements AndroidVarsRegistry { }*/ } + private Var.Builder createBuilder(@NotNull String varName, @NotNull String varValue) { + final Var.Builder result; + + final IConstant varFromRegistry = mathRegistry.get(varName); + if (varFromRegistry == null) { + result = new Var.Builder(varName, varValue); + } else { + result = new Var.Builder(varFromRegistry); + } + + return result; + } + @Override public synchronized void save(@NotNull Context context) { final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); @@ -163,8 +168,8 @@ class AndroidVarsRegistryImpl implements AndroidVarsRegistry { } @Override - public Var add(@Nullable String name, @NotNull IBuilder IBuilder) { - IConstant result = mathRegistry.add(name, IBuilder); + public Var add(@NotNull IBuilder IBuilder) { + IConstant result = mathRegistry.add(IBuilder); if (result instanceof Var) { return (Var) result; } else if (result != null) { diff --git a/src/main/java/org/solovyev/android/calculator/model/Var.java b/src/main/java/org/solovyev/android/calculator/model/Var.java index b8051682..bab72b0e 100644 --- a/src/main/java/org/solovyev/android/calculator/model/Var.java +++ b/src/main/java/org/solovyev/android/calculator/model/Var.java @@ -26,7 +26,6 @@ import org.solovyev.common.utils.StringUtils; @Root public class Var implements IConstant { - @NotNull @Transient private Integer id; @@ -82,7 +81,9 @@ public class Var implements IConstant { this.system = iConstant.isSystem(); this.description = iConstant.getDescription(); - this.id = iConstant.getId(); + if (iConstant.isIdDefined()) { + this.id = iConstant.getId(); + } } public Builder(@NotNull String name, @NotNull Double value) { @@ -145,6 +146,9 @@ public class Var implements IConstant { this.value = that.getValue(); this.description = that.getDescription(); this.system = that.isSystem(); + if (that.isIdDefined()) { + this.id = that.getId(); + } } else { throw new IllegalArgumentException("Trying to make a copy of unsupported type: " + o.getClass()); } @@ -184,6 +188,11 @@ public class Var implements IConstant { return this.id; } + @Override + public boolean isIdDefined() { + return this.id != null; + } + @Override public void setId(@NotNull Integer id) { this.id = id; diff --git a/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java b/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java index b8f9670b..3e57e915 100644 --- a/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java +++ b/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java @@ -130,7 +130,7 @@ public class CalculatorEngineTest { } junit.framework.Assert.assertEquals("24i", cm.evaluate(JsclOperation.numeric, "4!i").getResult()); - CalculatorEngine.instance.getVarsRegister().add(null, new Var.Builder("si", 5d)); + CalculatorEngine.instance.getVarsRegister().add(new Var.Builder("si", 5d)); Assert.assertEquals("-0.959", cm.evaluate(JsclOperation.numeric, "sin(5)").getResult()); Assert.assertEquals("-4.795", cm.evaluate(JsclOperation.numeric, "sin(5)si").getResult()); @@ -138,18 +138,19 @@ public class CalculatorEngineTest { Assert.assertEquals("-23.973", cm.evaluate(JsclOperation.numeric, "si*sin(5)si").getResult()); Assert.assertEquals("-3.309", cm.evaluate(JsclOperation.numeric, "sisin(5si)si").getResult()); - CalculatorEngine.instance.getVarsRegister().add(null, new Var.Builder("s", 1d)); + CalculatorEngine.instance.getVarsRegister().add(new Var.Builder("s", 1d)); Assert.assertEquals("5", cm.evaluate(JsclOperation.numeric, "si").getResult()); - CalculatorEngine.instance.getVarsRegister().add(null, new Var.Builder("k", 3.5d)); - CalculatorEngine.instance.getVarsRegister().add(null, new Var.Builder("k1", 4d)); + CalculatorEngine.instance.getVarsRegister().add(new Var.Builder("k", 3.5d)); + CalculatorEngine.instance.getVarsRegister().add(new Var.Builder("k1", 4d)); Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "k11").getResult()); - CalculatorEngine.instance.getVarsRegister().add(null, new Var.Builder("t", (String) null)); + CalculatorEngine.instance.getVarsRegister().add(new Var.Builder("t", (String) null)); Assert.assertEquals("11t", cm.evaluate(JsclOperation.numeric, "t11").getResult()); Assert.assertEquals("11et", cm.evaluate(JsclOperation.numeric, "t11e").getResult()); Assert.assertEquals("∞", cm.evaluate(JsclOperation.numeric, "∞").getResult()); - Assert.assertEquals("11t∞", cm.evaluate(JsclOperation.numeric, "t11∞").getResult()); + Assert.assertEquals("∞", cm.evaluate(JsclOperation.numeric, "Infinity").getResult()); + Assert.assertEquals("11∞t", cm.evaluate(JsclOperation.numeric, "t11∞").getResult()); Assert.assertEquals("-t+t^3", cm.evaluate(JsclOperation.numeric, "t(t-1)(t+1)").getResult()); Assert.assertEquals("3.957", cm.evaluate(JsclOperation.numeric, "ln(8)lg(8)+ln(8)").getResult()); @@ -192,7 +193,7 @@ public class CalculatorEngineTest { } Assert.assertEquals("0.739", 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))))))))))))))))))))))))))))))))))))").getResult()); - CalculatorEngine.instance.getVarsRegister().add(null, new Var.Builder("si", 5d)); + CalculatorEngine.instance.getVarsRegister().add(new Var.Builder("si", 5d)); Assert.assertEquals("5", cm.evaluate(JsclOperation.numeric, "si").getResult()); try { diff --git a/src/test/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessorTest.java b/src/test/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessorTest.java index 71cf8934..fb501c43 100644 --- a/src/test/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessorTest.java +++ b/src/test/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessorTest.java @@ -33,14 +33,15 @@ public class FromJsclSimplifyTextProcessorTest { Assert.assertEquals("e", tp.process("2.718281828459045")); Assert.assertEquals("tee", tp.process("t2.718281828459045*2.718281828459045")); - CalculatorEngine.instance.getVarsRegister().add(null, new Var.Builder("t2.718281828459045", "2")); + CalculatorEngine.instance.getVarsRegister().add(new Var.Builder("t2.718281828459045", "2")); + CalculatorEngine.instance.getVarsRegister().add(new Var.Builder("t", (String)null)); Assert.assertEquals("t2.718281828459045e", tp.process("t2.718281828459045*2.718281828459045")); Assert.assertEquals("ee", tp.process("2.718281828459045*2.718281828459045")); Assert.assertEquals("t×", tp.process("t*")); Assert.assertEquals("×t", tp.process("*t")); Assert.assertEquals("t2", tp.process("t*2")); Assert.assertEquals("2t", tp.process("2*t")); - CalculatorEngine.instance.getVarsRegister().add(null, new Var.Builder("t", (String) null)); + CalculatorEngine.instance.getVarsRegister().add(new Var.Builder("t", (String) null)); Assert.assertEquals("t×", tp.process("t*")); Assert.assertEquals("×t", tp.process("*t")); @@ -53,7 +54,7 @@ public class FromJsclSimplifyTextProcessorTest { Assert.assertEquals("t^[2×2t]", tp.process("t^[2*2*t]")); Assert.assertEquals("2t^2[2t]", tp.process("2*t^2[2*t]")); - CalculatorEngine.instance.getVarsRegister().add(null, new Var.Builder("k", (String) null)); + CalculatorEngine.instance.getVarsRegister().add(new Var.Builder("k", (String) null)); Assert.assertEquals("(t+2k)[k+2t]", tp.process("(t+2*k)*[k+2*t]")); Assert.assertEquals("(te+2k)e[k+2te]", tp.process("(t*e+2*k)*e*[k+2*t*e]"));