This commit is contained in:
serso 2011-11-12 14:28:34 +04:00
parent a3c985acd4
commit 63359cc0a9
7 changed files with 115 additions and 84 deletions

View File

@ -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<Var>(CalculatorEngine.instance.getVarsRegister().getEntities()));
final List<Var> vars = new ArrayList<Var>(CalculatorEngine.instance.getVarsRegister().getEntities());
CollectionsUtils.removeAll(vars, new Finder<Var>() {
@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);
}

View File

@ -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<String> 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) {

View File

@ -71,8 +71,8 @@ public class AndroidFunctionsRegistryImpl implements AndroidFunctionsRegistry {
}
@Override
public Function add(@Nullable String name, @NotNull IBuilder<? extends Function> IBuilder) {
return functionsRegistry.add(name, IBuilder);
public Function add(@NotNull IBuilder<? extends Function> IBuilder) {
return functionsRegistry.add(IBuilder);
}
@Override

View File

@ -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<? extends Var> IBuilder) {
IConstant result = mathRegistry.add(name, IBuilder);
public Var add(@NotNull IBuilder<? extends Var> IBuilder) {
IConstant result = mathRegistry.add(IBuilder);
if (result instanceof Var) {
return (Var) result;
} else if (result != null) {

View File

@ -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;

View File

@ -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 {

View File

@ -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]"));