VarsRegistry->VariablesRegistry

This commit is contained in:
serso 2016-01-27 17:09:33 +01:00
parent 59e4a15bd2
commit f767f10795
15 changed files with 58 additions and 47 deletions

View File

@ -472,15 +472,15 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
if (TextUtils.isEmpty(text)) {
return;
}
final EntitiesRegistry<IConstant> varsRegistry = Locator.getInstance().getEngine().getVarsRegistry();
final IConstant ansVar = varsRegistry.get(VarsRegistry.ANS);
final EntitiesRegistry<IConstant> varsRegistry = Locator.getInstance().getEngine().getVariablesRegistry();
final IConstant ansVar = varsRegistry.get(VariablesRegistry.ANS);
final Var.Builder builder = ansVar != null ? new Var.Builder(ansVar) : new Var.Builder();
builder.setName(VarsRegistry.ANS);
builder.setName(VariablesRegistry.ANS);
builder.setValue(text);
builder.setDescription(CalculatorMessages.getBundle().getString(CalculatorMessages.ans_description));
VarsRegistry.saveVariable(varsRegistry, builder, ansVar, this, false);
VariablesRegistry.saveVariable(varsRegistry, builder, ansVar, this, false);
}
@Subscribe
@ -498,7 +498,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
switch (calculatorEventType) {
case constant_changed:
final IConstant newConstant = ((Change<IConstant>) data).getNewValue();
if (!newConstant.getName().equals(VarsRegistry.ANS)) {
if (!newConstant.getName().equals(VariablesRegistry.ANS)) {
evaluate();
}
break;

View File

@ -54,7 +54,7 @@ public final class CalculatorUtils {
final Set<Constant> notSystemConstants = new HashSet<Constant>();
for (Constant constant : expression.getConstants()) {
IConstant var = Locator.getInstance().getEngine().getVarsRegistry().get(constant.getName());
IConstant var = Locator.getInstance().getEngine().getVariablesRegistry().get(constant.getName());
if (var != null && !var.isSystem() && !var.isDefined()) {
notSystemConstants.add(constant);
}

View File

@ -54,7 +54,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
@Nonnull
private final MathEngine mathEngine;
@Nonnull
private final VarsRegistry varsRegistry;
private final VariablesRegistry variablesRegistry;
@Nonnull
private final EntitiesRegistry<Operator> operatorsRegistry;
@ -71,9 +71,9 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
@Nonnull
private String multiplicationSign = Preferences.multiplicationSign.getDefaultValue();
public Engine(@Nonnull MathEngine mathEngine, @Nonnull VarsRegistry varsRegistry, @Nonnull FunctionsRegistry functionsRegistry, @Nonnull EntitiesRegistry<Operator> operatorsRegistry, @Nonnull EntitiesRegistry<Operator> postfixFunctionsRegistry) {
public Engine(@Nonnull MathEngine mathEngine, @Nonnull VariablesRegistry variablesRegistry, @Nonnull FunctionsRegistry functionsRegistry, @Nonnull EntitiesRegistry<Operator> operatorsRegistry, @Nonnull EntitiesRegistry<Operator> postfixFunctionsRegistry) {
this.mathEngine = mathEngine;
this.varsRegistry = varsRegistry;
this.variablesRegistry = variablesRegistry;
this.functionsRegistry = functionsRegistry;
this.operatorsRegistry = operatorsRegistry;
this.postfixFunctionsRegistry = postfixFunctionsRegistry;
@ -86,7 +86,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
this.mathEngine.setRoundResult(true);
this.mathEngine.setUseGroupingSeparator(true);
this.varsRegistry = new VarsRegistry(mathEngine.getConstantsRegistry(), new EntityDao<>("org.solovyev.android.calculator.CalculatorModel_vars", Vars.class, preferences));
this.variablesRegistry = new VariablesRegistry(mathEngine.getConstantsRegistry(), new EntityDao<>("org.solovyev.android.calculator.CalculatorModel_vars", Vars.class, preferences));
this.operatorsRegistry = new OperatorsRegistry(mathEngine.getOperatorsRegistry());
this.postfixFunctionsRegistry = new PostfixFunctionsRegistry(mathEngine.getPostfixFunctionsRegistry());
}
@ -106,8 +106,8 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
}
@Nonnull
public VarsRegistry getVarsRegistry() {
return varsRegistry;
public VariablesRegistry getVariablesRegistry() {
return variablesRegistry;
}
@Nonnull
@ -169,7 +169,7 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
}
private void initAsync() {
init(varsRegistry);
init(variablesRegistry);
init(functionsRegistry);
init(operatorsRegistry);
init(postfixFunctionsRegistry);

View File

@ -114,9 +114,9 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
if (functionName == null) {
String operatorName = App.find(MathType.operator.getTokens(), startsWithFinder);
if (operatorName == null) {
String varName = App.find(Locator.getInstance().getEngine().getVarsRegistry().getNames(), startsWithFinder);
String varName = App.find(Locator.getInstance().getEngine().getVariablesRegistry().getNames(), startsWithFinder);
if (varName != null) {
final IConstant var = Locator.getInstance().getEngine().getVarsRegistry().get(varName);
final IConstant var = Locator.getInstance().getEngine().getVariablesRegistry().get(varName);
if (var != null) {
if (!var.isDefined()) {
undefinedVars.add(var);

View File

@ -22,7 +22,6 @@
package org.solovyev.android.calculator;
import jscl.math.function.IConstant;
import org.solovyev.android.calculator.model.EntityDao;
import org.solovyev.android.calculator.model.MathEntityBuilder;
import org.solovyev.android.calculator.model.Var;
@ -31,12 +30,15 @@ import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathEntity;
import org.solovyev.common.math.MathRegistry;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
public class VarsRegistry extends BaseEntitiesRegistry<IConstant, Var> {
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import jscl.math.function.IConstant;
public class VariablesRegistry extends BaseEntitiesRegistry<IConstant, Var> {
@Nonnull
public static final String ANS = "ans";
@ -52,7 +54,7 @@ public class VarsRegistry extends BaseEntitiesRegistry<IConstant, Var> {
substitutes.put("NaN", "nan");
}
public VarsRegistry(@Nonnull MathRegistry<IConstant> mathRegistry,
public VariablesRegistry(@Nonnull MathRegistry<IConstant> mathRegistry,
@Nonnull EntityDao<Var> entityDao) {
super(mathRegistry, "c_var_description_", entityDao);
}

View File

@ -60,7 +60,7 @@ import org.solovyev.android.calculator.KeyboardWindow;
import org.solovyev.android.calculator.Locator;
import org.solovyev.android.calculator.ParseException;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.VarsRegistry;
import org.solovyev.android.calculator.VariablesRegistry;
import org.solovyev.android.calculator.math.edit.FunctionsActivity;
import org.solovyev.android.calculator.math.edit.VarEditorSaver;
import org.solovyev.android.calculator.view.EditTextCompat;
@ -91,7 +91,7 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC
private static final int MENU_CATEGORY = Menu.FIRST + 2;
@NonNull
private final VarsRegistry constantsRegistry = Locator.getInstance().getEngine().getVarsRegistry();
private final VariablesRegistry constantsRegistry = Locator.getInstance().getEngine().getVariablesRegistry();
@NonNull
private final KeyboardWindow keyboardWindow = new KeyboardWindow();
@NonNull

View File

@ -170,7 +170,7 @@ public enum MathType {
@Nonnull
@Override
public List<String> getTokens() {
return Locator.getInstance().getEngine().getVarsRegistry().getNames();
return Locator.getInstance().getEngine().getVariablesRegistry().getNames();
}
@Override

View File

@ -91,7 +91,7 @@ public class MathEntityRemover<T extends MathEntity> implements View.OnClickList
@Nullable DialogInterface.OnClickListener callbackOnCancel,
@Nonnull Context context,
@Nonnull Object source) {
return new MathEntityRemover<IConstant>(constant, callbackOnCancel, false, Locator.getInstance().getEngine().getVarsRegistry(), context, source, Params.newConstantInstance());
return new MathEntityRemover<IConstant>(constant, callbackOnCancel, false, Locator.getInstance().getEngine().getVariablesRegistry(), context, source, Params.newConstantInstance());
}
/*

View File

@ -163,7 +163,7 @@ public class VarEditDialogFragment extends DialogFragment implements CalculatorE
}
});
root.findViewById(R.id.save_button).setOnClickListener(new VarEditorSaver<IConstant>(varBuilder, constant, root, Locator.getInstance().getEngine().getVarsRegistry(), this));
root.findViewById(R.id.save_button).setOnClickListener(new VarEditorSaver<IConstant>(varBuilder, constant, root, Locator.getInstance().getEngine().getVariablesRegistry(), this));
if (constant == null) {
// CREATE MODE

View File

@ -28,7 +28,7 @@ import android.widget.EditText;
import org.solovyev.android.calculator.EntitiesRegistry;
import org.solovyev.android.calculator.Locator;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.VarsRegistry;
import org.solovyev.android.calculator.VariablesRegistry;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.calculator.model.MathEntityBuilder;
import org.solovyev.common.math.MathEntity;
@ -145,7 +145,7 @@ public class VarEditorSaver<T extends MathEntity> implements View.OnClickListene
if (error != null) {
Locator.getInstance().getNotifier().showMessage(error, MessageType.error);
} else {
VarsRegistry.saveVariable(mathRegistry, varBuilder, editedInstance, source, true);
VariablesRegistry.saveVariable(mathRegistry, varBuilder, editedInstance, source, true);
}
}
}

View File

@ -45,7 +45,7 @@ public class VarsFragment extends BaseEntitiesFragment<IConstant>implements Calc
public static final String CREATE_VAR_EXTRA_STRING = "create_var";
@NonNull
private final EntitiesRegistry<IConstant> registry = Locator.getInstance().getEngine().getVarsRegistry();
private final EntitiesRegistry<IConstant> registry = Locator.getInstance().getEngine().getVariablesRegistry();
public VarsFragment() {
super(CalculatorFragmentType.variables);

View File

@ -23,8 +23,9 @@
package org.solovyev.android.calculator;
import android.content.Context;
import com.squareup.otto.Bus;
import jscl.JsclMathEngine;
import org.junit.Assert;
import org.mockito.Mockito;
import org.robolectric.fakes.RoboSharedPreferences;
@ -33,9 +34,12 @@ import org.solovyev.android.calculator.language.Languages;
import org.solovyev.android.calculator.model.EntityDao;
import org.solovyev.android.calculator.plot.CalculatorPlotter;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.DecimalFormatSymbols;
import java.util.HashMap;
import java.util.Map;
@ -43,6 +47,11 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import jscl.JsclMathEngine;
/**
* User: serso
* Date: 10/7/12
@ -94,12 +103,12 @@ public class CalculatorTestUtils {
final JsclMathEngine jsclEngine = JsclMathEngine.getInstance();
final VarsRegistry varsRegistry = new VarsRegistry(jsclEngine.getConstantsRegistry(), entityDao);
final VariablesRegistry variablesRegistry = new VariablesRegistry(jsclEngine.getConstantsRegistry(), entityDao);
final FunctionsRegistry functionsRegistry = new FunctionsRegistry(jsclEngine);
final OperatorsRegistry operatorsRegistry = new OperatorsRegistry(jsclEngine.getOperatorsRegistry());
final PostfixFunctionsRegistry postfixFunctionsRegistry = new PostfixFunctionsRegistry(jsclEngine.getPostfixFunctionsRegistry());
return new Engine(jsclEngine, varsRegistry, functionsRegistry, operatorsRegistry, postfixFunctionsRegistry);
return new Engine(jsclEngine, variablesRegistry, functionsRegistry, operatorsRegistry, postfixFunctionsRegistry);
}
public static void assertEval(@Nonnull String expected, @Nonnull String expression) {

View File

@ -57,15 +57,15 @@ public class FromJsclSimplifyTextProcessorTest extends AbstractCalculatorTest {
//Assert.assertEquals("e", tp.process("2.718281828459045"));
//Assert.assertEquals("tee", tp.process("t2.718281828459045*2.718281828459045"));
Locator.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("t2.718281828459045", "2"));
Locator.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("t", (String) null));
Locator.getInstance().getEngine().getVariablesRegistry().add(new Var.Builder("t2.718281828459045", "2"));
Locator.getInstance().getEngine().getVariablesRegistry().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"));
Locator.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("t", (String) null));
Locator.getInstance().getEngine().getVariablesRegistry().add(new Var.Builder("t", (String) null));
Assert.assertEquals("t×", tp.process("t*"));
Assert.assertEquals("×t", tp.process("*t"));
@ -78,7 +78,7 @@ public class FromJsclSimplifyTextProcessorTest extends AbstractCalculatorTest {
Assert.assertEquals("t^[2×2t]", tp.process("t^[2*2*t]"));
Assert.assertEquals("2t^2[2t]", tp.process("2*t^2[2*t]"));
Locator.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("k", (String) null));
Locator.getInstance().getEngine().getVariablesRegistry().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]"));

View File

@ -139,7 +139,7 @@ public class AndroidEngineTest extends AbstractCalculatorTest {
cm.setAngleUnits(defaultAngleUnit);
}
Locator.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("si", 5d));
Locator.getInstance().getEngine().getVariablesRegistry().add(new Var.Builder("si", 5d));
CalculatorTestUtils.assertEval("5", cm.evaluate("si"));
CalculatorTestUtils.assertError("sin");

View File

@ -116,7 +116,7 @@ public class EvaluateTest extends AbstractCalculatorTest {
CalculatorTestUtils.assertError("(-1)i!");
CalculatorTestUtils.assertEval("24i", "4!i");
Locator.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("si", 5d));
Locator.getInstance().getEngine().getVariablesRegistry().add(new Var.Builder("si", 5d));
try {
cm.setAngleUnits(AngleUnit.rad);
@ -130,14 +130,14 @@ public class EvaluateTest extends AbstractCalculatorTest {
cm.setAngleUnits(defaultAngleUnit);
}
Locator.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("s", 1d));
Locator.getInstance().getEngine().getVariablesRegistry().add(new Var.Builder("s", 1d));
CalculatorTestUtils.assertEval("5", cm.evaluate("si"));
Locator.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("k", 3.5d));
Locator.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("k1", 4d));
Locator.getInstance().getEngine().getVariablesRegistry().add(new Var.Builder("k", 3.5d));
Locator.getInstance().getEngine().getVariablesRegistry().add(new Var.Builder("k1", 4d));
CalculatorTestUtils.assertEval("4", "k11");
Locator.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("t", (String) null));
Locator.getInstance().getEngine().getVariablesRegistry().add(new Var.Builder("t", (String) null));
CalculatorTestUtils.assertEval("11t", "t11");
CalculatorTestUtils.assertEval("11et", "t11e");
CalculatorTestUtils.assertEval("", "");
@ -183,10 +183,10 @@ public class EvaluateTest extends AbstractCalculatorTest {
cm.setTimeout(3000);
}*/
Locator.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("t", (String) null));
Locator.getInstance().getEngine().getVariablesRegistry().add(new Var.Builder("t", (String) null));
CalculatorTestUtils.assertEval("2t", "∂(t^2,t)", JsclOperation.simplify);
CalculatorTestUtils.assertEval("2t", "∂(t^2,t)");
Locator.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("t", "2"));
Locator.getInstance().getEngine().getVariablesRegistry().add(new Var.Builder("t", "2"));
CalculatorTestUtils.assertEval("2t", "∂(t^2,t)", JsclOperation.simplify);
CalculatorTestUtils.assertEval("4", "∂(t^2,t)");