Variables refactor

This commit is contained in:
serso 2016-01-27 17:33:01 +01:00
parent f767f10795
commit 2448f1b387
23 changed files with 324 additions and 383 deletions

View File

@ -4,6 +4,7 @@ import org.solovyev.android.calculator.function.EditFunctionFragment;
import org.solovyev.android.calculator.history.BaseHistoryFragment;
import org.solovyev.android.calculator.history.EditHistoryFragment;
import org.solovyev.android.calculator.math.edit.FunctionsFragment;
import org.solovyev.android.calculator.math.edit.VariablesFragment;
import org.solovyev.android.calculator.onscreen.CalculatorOnscreenService;
import javax.inject.Singleton;
@ -22,4 +23,5 @@ public interface AppComponent {
void inject(EditFunctionFragment fragment);
void inject(EditHistoryFragment fragment);
void inject(FunctionsFragment fragment);
void inject(VariablesFragment fragment);
}

View File

@ -22,33 +22,48 @@
package org.solovyev.android.calculator;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Handler;
import com.squareup.otto.Bus;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.model.EntityDao;
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.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
public abstract class BaseEntitiesRegistry<T extends MathEntity, P extends PersistedEntity> implements EntitiesRegistry<T> {
@Nonnull
private final MathRegistry<T> mathRegistry;
@Nonnull
private final String prefix;
@Nullable
protected final EntityDao<P> entityDao;
@Nonnull
protected final Object lock = this;
@Nonnull
private final MathRegistry<T> mathRegistry;
@Nonnull
private final String prefix;
@Inject
Handler handler;
@Inject
SharedPreferences preferences;
@Inject
Application application;
@Inject
Bus bus;
@Inject
ErrorReporter errorReporter;
// synchronized on lock
private boolean initialized;
@ -192,6 +207,16 @@ public abstract class BaseEntitiesRegistry<T extends MathEntity, P extends Persi
return mathRegistry.add(JBuilder);
}
@Nullable
protected T addSafely(@Nonnull JBuilder<? extends T> builder) {
try {
return add(builder);
} catch (Exception e) {
errorReporter.onException(e);
}
return null;
}
@Override
public void remove(@Nonnull T var) {
mathRegistry.remove(var);

View File

@ -43,7 +43,7 @@ import org.solovyev.android.calculator.math.edit.FunctionsActivity;
import org.solovyev.android.calculator.math.edit.OperatorsActivity;
import org.solovyev.android.calculator.math.edit.CalculatorVarsActivity;
import org.solovyev.android.calculator.math.edit.VarEditDialogFragment;
import org.solovyev.android.calculator.math.edit.VarsFragment;
import org.solovyev.android.calculator.math.edit.VariablesFragment;
import org.solovyev.android.calculator.matrix.CalculatorMatrixActivity;
import org.solovyev.android.calculator.plot.CalculatorPlotActivity;
import org.solovyev.android.calculator.plot.CalculatorPlotter;
@ -144,12 +144,12 @@ public final class CalculatorActivityLauncher implements CalculatorEventListener
if (viewState.valid) {
final String varValue = viewState.text;
if (!Strings.isEmpty(varValue)) {
if (VarsFragment.isValidValue(varValue)) {
if (VariablesFragment.isValidValue(varValue)) {
if (context instanceof AppCompatActivity) {
VarEditDialogFragment.showDialog(VarEditDialogFragment.Input.newFromValue(varValue), ((AppCompatActivity) context).getSupportFragmentManager());
} else {
final Intent intent = new Intent(context, CalculatorVarsActivity.class);
intent.putExtra(VarsFragment.CREATE_VAR_EXTRA_STRING, varValue);
intent.putExtra(VariablesFragment.CREATE_VAR_EXTRA_STRING, varValue);
Activities.addIntentFlags(intent, false, context);
context.startActivity(intent);
}

View File

@ -30,7 +30,7 @@ import org.solovyev.android.calculator.history.RecentHistoryFragment;
import org.solovyev.android.calculator.history.SavedHistoryFragment;
import org.solovyev.android.calculator.math.edit.FunctionsFragment;
import org.solovyev.android.calculator.math.edit.OperatorsFragment;
import org.solovyev.android.calculator.math.edit.VarsFragment;
import org.solovyev.android.calculator.math.edit.VariablesFragment;
import org.solovyev.android.calculator.matrix.CalculatorMatrixEditFragment;
import org.solovyev.android.calculator.plot.CalculatorPlotFragment;
import org.solovyev.android.calculator.plot.CalculatorPlotFunctionSettingsActivity;
@ -52,7 +52,7 @@ public enum CalculatorFragmentType {
//keyboard(CalculatorHistoryFragment.class, "history", R.layout.fragment_history, R.string.c_history),
history(RecentHistoryFragment.class, R.layout.fragment_history, R.string.cpp_history_tab_recent),
saved_history(SavedHistoryFragment.class, R.layout.fragment_history, R.string.cpp_history_tab_saved),
variables(VarsFragment.class, R.layout.vars_fragment, R.string.c_vars),
variables(VariablesFragment.class, R.layout.vars_fragment, R.string.c_vars),
functions(FunctionsFragment.class, R.layout.fragment_entities, R.string.c_functions),
operators(OperatorsFragment.class, R.layout.fragment_entities, R.string.c_operators),
plotter(CalculatorPlotFragment.class, R.layout.cpp_plotter_fragment, R.string.c_graph),

View File

@ -35,7 +35,7 @@ import jscl.math.function.IConstant;
import jscl.math.operator.Operator;
import jscl.text.ParseInterruptedException;
import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.android.calculator.model.Var;
import org.solovyev.android.calculator.model.OldVar;
import org.solovyev.android.calculator.units.CalculatorNumeralBase;
import org.solovyev.common.msg.ListMessageRegistry;
import org.solovyev.common.msg.Message;
@ -475,7 +475,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
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();
final OldVar.Builder builder = ansVar != null ? new OldVar.Builder(ansVar) : new OldVar.Builder();
builder.setName(VariablesRegistry.ANS);
builder.setValue(text);
builder.setDescription(CalculatorMessages.getBundle().getString(CalculatorMessages.ans_description));

View File

@ -23,15 +23,10 @@
package org.solovyev.android.calculator;
import android.content.SharedPreferences;
import com.squareup.otto.Bus;
import jscl.AngleUnit;
import jscl.JsclMathEngine;
import jscl.MathEngine;
import jscl.NumeralBase;
import jscl.math.operator.Operator;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.model.EntityDao;
import org.solovyev.android.calculator.model.Vars;
import org.solovyev.android.prefs.BooleanPreference;
import org.solovyev.android.prefs.IntegerPreference;
import org.solovyev.android.prefs.Preference;
@ -40,22 +35,26 @@ import org.solovyev.common.text.EnumMapper;
import org.solovyev.common.text.NumberMapper;
import org.solovyev.common.text.Strings;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executor;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import javax.inject.Singleton;
import jscl.AngleUnit;
import jscl.JsclMathEngine;
import jscl.MathEngine;
import jscl.NumeralBase;
import jscl.math.operator.Operator;
@Singleton
public class Engine implements SharedPreferences.OnSharedPreferenceChangeListener {
@Nonnull
private final MathEngine mathEngine;
@Nonnull
private final VariablesRegistry variablesRegistry;
@Nonnull
private final EntitiesRegistry<Operator> operatorsRegistry;
@Nonnull
@ -68,6 +67,8 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
ErrorReporter errorReporter;
@Inject
FunctionsRegistry functionsRegistry;
@Inject
VariablesRegistry variablesRegistry;
@Nonnull
private String multiplicationSign = Preferences.multiplicationSign.getDefaultValue();
@ -86,7 +87,6 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
this.mathEngine.setRoundResult(true);
this.mathEngine.setUseGroupingSeparator(true);
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());
}

View File

@ -22,14 +22,8 @@
package org.solovyev.android.calculator;
import android.app.Application;
import android.content.SharedPreferences;
import android.os.Handler;
import android.support.annotation.NonNull;
import com.squareup.otto.Bus;
import jscl.JsclMathEngine;
import jscl.math.function.CustomFunction;
import jscl.math.function.Function;
import org.json.JSONArray;
import org.json.JSONException;
import org.simpleframework.xml.Serializer;
@ -43,15 +37,25 @@ import org.solovyev.android.io.FileSaver;
import org.solovyev.common.JBuilder;
import org.solovyev.common.text.Strings;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.Executor;
import jscl.JsclMathEngine;
import jscl.math.function.CustomFunction;
import jscl.math.function.Function;
import static android.text.TextUtils.isEmpty;
@ -73,17 +77,7 @@ public class FunctionsRegistry extends BaseEntitiesRegistry<Function, OldFunctio
@Named(AppModule.THREAD_BACKGROUND)
Executor backgroundThread;
@Inject
Handler handler;
@Inject
ErrorReporter errorReporter;
@Inject
Calculator calculator;
@Inject
SharedPreferences preferences;
@Inject
Application application;
@Inject
Bus bus;
@Inject
public FunctionsRegistry(@Nonnull JsclMathEngine mathEngine) {
@ -102,6 +96,7 @@ public class FunctionsRegistry extends BaseEntitiesRegistry<Function, OldFunctio
@Override
public void init() {
Check.isNotMainThread();
try {
migrateOldFunctions();
addSafely(new CustomFunction.Builder(true, "log", Arrays.asList("base", "x"), "ln(x)/ln(base)"));
@ -114,9 +109,10 @@ public class FunctionsRegistry extends BaseEntitiesRegistry<Function, OldFunctio
for (CppFunction function : loadFunctions()) {
addSafely(function.toCustomFunctionBuilder());
}
} finally {
setInitialized();
}
}
@Override
public void remove(@Nonnull Function function) {
@ -134,18 +130,6 @@ public class FunctionsRegistry extends BaseEntitiesRegistry<Function, OldFunctio
return function;
}
@Nullable
public Function addSafely(@Nonnull JBuilder<? extends Function> builder) {
try {
// todo serso: currently JSCL might produce function's body which can't be loaded afterwards. F.e. f(x) = 3 * 6
// might be simpliffied to f(x) = 18 × 10 ^ 0 and × is not supported by JSCL
return add(builder);
} catch (Exception e) {
errorReporter.onException(e);
}
return null;
}
@NonNull
private List<CppFunction> loadFunctions() {
try {

View File

@ -22,23 +22,38 @@
package org.solovyev.android.calculator;
import org.solovyev.android.calculator.model.EntityDao;
import android.support.annotation.NonNull;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.json.Json;
import org.solovyev.android.calculator.model.MathEntityBuilder;
import org.solovyev.android.calculator.model.Var;
import org.solovyev.android.calculator.model.Vars;
import org.solovyev.android.calculator.model.OldFunctions;
import org.solovyev.android.calculator.model.OldVar;
import org.solovyev.android.calculator.model.OldVars;
import org.solovyev.android.calculator.variables.CppVariable;
import org.solovyev.android.io.FileSaver;
import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathEntity;
import org.solovyev.common.math.MathRegistry;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Singleton;
import jscl.JsclMathEngine;
import jscl.math.function.IConstant;
public class VariablesRegistry extends BaseEntitiesRegistry<IConstant, Var> {
import static android.text.TextUtils.isEmpty;
@Singleton
public class VariablesRegistry extends BaseEntitiesRegistry<IConstant, OldVar> {
@Nonnull
public static final String ANS = "ans";
@ -54,9 +69,9 @@ public class VariablesRegistry extends BaseEntitiesRegistry<IConstant, Var> {
substitutes.put("NaN", "nan");
}
public VariablesRegistry(@Nonnull MathRegistry<IConstant> mathRegistry,
@Nonnull EntityDao<Var> entityDao) {
super(mathRegistry, "c_var_description_", entityDao);
@Inject
public VariablesRegistry(@Nonnull JsclMathEngine mathEngine) {
super(mathEngine.getConstantsRegistry(), "c_var_description_", null);
}
public static <T extends MathEntity> void saveVariable(@Nonnull EntitiesRegistry<T> registry,
@ -82,41 +97,69 @@ public class VariablesRegistry extends BaseEntitiesRegistry<IConstant, Var> {
return substitutes;
}
public synchronized void init() {
super.init();
public void init() {
Check.isNotMainThread();
tryToAddAuxVar("x");
tryToAddAuxVar("y");
tryToAddAuxVar("t");
tryToAddAuxVar("j");
try {
//migrateOldVariables();
addSafely("x");
addSafely("y");
addSafely("t");
addSafely("j");
} finally {
setInitialized();
}
}
private void migrateOldVariables() {
final String xml = preferences.getString(OldVars.PREFS_KEY, null);
if (isEmpty(xml)) {
return;
}
try {
final Serializer serializer = new Persister();
final OldVars oldVariables = serializer.read(OldVars.class, xml);
if (oldVariables != null) {
List<CppVariable> variables = OldVars.toCppVariables(oldVariables);
FileSaver.save(getVariablesFile(), Json.toJson(variables).toString());
}
preferences.edit().remove(OldFunctions.PREFS_KEY).apply();
} catch (Exception e) {
errorReporter.onException(e);
}
}
@Nonnull
@Override
protected JBuilder<? extends IConstant> createBuilder(@Nonnull Var entity) {
return new Var.Builder(entity);
@NonNull
private File getVariablesFile() {
return new File(application.getFilesDir(), "variables.json");
}
@Nonnull
@Override
protected PersistedEntitiesContainer<Var> createPersistenceContainer() {
return new Vars();
protected JBuilder<? extends IConstant> createBuilder(@Nonnull OldVar entity) {
return new OldVar.Builder(entity);
}
private void tryToAddAuxVar(@Nonnull String name) {
@Nonnull
@Override
protected PersistedEntitiesContainer<OldVar> createPersistenceContainer() {
return new OldVars();
}
private void addSafely(@Nonnull String name) {
if (!contains(name)) {
add(new Var.Builder(name, (String) null));
addSafely(new OldVar.Builder(name, (String) null));
}
}
@Nonnull
@Override
protected Var transform(@Nonnull IConstant entity) {
if (entity instanceof Var) {
return (Var) entity;
protected OldVar transform(@Nonnull IConstant entity) {
if (entity instanceof OldVar) {
return (OldVar) entity;
} else {
return new Var.Builder(entity).create();
return new OldVar.Builder(entity).create();
}
}

View File

@ -45,7 +45,7 @@ import org.solovyev.android.calculator.CalculatorEventListener;
import org.solovyev.android.calculator.CalculatorEventType;
import org.solovyev.android.calculator.Locator;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.model.Var;
import org.solovyev.android.calculator.model.OldVar;
import org.solovyev.common.text.Strings;
import java.util.Arrays;
@ -148,12 +148,12 @@ public class VarEditDialogFragment extends DialogFragment implements CalculatorE
final EditText editDescription = (EditText) root.findViewById(R.id.var_edit_description);
editDescription.setText(input.getDescription());
final Var.Builder varBuilder;
final OldVar.Builder varBuilder;
final IConstant constant = input.getConstant();
if (constant != null) {
varBuilder = new Var.Builder(constant);
varBuilder = new OldVar.Builder(constant);
} else {
varBuilder = new Var.Builder();
varBuilder = new OldVar.Builder();
}
root.findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() {

View File

@ -121,7 +121,7 @@ public class VarEditorSaver<T extends MathEntity> implements View.OnClickListene
error = null;
} else {
// value is not empty => must be a number
boolean valid = VarsFragment.isValidValue(value);
boolean valid = VariablesFragment.isValidValue(value);
if (valid) {
varBuilder.setName(name);

View File

@ -28,26 +28,51 @@ import android.support.v4.app.FragmentActivity;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import com.melnykov.fab.FloatingActionButton;
import jscl.math.function.IConstant;
import org.solovyev.android.calculator.*;
import com.squareup.otto.Bus;
import org.solovyev.android.calculator.AppComponent;
import org.solovyev.android.calculator.Calculator;
import org.solovyev.android.calculator.CalculatorEventData;
import org.solovyev.android.calculator.CalculatorEventListener;
import org.solovyev.android.calculator.CalculatorEventType;
import org.solovyev.android.calculator.CalculatorFragmentType;
import org.solovyev.android.calculator.Category;
import org.solovyev.android.calculator.Change;
import org.solovyev.android.calculator.Keyboard;
import org.solovyev.android.calculator.Locator;
import org.solovyev.android.calculator.PreparedExpression;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.ToJsclTextProcessor;
import org.solovyev.android.calculator.VariablesRegistry;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.common.JPredicate;
import org.solovyev.common.collections.Collections;
import org.solovyev.common.text.Strings;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
public class VarsFragment extends BaseEntitiesFragment<IConstant>implements CalculatorEventListener {
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import jscl.math.function.IConstant;
public class VariablesFragment extends BaseEntitiesFragment<IConstant> implements CalculatorEventListener {
public static final String CREATE_VAR_EXTRA_STRING = "create_var";
@NonNull
private final EntitiesRegistry<IConstant> registry = Locator.getInstance().getEngine().getVariablesRegistry();
@Inject
VariablesRegistry registry;
@Inject
Calculator calculator;
@Inject
Keyboard keyboard;
@Inject
Bus bus;
public VarsFragment() {
public VariablesFragment() {
super(CalculatorFragmentType.variables);
}
@ -79,6 +104,12 @@ public class VarsFragment extends BaseEntitiesFragment<IConstant>implements Calc
setHasOptionsMenu(true);
}
@Override
protected void inject(@Nonnull AppComponent component) {
super.inject(component);
component.inject(this);
}
@Override
public void onViewCreated(View root, Bundle savedInstanceState) {
super.onViewCreated(root, savedInstanceState);

View File

@ -22,26 +22,21 @@
package org.solovyev.android.calculator.model;
import jscl.math.function.IFunction;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Transient;
import org.solovyev.android.calculator.ParseException;
import org.solovyev.android.calculator.Locator;
import org.solovyev.android.calculator.PersistedEntity;
import org.solovyev.common.math.MathEntity;
import org.solovyev.common.msg.Message;
import org.solovyev.common.msg.MessageLevel;
import org.solovyev.common.text.Strings;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import javax.annotation.Nonnull;
import jscl.math.function.IFunction;
@Root(name = "function")
public class OldFunction implements IFunction, PersistedEntity, Serializable {
@ -50,11 +45,9 @@ public class OldFunction implements IFunction, PersistedEntity, Serializable {
private Integer id;
@Element
@Nonnull
private String name;
@Element(name = "body")
@Nonnull
private String content;
@ElementList(type = String.class)
@ -71,10 +64,6 @@ public class OldFunction implements IFunction, PersistedEntity, Serializable {
public OldFunction() {
}
public OldFunction(Integer id) {
this.id = id;
}
public static OldFunction fromIFunction(@Nonnull IFunction function) {
final OldFunction result = new OldFunction();
@ -167,140 +156,4 @@ public class OldFunction implements IFunction, PersistedEntity, Serializable {
public List<String> getParameterNames() {
return parameterNames;
}
public void setParameterNames(@Nonnull List<String> parameterNames) {
this.parameterNames = parameterNames;
}
public static class Builder implements MathEntityBuilder<OldFunction> {
@Nonnull
private String name;
@Nullable
private String value;
private boolean system = false;
@Nullable
private String description;
@Nullable
private Integer id;
@Nonnull
private List<String> parameterNames = Collections.emptyList();
public Builder() {
}
public Builder(@Nonnull IFunction function) {
this.name = function.getName();
this.value = function.getContent();
this.system = function.isSystem();
this.description = function.getDescription();
if (function.isIdDefined()) {
this.id = function.getId();
}
this.parameterNames = new ArrayList<String>(function.getParameterNames());
}
public Builder(@Nonnull String name,
@Nonnull String value,
@Nonnull List<String> parameterNames) {
this.name = name;
this.value = value;
this.parameterNames = parameterNames;
}
@Nonnull
public Builder setName(@Nonnull String name) {
this.name = name;
return this;
}
@Nonnull
public Builder setValue(@Nullable String value) {
this.value = value;
return this;
}
protected Builder setSystem(boolean system) {
this.system = system;
return this;
}
public void setParameterNames(@Nonnull List<String> parameterNames) {
this.parameterNames = parameterNames;
}
@Nonnull
public Builder setDescription(@Nullable String description) {
this.description = description;
return this;
}
@Nonnull
public OldFunction create() throws OldFunction.Builder.CreationException {
final OldFunction result;
if (id != null) {
result = new OldFunction(id);
} else {
result = new OldFunction();
}
result.name = name;
try {
result.content = Locator.getInstance().getCalculator().prepareExpression(value).toString();
} catch (ParseException e) {
throw new CreationException(e);
}
result.system = system;
result.description = Strings.getNotEmpty(description, "");
result.parameterNames = new ArrayList<String>(parameterNames);
return result;
}
public static class CreationException extends RuntimeException implements Message {
@Nonnull
private final ParseException message;
public CreationException(@Nonnull ParseException cause) {
super(cause);
message = cause;
}
@Nonnull
@Override
public String getMessageCode() {
return message.getMessageCode();
}
@Nonnull
@Override
public List<Object> getParameters() {
return message.getParameters();
}
@Nonnull
@Override
public MessageLevel getMessageLevel() {
return message.getMessageLevel();
}
@Override
@Nonnull
public String getLocalizedMessage() {
return message.getLocalizedMessage();
}
@Nonnull
@Override
public String getLocalizedMessage(@Nonnull Locale locale) {
return message.getLocalizedMessage(locale);
}
}
}
}

View File

@ -23,15 +23,17 @@
package org.solovyev.android.calculator.model;
import android.text.TextUtils;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.solovyev.android.calculator.PersistedEntitiesContainer;
import org.solovyev.android.calculator.function.CppFunction;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
@Root(name = "Functions")
public class OldFunctions implements PersistedEntitiesContainer<OldFunction> {
@ -46,7 +48,7 @@ public class OldFunctions implements PersistedEntitiesContainer<OldFunction> {
@Nonnull
public static List<CppFunction> toCppFunctions(@Nonnull OldFunctions oldFunctions) {
final List<CppFunction> functions = new ArrayList<>();
for (OldFunction oldFunction : oldFunctions.getEntities()) {
for (OldFunction oldFunction : oldFunctions.functions) {
final String name = oldFunction.getName();
final String body = oldFunction.getContent();
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(body)) {

View File

@ -37,14 +37,8 @@ import jscl.math.function.Constant;
import jscl.math.function.ExtendedConstant;
import jscl.math.function.IConstant;
/**
* User: serso
* Date: 9/28/11
* Time: 11:22 PM
*/
@Root
public class Var implements IConstant, PersistedEntity {
@Root(name = "var")
public class OldVar implements IConstant, PersistedEntity {
@Transient
private Integer id;
@ -67,10 +61,10 @@ public class Var implements IConstant, PersistedEntity {
@Transient
private Constant constant;
private Var() {
private OldVar() {
}
private Var(@Nonnull Integer id) {
private OldVar(@Nonnull Integer id) {
this.id = id;
}
@ -167,7 +161,7 @@ public class Var implements IConstant, PersistedEntity {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Var var = (Var) o;
OldVar var = (OldVar) o;
if (!name.equals(var.name)) return false;
@ -179,7 +173,7 @@ public class Var implements IConstant, PersistedEntity {
return name.hashCode();
}
public static class Builder implements JBuilder<Var>, MathEntityBuilder<Var> {
public static class Builder implements JBuilder<OldVar>, MathEntityBuilder<OldVar> {
@Nonnull
private String name;
@ -198,7 +192,7 @@ public class Var implements IConstant, PersistedEntity {
public Builder() {
}
public Builder(@Nonnull Var var) {
public Builder(@Nonnull OldVar var) {
this.name = var.name;
this.value = var.value;
this.system = var.system;
@ -252,12 +246,12 @@ public class Var implements IConstant, PersistedEntity {
}
@Nonnull
public Var create() {
final Var result;
public OldVar create() {
final OldVar result;
if (id != null) {
result = new Var(id);
result = new OldVar(id);
} else {
result = new Var();
result = new OldVar();
}
result.name = name;

View File

@ -0,0 +1,64 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.calculator.model;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.solovyev.android.calculator.PersistedEntitiesContainer;
import org.solovyev.android.calculator.variables.CppVariable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
@Root(name = "vars")
public class OldVars implements PersistedEntitiesContainer<OldVar> {
public static final String PREFS_KEY = "org.solovyev.android.calculator.CalculatorModel_vars";
@ElementList(type = OldVar.class)
private List<OldVar> vars = new ArrayList<OldVar>();
public OldVars() {
}
public List<OldVar> getEntities() {
return vars;
}
@Nonnull
public static List<CppVariable> toCppVariables(@Nonnull OldVars oldVariables) {
final List<CppVariable> variables = new ArrayList<>();
/*for (OldVar oldVar : oldVariables.vars) {
final String name = oldVar.getName();
final String body = oldVar.getContent();
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(body)) {
continue;
}
variables.add(CppFunction.builder(name, body)
.withParameters(oldVar.getParameterNames())
.withDescription(oldVar.getDescription()).build());
}*/
return variables;
}
}

View File

@ -1,50 +0,0 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.calculator.model;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.solovyev.android.calculator.PersistedEntitiesContainer;
import java.util.ArrayList;
import java.util.List;
/**
* User: serso
* Date: 9/29/11
* Time: 5:19 PM
*/
@Root
public class Vars implements PersistedEntitiesContainer<Var> {
@ElementList(type = Var.class)
private List<Var> vars = new ArrayList<Var>();
public Vars() {
}
public List<Var> getEntities() {
return vars;
}
}

View File

@ -0,0 +1,15 @@
package org.solovyev.android.calculator.variables;
import org.json.JSONException;
import org.json.JSONObject;
import org.solovyev.android.calculator.json.Jsonable;
import javax.annotation.Nonnull;
public class CppVariable implements Jsonable {
@Nonnull
@Override
public JSONObject toJson() throws JSONException {
return null;
}
}

View File

@ -103,7 +103,7 @@ public class CalculatorTestUtils {
final JsclMathEngine jsclEngine = JsclMathEngine.getInstance();
final VariablesRegistry variablesRegistry = new VariablesRegistry(jsclEngine.getConstantsRegistry(), entityDao);
final VariablesRegistry variablesRegistry = new VariablesRegistry(jsclEngine);
final FunctionsRegistry functionsRegistry = new FunctionsRegistry(jsclEngine);
final OperatorsRegistry operatorsRegistry = new OperatorsRegistry(jsclEngine.getOperatorsRegistry());
final PostfixFunctionsRegistry postfixFunctionsRegistry = new PostfixFunctionsRegistry(jsclEngine.getPostfixFunctionsRegistry());

View File

@ -25,7 +25,7 @@ package org.solovyev.android.calculator;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.solovyev.android.calculator.model.Var;
import org.solovyev.android.calculator.model.OldVar;
import org.solovyev.android.calculator.text.FromJsclSimplifyTextProcessor;
import java.text.DecimalFormatSymbols;
@ -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().getVariablesRegistry().add(new Var.Builder("t2.718281828459045", "2"));
Locator.getInstance().getEngine().getVariablesRegistry().add(new Var.Builder("t", (String) null));
Locator.getInstance().getEngine().getVariablesRegistry().add(new OldVar.Builder("t2.718281828459045", "2"));
Locator.getInstance().getEngine().getVariablesRegistry().add(new OldVar.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().getVariablesRegistry().add(new Var.Builder("t", (String) null));
Locator.getInstance().getEngine().getVariablesRegistry().add(new OldVar.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().getVariablesRegistry().add(new Var.Builder("k", (String) null));
Locator.getInstance().getEngine().getVariablesRegistry().add(new OldVar.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().getVariablesRegistry().add(new Var.Builder("si", 5d));
Locator.getInstance().getEngine().getVariablesRegistry().add(new OldVar.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().getVariablesRegistry().add(new Var.Builder("si", 5d));
Locator.getInstance().getEngine().getVariablesRegistry().add(new OldVar.Builder("si", 5d));
try {
cm.setAngleUnits(AngleUnit.rad);
@ -130,14 +130,14 @@ public class EvaluateTest extends AbstractCalculatorTest {
cm.setAngleUnits(defaultAngleUnit);
}
Locator.getInstance().getEngine().getVariablesRegistry().add(new Var.Builder("s", 1d));
Locator.getInstance().getEngine().getVariablesRegistry().add(new OldVar.Builder("s", 1d));
CalculatorTestUtils.assertEval("5", cm.evaluate("si"));
Locator.getInstance().getEngine().getVariablesRegistry().add(new Var.Builder("k", 3.5d));
Locator.getInstance().getEngine().getVariablesRegistry().add(new Var.Builder("k1", 4d));
Locator.getInstance().getEngine().getVariablesRegistry().add(new OldVar.Builder("k", 3.5d));
Locator.getInstance().getEngine().getVariablesRegistry().add(new OldVar.Builder("k1", 4d));
CalculatorTestUtils.assertEval("4", "k11");
Locator.getInstance().getEngine().getVariablesRegistry().add(new Var.Builder("t", (String) null));
Locator.getInstance().getEngine().getVariablesRegistry().add(new OldVar.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().getVariablesRegistry().add(new Var.Builder("t", (String) null));
Locator.getInstance().getEngine().getVariablesRegistry().add(new OldVar.Builder("t", (String) null));
CalculatorTestUtils.assertEval("2t", "∂(t^2,t)", JsclOperation.simplify);
CalculatorTestUtils.assertEval("2t", "∂(t^2,t)");
Locator.getInstance().getEngine().getVariablesRegistry().add(new Var.Builder("t", "2"));
Locator.getInstance().getEngine().getVariablesRegistry().add(new OldVar.Builder("t", "2"));
CalculatorTestUtils.assertEval("2t", "∂(t^2,t)", JsclOperation.simplify);
CalculatorTestUtils.assertEval("4", "∂(t^2,t)");

View File

@ -22,7 +22,6 @@
package org.solovyev.android.calculator.model;
import jscl.util.ExpressionGenerator;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.Assert;
@ -33,12 +32,16 @@ import org.simpleframework.xml.core.Persister;
import org.solovyev.android.calculator.CalculatorTestUtils;
import org.solovyev.common.Objects;
import org.solovyev.common.equals.CollectionEqualizer;
import org.solovyev.common.text.Strings;
import java.io.StringWriter;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.StringWriter;
import java.util.*;
import jscl.util.ExpressionGenerator;
/**
* User: serso
@ -87,31 +90,6 @@ public class OldFunctionsTest {
CalculatorTestUtils.staticSetUp();
}
@Test
public void testXml() throws Exception {
final OldFunctions in = new OldFunctions();
OldFunction first = new OldFunction.Builder("test", "x+y", Arrays.asList("x", "y")).setDescription("description").setSystem(false).create();
in.getEntities().add(first);
OldFunction second = new OldFunction.Builder("z_2", "e^(z_1^2+z_2^2)", Arrays.asList("z_1", "z_2")).setSystem(true).create();
in.getEntities().add(second);
OldFunction third = new OldFunction.Builder("z_2", "e^(z_1^2+z_2^2)", Arrays.asList("z_1", "z_2")).setSystem(true).setDescription("").create();
in.getEntities().add(third);
final OldFunctions out = testXml(in, xml);
Assert.assertTrue(!out.getEntities().isEmpty());
final OldFunction firstOut = out.getEntities().get(0);
final OldFunction secondOut = out.getEntities().get(1);
assertEquals(first, firstOut);
assertEquals(second, secondOut);
}
@Nonnull
private OldFunctions testXml(@Nonnull OldFunctions in, @Nullable String expectedXml) throws Exception {
final String actualXml = toXml(in);
@ -142,7 +120,7 @@ public class OldFunctionsTest {
ExpressionGenerator generator = new ExpressionGenerator(10);
for (int i = 0; i < 1000; i++) {
final String content = generator.generate();
/* final String content = generator.generate();
final String paramsString = Strings.generateRandomString(random.nextInt(10));
final List<String> parameterNames = new ArrayList<String>();
@ -158,7 +136,7 @@ public class OldFunctionsTest {
builder.setSystem(random.nextBoolean());
in.getEntities().add(builder.create());
in.getEntities().add(builder.create());*/
}
testXml(in, null);

View File

@ -37,7 +37,7 @@ import static org.junit.Assert.assertEquals;
* Date: 11/7/11
* Time: 7:52 PM
*/
public class VarTest {
public class OldVarTest {
private static final String xml = "<vars>\n" +
" <vars class=\"java.util.ArrayList\">\n" +
@ -57,10 +57,10 @@ public class VarTest {
@Test
public void testXml() throws Exception {
final Vars vars = new Vars();
Var first = new Var.Builder("e", Math.E).setDescription("description").setSystem(true).create();
final OldVars vars = new OldVars();
OldVar first = new OldVar.Builder("e", Math.E).setDescription("description").setSystem(true).create();
vars.getEntities().add(first);
Var second = new Var.Builder(";", 3d).setSystem(true).create();
OldVar second = new OldVar.Builder(";", 3d).setSystem(true).create();
vars.getEntities().add(second);
final StringWriter sw = new StringWriter();
@ -69,7 +69,7 @@ public class VarTest {
assertEquals(xml, sw.toString());
final Vars result = serializer.read(Vars.class, xml);
final OldVars result = serializer.read(OldVars.class, xml);
final IConstant actualFirst = result.getEntities().get(0);
final IConstant actualSecond = result.getEntities().get(1);