From cb2047d05e12d688ba41b89d063aee76d1f49f78 Mon Sep 17 00:00:00 2001 From: serso Date: Thu, 28 Jan 2016 15:19:18 +0100 Subject: [PATCH] Variables refactor --- .../calculator/BaseEntitiesRegistry.java | 149 ++++++++-------- .../calculator/CalculatorFragmentType.java | 2 +- .../android/calculator/CalculatorImpl.java | 43 +++-- .../solovyev/android/calculator/Category.java | 16 -- .../android/calculator/EntitiesRegistry.java | 1 + .../android/calculator/FunctionsRegistry.java | 116 ++----------- .../android/calculator/OperatorsRegistry.java | 46 +++-- .../PersistedEntitiesContainer.java | 29 ---- .../calculator/PostfixFunctionsRegistry.java | 57 +++---- .../android/calculator/VariablesRegistry.java | 78 ++++----- .../android/calculator/entities/Category.java | 20 +++ .../android/calculator/entities/Entities.java | 25 +++ .../calculator/function/CppFunction.java | 24 ++- .../function/EditFunctionFragment.java | 5 +- .../{ => function}/FunctionCategory.java | 21 ++- .../OldFunction.java} | 36 +++- .../{model => function}/OldFunctions.java | 25 ++- .../math/edit/BaseEntitiesFragment.java | 22 ++- .../math/edit/CalculatorVarsActivity.java | 6 +- .../math/edit/FunctionsActivity.java | 2 +- .../math/edit/FunctionsFragment.java | 1 + .../math/edit/OperatorsActivity.java | 2 +- .../math/edit/OperatorsFragment.java | 1 + .../math/edit/VarEditDialogFragment.java | 2 +- .../calculator/math/edit/VarEditorSaver.java | 2 +- .../math/edit/VariablesFragment.java | 4 +- .../android/calculator/model/EntityDao.java | 81 --------- .../android/calculator/model/OldFunction.java | 159 ------------------ .../{ => operators}/OperatorCategory.java | 17 +- .../calculator/variables/CppVariable.java | 158 ++++++++++++++++- .../calculator/variables/JsclConstant.java | 116 +++++++++++++ .../{model => variables}/OldVar.java | 8 +- .../{model => variables}/OldVars.java | 32 ++-- .../VariablesCategory.java} | 23 +-- app/src/main/res/layout/vars_fragment.xml | 52 ------ .../calculator/CalculatorTestUtils.java | 3 - .../FromJsclSimplifyTextProcessorTest.java | 2 +- .../calculator/model/AndroidEngineTest.java | 1 + .../calculator/model/EvaluateTest.java | 1 + .../calculator/model/OldFunctionsTest.java | 12 +- .../android/calculator/model/OldVarTest.java | 10 +- 41 files changed, 673 insertions(+), 737 deletions(-) delete mode 100644 app/src/main/java/org/solovyev/android/calculator/Category.java delete mode 100644 app/src/main/java/org/solovyev/android/calculator/PersistedEntitiesContainer.java create mode 100644 app/src/main/java/org/solovyev/android/calculator/entities/Category.java create mode 100644 app/src/main/java/org/solovyev/android/calculator/entities/Entities.java rename app/src/main/java/org/solovyev/android/calculator/{ => function}/FunctionCategory.java (92%) rename app/src/main/java/org/solovyev/android/calculator/{PersistedEntity.java => function/OldFunction.java} (51%) rename app/src/main/java/org/solovyev/android/calculator/{model => function}/OldFunctions.java (66%) delete mode 100644 app/src/main/java/org/solovyev/android/calculator/model/EntityDao.java delete mode 100644 app/src/main/java/org/solovyev/android/calculator/model/OldFunction.java rename app/src/main/java/org/solovyev/android/calculator/{ => operators}/OperatorCategory.java (82%) create mode 100644 app/src/main/java/org/solovyev/android/calculator/variables/JsclConstant.java rename app/src/main/java/org/solovyev/android/calculator/{model => variables}/OldVar.java (96%) rename app/src/main/java/org/solovyev/android/calculator/{model => variables}/OldVars.java (63%) rename app/src/main/java/org/solovyev/android/calculator/{VarCategory.java => variables/VariablesCategory.java} (71%) delete mode 100644 app/src/main/res/layout/vars_fragment.xml diff --git a/app/src/main/java/org/solovyev/android/calculator/BaseEntitiesRegistry.java b/app/src/main/java/org/solovyev/android/calculator/BaseEntitiesRegistry.java index a55c4b38..415cdca5 100644 --- a/app/src/main/java/org/solovyev/android/calculator/BaseEntitiesRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/BaseEntitiesRegistry.java @@ -27,33 +27,43 @@ import android.content.Context; import android.content.SharedPreferences; import android.content.res.Resources; import android.os.Handler; +import android.support.annotation.NonNull; import com.squareup.otto.Bus; +import org.json.JSONArray; +import org.json.JSONException; import org.solovyev.android.Check; -import org.solovyev.android.calculator.model.EntityDao; +import org.solovyev.android.calculator.json.Json; +import org.solovyev.android.calculator.json.Jsonable; +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.io.IOException; import java.util.ArrayList; +import java.util.Collections; 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; -public abstract class BaseEntitiesRegistry implements EntitiesRegistry { +public abstract class BaseEntitiesRegistry implements EntitiesRegistry { - @Nullable - protected final EntityDao

entityDao; @Nonnull protected final Object lock = this; @Nonnull private final MathRegistry mathRegistry; @Nonnull private final String prefix; + @NonNull + private final WriteTask writeTask = new WriteTask(); @Inject Handler handler; @Inject @@ -64,16 +74,17 @@ public abstract class BaseEntitiesRegistry mathRegistry, - @Nonnull String prefix, - @Nullable EntityDao

entityDao) { + @Nonnull String prefix) { this.mathRegistry = mathRegistry; this.prefix = prefix; - this.entityDao = entityDao; } @@ -96,6 +107,10 @@ public abstract class BaseEntitiesRegistry List loadEntities(@NonNull Json.Creator creator) { + final File file = getEntitiesFile(); + if (file == null) { + return Collections.emptyList(); } - final PersistedEntitiesContainer

persistenceContainer = entityDao.load(); - - final List

notCreatedEntities = new ArrayList

(); - - if (persistenceContainer != null) { - for (P entity : persistenceContainer.getEntities()) { - if (!contains(entity.getName())) { - try { - final JBuilder builder = createBuilder(entity); - add(builder); - } catch (RuntimeException e) { - Locator.getInstance().getErrorReporter().onException(e); - notCreatedEntities.add(entity); - } - } - } - } - try { - if (!notCreatedEntities.isEmpty()) { - final StringBuilder errorMessage = new StringBuilder(notCreatedEntities.size() * 100); - for (P notCreatedEntity : notCreatedEntities) { - errorMessage.append(notCreatedEntity).append("\n\n"); - } - - Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_message_dialog, MessageDialogData.newInstance(CalculatorMessages.newErrorMessage(CalculatorMessages.msg_007, errorMessage.toString()), null)); - } - } catch (RuntimeException e) { - // just in case - Locator.getInstance().getErrorReporter().onException(e); + return Json.load(file, creator); + } catch (IOException | JSONException e) { + errorReporter.onException(e); } - setInitialized(); + return Collections.emptyList(); } protected final void setInitialized() { @@ -162,34 +151,12 @@ public abstract class BaseEntitiesRegistry createBuilder(@Nonnull P entity); - @Override - public synchronized void save() { - if (entityDao == null) { - return; - } - final PersistedEntitiesContainer

container = createPersistenceContainer(); - - for (T entity : this.getEntities()) { - if (!entity.isSystem()) { - final P persistenceEntity = transform(entity); - if (persistenceEntity != null) { - container.getEntities().add(persistenceEntity); - } - } - } - - entityDao.save(container); + public void save() { + handler.removeCallbacks(writeTask); + handler.postDelayed(writeTask, 500); } - @Nullable - protected abstract P transform(@Nonnull T entity); - - @Nonnull - protected abstract PersistedEntitiesContainer

createPersistenceContainer(); - @Nonnull @Override public List getEntities() { @@ -203,8 +170,12 @@ public abstract class BaseEntitiesRegistry JBuilder) { - return mathRegistry.add(JBuilder); + public T add(@Nonnull JBuilder builder) { + final T entity = mathRegistry.add(builder); + if (!entity.isSystem() && isInitialized()) { + save(); + } + return entity; } @Nullable @@ -220,6 +191,7 @@ public abstract class BaseEntitiesRegistry entities = new ArrayList<>(); + for (T entity : getEntities()) { + if (entity.isSystem()) { + continue; + } + final Jsonable jsonable = toJsonable(entity); + if (jsonable != null) { + entities.add(jsonable); + } + } + backgroundThread.execute(new Runnable() { + @Override + public void run() { + final JSONArray array = Json.toJson(entities); + try { + FileSaver.save(file, array.toString()); + } catch (IOException e) { + errorReporter.onException(e); + } + } + }); + } + } } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorFragmentType.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorFragmentType.java index 18a6650c..e0b358df 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorFragmentType.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorFragmentType.java @@ -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(VariablesFragment.class, R.layout.vars_fragment, R.string.c_vars), + variables(VariablesFragment.class, R.layout.fragment_entities, 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), diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java index 43c44611..78016c2b 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java @@ -22,21 +22,16 @@ package org.solovyev.android.calculator; +import android.support.annotation.NonNull; import android.text.TextUtils; import android.util.Log; + import com.squareup.otto.Bus; import com.squareup.otto.Subscribe; -import jscl.AbstractJsclArithmeticException; -import jscl.MathEngine; -import jscl.NumeralBase; -import jscl.NumeralBaseException; -import jscl.math.Generic; -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.OldVar; import org.solovyev.android.calculator.units.CalculatorNumeralBase; +import org.solovyev.android.calculator.variables.CppVariable; import org.solovyev.common.msg.ListMessageRegistry; import org.solovyev.common.msg.Message; import org.solovyev.common.msg.MessageRegistry; @@ -45,14 +40,24 @@ import org.solovyev.common.text.Strings; import org.solovyev.common.units.ConversionException; import org.solovyev.common.units.Conversions; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicLong; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import jscl.AbstractJsclArithmeticException; +import jscl.MathEngine; +import jscl.NumeralBase; +import jscl.NumeralBaseException; +import jscl.math.Generic; +import jscl.math.function.IConstant; +import jscl.math.operator.Operator; +import jscl.text.ParseInterruptedException; + public class CalculatorImpl implements Calculator, CalculatorEventListener { // one minute @@ -472,15 +477,19 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { if (TextUtils.isEmpty(text)) { return; } + updateAnsVariable(text); + } + + private void updateAnsVariable(@NonNull String value) { final EntitiesRegistry varsRegistry = Locator.getInstance().getEngine().getVariablesRegistry(); - final IConstant ansVar = varsRegistry.get(VariablesRegistry.ANS); + final IConstant variable = varsRegistry.get(VariablesRegistry.ANS); - 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)); + final CppVariable.Builder b = variable != null ? CppVariable.builder(variable) : CppVariable.builder(VariablesRegistry.ANS); + b.withValue(value); + b.withSystem(true); + b.withDescription(CalculatorMessages.getBundle().getString(CalculatorMessages.ans_description)); - VariablesRegistry.saveVariable(varsRegistry, builder, ansVar, this, false); + VariablesRegistry.add(varsRegistry, b.build().toJsclBuilder(), variable, this); } @Subscribe diff --git a/app/src/main/java/org/solovyev/android/calculator/Category.java b/app/src/main/java/org/solovyev/android/calculator/Category.java deleted file mode 100644 index aa3f78f7..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/Category.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.solovyev.android.calculator; - -import android.support.annotation.StringRes; - -import javax.annotation.Nonnull; - -public interface Category { - - int ordinal(); - - @Nonnull - String name(); - - @StringRes - int title(); -} diff --git a/app/src/main/java/org/solovyev/android/calculator/EntitiesRegistry.java b/app/src/main/java/org/solovyev/android/calculator/EntitiesRegistry.java index becd2116..65187065 100644 --- a/app/src/main/java/org/solovyev/android/calculator/EntitiesRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/EntitiesRegistry.java @@ -22,6 +22,7 @@ package org.solovyev.android.calculator; +import org.solovyev.android.calculator.entities.Category; import org.solovyev.common.math.MathEntity; import org.solovyev.common.math.MathRegistry; diff --git a/app/src/main/java/org/solovyev/android/calculator/FunctionsRegistry.java b/app/src/main/java/org/solovyev/android/calculator/FunctionsRegistry.java index 379f346a..bda58f7e 100644 --- a/app/src/main/java/org/solovyev/android/calculator/FunctionsRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/FunctionsRegistry.java @@ -24,64 +24,55 @@ package org.solovyev.android.calculator; import android.support.annotation.NonNull; -import org.json.JSONArray; -import org.json.JSONException; import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; import org.solovyev.android.Check; +import org.solovyev.android.calculator.entities.Category; +import org.solovyev.android.calculator.entities.Entities; import org.solovyev.android.calculator.function.CppFunction; +import org.solovyev.android.calculator.function.FunctionCategory; import org.solovyev.android.calculator.json.Json; -import org.solovyev.android.calculator.model.OldFunction; -import org.solovyev.android.calculator.model.OldFunctions; +import org.solovyev.android.calculator.json.Jsonable; +import org.solovyev.android.calculator.function.OldFunctions; 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 jscl.JsclMathEngine; import jscl.math.function.CustomFunction; import jscl.math.function.Function; +import jscl.math.function.IFunction; import static android.text.TextUtils.isEmpty; @Singleton -public class FunctionsRegistry extends BaseEntitiesRegistry { +public class FunctionsRegistry extends BaseEntitiesRegistry { @Nonnull private static final Map substitutes = new HashMap<>(); - @Nonnull - private static final String FUNCTION_DESCRIPTION_PREFIX = "c_fun_description_"; static { substitutes.put("√", "sqrt"); } - @NonNull - private final WriteTask writeTask = new WriteTask(); - @Inject - @Named(AppModule.THREAD_BACKGROUND) - Executor backgroundThread; @Inject Calculator calculator; @Inject public FunctionsRegistry(@Nonnull JsclMathEngine mathEngine) { - super(mathEngine.getFunctionsRegistry(), FUNCTION_DESCRIPTION_PREFIX, null); + super(mathEngine.getFunctionsRegistry(), "c_fun_description_"); } public void add(@NonNull JBuilder builder, @Nullable Function oldFunction) { @@ -106,8 +97,8 @@ public class FunctionsRegistry extends BaseEntitiesRegistry builder) { - final Function function = super.add(builder); - if (isInitialized()) { - save(); + protected Jsonable toJsonable(@NonNull Function function) { + if (function instanceof IFunction) { + return CppFunction.builder((IFunction) function).build(); } - return function; - } - - @NonNull - private List loadFunctions() { - try { - return Json.load(getFunctionsFile(), CppFunction.JSON_CREATOR); - } catch (IOException | JSONException e) { - errorReporter.onException(e); - } - return Collections.emptyList(); + return null; } private void migrateOldFunctions() { @@ -151,7 +131,7 @@ public class FunctionsRegistry extends BaseEntitiesRegistry functions = OldFunctions.toCppFunctions(oldFunctions); // todo serso: fix multiplication sign issue - FileSaver.save(getFunctionsFile(), Json.toJson(functions).toString()); + FileSaver.save(getEntitiesFile(), Json.toJson(functions).toString()); } preferences.edit().remove(OldFunctions.PREFS_KEY).apply(); } catch (Exception e) { @@ -160,13 +140,8 @@ public class FunctionsRegistry extends BaseEntitiesRegistry createBuilder(@Nonnull OldFunction function) { - throw new UnsupportedOperationException(); - } - - @Override - protected OldFunction transform(@Nonnull Function function) { - if (function instanceof CustomFunction) { - return OldFunction.fromIFunction((CustomFunction) function); - } else { - return null; - } - } - - @Nonnull - @Override - protected PersistedEntitiesContainer createPersistenceContainer() { - return new OldFunctions(); - } - - private class WriteTask implements Runnable { - - @Override - public void run() { - Check.isMainThread(); - final List functions = new ArrayList<>(); - for (Function function : getEntities()) { - if (function.isSystem()) { - continue; - } - if (function instanceof CustomFunction) { - functions.add(CppFunction.builder((CustomFunction) function).build()); - } - } - backgroundThread.execute(new Runnable() { - @Override - public void run() { - final JSONArray array = Json.toJson(functions); - try { - FileSaver.save(getFunctionsFile(), array.toString()); - } catch (IOException e) { - errorReporter.onException(e); - } - } - }); - } - } - public static final class RemovedEvent { @NonNull public final Function function; diff --git a/app/src/main/java/org/solovyev/android/calculator/OperatorsRegistry.java b/app/src/main/java/org/solovyev/android/calculator/OperatorsRegistry.java index f60ddf7a..0417c67f 100644 --- a/app/src/main/java/org/solovyev/android/calculator/OperatorsRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/OperatorsRegistry.java @@ -22,20 +22,28 @@ package org.solovyev.android.calculator; -import jscl.math.operator.Operator; -import org.solovyev.common.JBuilder; +import android.support.annotation.NonNull; + +import org.solovyev.android.calculator.entities.Category; +import org.solovyev.android.calculator.entities.Entities; +import org.solovyev.android.calculator.function.FunctionCategory; +import org.solovyev.android.calculator.json.Jsonable; +import org.solovyev.android.calculator.operators.OperatorCategory; import org.solovyev.common.math.MathRegistry; -import javax.annotation.Nonnull; +import java.io.File; import java.util.HashMap; import java.util.Map; -public class OperatorsRegistry extends BaseEntitiesRegistry { +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import jscl.math.operator.Operator; + +public class OperatorsRegistry extends BaseEntitiesRegistry { @Nonnull private static final Map substitutes = new HashMap(); - @Nonnull - private static final String OPERATOR_DESCRIPTION_PREFIX = "c_op_description_"; static { substitutes.put("Σ", "sum"); @@ -47,7 +55,7 @@ public class OperatorsRegistry extends BaseEntitiesRegistry functionsRegistry) { - super(functionsRegistry, OPERATOR_DESCRIPTION_PREFIX, null); + super(functionsRegistry, "c_op_description_"); } @Nonnull @@ -56,30 +64,20 @@ public class OperatorsRegistry extends BaseEntitiesRegistry createBuilder(@Nonnull PersistedEntity entity) { - return null; //To change body of implemented methods use File | Settings | File Templates. + protected File getEntitiesFile() { + return null; } @Override - protected PersistedEntity transform(@Nonnull Operator entity) { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Nonnull - @Override - protected PersistedEntitiesContainer createPersistenceContainer() { - return null; //To change body of implemented methods use File | Settings | File Templates. + public Category getCategory(@Nonnull Operator operator) { + return Entities.getCategory(operator, OperatorCategory.values()); } } diff --git a/app/src/main/java/org/solovyev/android/calculator/PersistedEntitiesContainer.java b/app/src/main/java/org/solovyev/android/calculator/PersistedEntitiesContainer.java deleted file mode 100644 index 0e3bcb55..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/PersistedEntitiesContainer.java +++ /dev/null @@ -1,29 +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; - -import java.util.List; - -public interface PersistedEntitiesContainer { - List getEntities(); -} diff --git a/app/src/main/java/org/solovyev/android/calculator/PostfixFunctionsRegistry.java b/app/src/main/java/org/solovyev/android/calculator/PostfixFunctionsRegistry.java index e940e159..89e72b07 100644 --- a/app/src/main/java/org/solovyev/android/calculator/PostfixFunctionsRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/PostfixFunctionsRegistry.java @@ -22,20 +22,27 @@ package org.solovyev.android.calculator; -import jscl.math.operator.Operator; -import org.solovyev.common.JBuilder; +import android.support.annotation.NonNull; + +import org.solovyev.android.calculator.entities.Category; +import org.solovyev.android.calculator.entities.Entities; +import org.solovyev.android.calculator.json.Jsonable; +import org.solovyev.android.calculator.operators.OperatorCategory; import org.solovyev.common.math.MathRegistry; -import javax.annotation.Nonnull; +import java.io.File; import java.util.HashMap; import java.util.Map; -public class PostfixFunctionsRegistry extends BaseEntitiesRegistry { +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import jscl.math.operator.Operator; + +public class PostfixFunctionsRegistry extends BaseEntitiesRegistry { @Nonnull private static final Map substitutes = new HashMap(); - @Nonnull - private static final String POSTFIX_FUNCTION_DESCRIPTION_PREFIX = "c_pf_description_"; static { substitutes.put("%", "percent"); @@ -45,7 +52,7 @@ public class PostfixFunctionsRegistry extends BaseEntitiesRegistry functionsRegistry) { - super(functionsRegistry, POSTFIX_FUNCTION_DESCRIPTION_PREFIX, null); + super(functionsRegistry, "c_pf_description_"); } @Nonnull @@ -54,30 +61,20 @@ public class PostfixFunctionsRegistry extends BaseEntitiesRegistry createBuilder(@Nonnull PersistedEntity entity) { - throw new UnsupportedOperationException(); - } - - @Override - protected PersistedEntity transform(@Nonnull Operator entity) { - return null; - } - - @Nonnull - @Override - protected PersistedEntitiesContainer createPersistenceContainer() { - throw new UnsupportedOperationException(); + return Entities.getCategory(operator, OperatorCategory.values()); } } diff --git a/app/src/main/java/org/solovyev/android/calculator/VariablesRegistry.java b/app/src/main/java/org/solovyev/android/calculator/VariablesRegistry.java index 68d9af9c..9b9eab03 100644 --- a/app/src/main/java/org/solovyev/android/calculator/VariablesRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/VariablesRegistry.java @@ -24,15 +24,18 @@ package org.solovyev.android.calculator; import android.support.annotation.NonNull; +import com.google.common.base.Strings; + import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; import org.solovyev.android.Check; +import org.solovyev.android.calculator.entities.Category; +import org.solovyev.android.calculator.entities.Entities; import org.solovyev.android.calculator.json.Json; -import org.solovyev.android.calculator.model.MathEntityBuilder; -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.json.Jsonable; import org.solovyev.android.calculator.variables.CppVariable; +import org.solovyev.android.calculator.variables.OldVars; +import org.solovyev.android.calculator.variables.VariablesCategory; import org.solovyev.android.io.FileSaver; import org.solovyev.common.JBuilder; import org.solovyev.common.math.MathEntity; @@ -50,10 +53,8 @@ import javax.inject.Singleton; import jscl.JsclMathEngine; import jscl.math.function.IConstant; -import static android.text.TextUtils.isEmpty; - @Singleton -public class VariablesRegistry extends BaseEntitiesRegistry { +public class VariablesRegistry extends BaseEntitiesRegistry { @Nonnull public static final String ANS = "ans"; @@ -71,19 +72,15 @@ public class VariablesRegistry extends BaseEntitiesRegistry { @Inject public VariablesRegistry(@Nonnull JsclMathEngine mathEngine) { - super(mathEngine.getConstantsRegistry(), "c_var_description_", null); + super(mathEngine.getConstantsRegistry(), "c_var_description_"); } - public static void saveVariable(@Nonnull EntitiesRegistry registry, - @Nonnull MathEntityBuilder builder, - @Nullable T editedInstance, - @Nonnull Object source, boolean save) { + public static void add(@Nonnull EntitiesRegistry registry, + @Nonnull JBuilder builder, + @Nullable T editedInstance, + @Nonnull Object source) { final T addedVar = registry.add(builder); - if (save) { - registry.save(); - } - if (editedInstance == null) { Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.constant_added, addedVar, source); } else { @@ -101,12 +98,16 @@ public class VariablesRegistry extends BaseEntitiesRegistry { Check.isNotMainThread(); try { - //migrateOldVariables(); + migrateOldVariables(); addSafely("x"); addSafely("y"); addSafely("t"); addSafely("j"); + + for (CppVariable variable : loadEntities(CppVariable.JSON_CREATOR)) { + addSafely(variable.toJsclBuilder()); + } } finally { setInitialized(); } @@ -114,7 +115,7 @@ public class VariablesRegistry extends BaseEntitiesRegistry { private void migrateOldVariables() { final String xml = preferences.getString(OldVars.PREFS_KEY, null); - if (isEmpty(xml)) { + if (Strings.isNullOrEmpty(xml)) { return; } try { @@ -122,44 +123,29 @@ public class VariablesRegistry extends BaseEntitiesRegistry { final OldVars oldVariables = serializer.read(OldVars.class, xml); if (oldVariables != null) { List variables = OldVars.toCppVariables(oldVariables); - FileSaver.save(getVariablesFile(), Json.toJson(variables).toString()); + FileSaver.save(getEntitiesFile(), Json.toJson(variables).toString()); } - preferences.edit().remove(OldFunctions.PREFS_KEY).apply(); + preferences.edit().remove(OldVars.PREFS_KEY).apply(); } catch (Exception e) { errorReporter.onException(e); } } + @Override @NonNull - private File getVariablesFile() { + protected File getEntitiesFile() { return new File(application.getFilesDir(), "variables.json"); } - @Nonnull + @Nullable @Override - protected JBuilder createBuilder(@Nonnull OldVar entity) { - return new OldVar.Builder(entity); - } - - @Nonnull - @Override - protected PersistedEntitiesContainer createPersistenceContainer() { - return new OldVars(); + protected Jsonable toJsonable(@NonNull IConstant constant) { + return CppVariable.builder(constant).build(); } private void addSafely(@Nonnull String name) { if (!contains(name)) { - addSafely(new OldVar.Builder(name, (String) null)); - } - } - - @Nonnull - @Override - protected OldVar transform(@Nonnull IConstant entity) { - if (entity instanceof OldVar) { - return (OldVar) entity; - } else { - return new OldVar.Builder(entity).create(); + addSafely(CppVariable.builder(name).build().toJsclBuilder()); } } @@ -174,13 +160,7 @@ public class VariablesRegistry extends BaseEntitiesRegistry { } @Override - public Category getCategory(@Nonnull IConstant var) { - for (VarCategory category : VarCategory.values()) { - if (category.isInCategory(var)) { - return category; - } - } - - return null; + public Category getCategory(@Nonnull IConstant variable) { + return Entities.getCategory(variable, VariablesCategory.values()); } } diff --git a/app/src/main/java/org/solovyev/android/calculator/entities/Category.java b/app/src/main/java/org/solovyev/android/calculator/entities/Category.java new file mode 100644 index 00000000..bab2a075 --- /dev/null +++ b/app/src/main/java/org/solovyev/android/calculator/entities/Category.java @@ -0,0 +1,20 @@ +package org.solovyev.android.calculator.entities; + +import android.support.annotation.StringRes; + +import org.solovyev.common.math.MathEntity; + +import javax.annotation.Nonnull; + +public interface Category { + + int ordinal(); + + @Nonnull + String name(); + + boolean isInCategory(@Nonnull E entity); + + @StringRes + int title(); +} diff --git a/app/src/main/java/org/solovyev/android/calculator/entities/Entities.java b/app/src/main/java/org/solovyev/android/calculator/entities/Entities.java new file mode 100644 index 00000000..00fc61e9 --- /dev/null +++ b/app/src/main/java/org/solovyev/android/calculator/entities/Entities.java @@ -0,0 +1,25 @@ +package org.solovyev.android.calculator.entities; + +import android.support.annotation.NonNull; + +import org.solovyev.common.math.MathEntity; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public final class Entities { + + private Entities() { + } + + @Nullable + public static > Category getCategory(@Nonnull E entity, @NonNull C[] categories) { + for (C category : categories) { + if (category.isInCategory(entity)) { + return category; + } + } + + return null; + } +} diff --git a/app/src/main/java/org/solovyev/android/calculator/function/CppFunction.java b/app/src/main/java/org/solovyev/android/calculator/function/CppFunction.java index b3675ac5..edc36353 100644 --- a/app/src/main/java/org/solovyev/android/calculator/function/CppFunction.java +++ b/app/src/main/java/org/solovyev/android/calculator/function/CppFunction.java @@ -4,21 +4,26 @@ import android.os.Parcel; import android.os.Parcelable; import android.support.annotation.NonNull; import android.text.TextUtils; -import jscl.math.function.CustomFunction; -import jscl.math.function.IFunction; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.solovyev.android.Check; import org.solovyev.android.calculator.json.Json; import org.solovyev.android.calculator.json.Jsonable; +import org.solovyev.common.JBuilder; import org.solovyev.common.text.Strings; -import javax.annotation.Nonnull; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import javax.annotation.Nonnull; + +import jscl.math.function.CustomFunction; +import jscl.math.function.Function; +import jscl.math.function.IFunction; + public class CppFunction implements Jsonable, Parcelable { public static final Json.Creator JSON_CREATOR = new Json.Creator() { @@ -46,7 +51,7 @@ public class CppFunction implements Jsonable, Parcelable { private static final String JSON_DESCRIPTION = "d"; @Nonnull protected final List parameters = new ArrayList<>(); - protected int id; + protected int id = NO_ID; @Nonnull protected String name; @Nonnull @@ -55,13 +60,11 @@ public class CppFunction implements Jsonable, Parcelable { protected String description = ""; private CppFunction(@Nonnull String name, @Nonnull String body) { - this.id = NO_ID; this.name = name; this.body = body; } private CppFunction(@NonNull JSONObject json) throws JSONException { - id = NO_ID; final JSONArray array = json.optJSONArray(JSON_PARAMETERS); if (array != null) { for (int i = 0; i < array.length(); i++) { @@ -180,7 +183,7 @@ public class CppFunction implements Jsonable, Parcelable { } @Nonnull - public CustomFunction.Builder toCustomFunctionBuilder() { + public JBuilder toJsclBuilder() { final CustomFunction.Builder builder = new CustomFunction.Builder(name, parameters, body); builder.setDescription(description); if (id != NO_ID) { @@ -235,13 +238,6 @@ public class CppFunction implements Jsonable, Parcelable { return this; } - @Nonnull - public Builder withBody(@NonNull String body) { - Check.isTrue(!built); - function.body = body; - return this; - } - @Nonnull public CppFunction build() { built = true; diff --git a/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java b/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java index 97f75de3..39870fd6 100644 --- a/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java @@ -78,7 +78,6 @@ import javax.inject.Inject; import butterknife.Bind; import butterknife.ButterKnife; -import jscl.math.function.CustomFunction; import jscl.math.function.Function; import static org.solovyev.android.calculator.function.CppFunction.NO_ID; @@ -213,7 +212,7 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC showRemovalDialog(function); return; } - final CustomFunction entity = function.toCustomFunctionBuilder().create(); + final Function entity = function.toJsclBuilder().create(); functionsRegistry.remove(entity); calculator.fireCalculatorEvent(CalculatorEventType.function_removed, entity, this); dismiss(); @@ -317,7 +316,7 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC .withParameters(collectParameters()) .withDescription(descriptionView.getText().toString()).build(); final Function oldFunction = isNewFunction() ? null : functionsRegistry.getById(function.id); - functionsRegistry.add(newFunction.toCustomFunctionBuilder(), oldFunction); + functionsRegistry.add(newFunction.toJsclBuilder(), oldFunction); return true; } catch (RuntimeException e) { setError(bodyLabel, e.getLocalizedMessage()); diff --git a/app/src/main/java/org/solovyev/android/calculator/FunctionCategory.java b/app/src/main/java/org/solovyev/android/calculator/function/FunctionCategory.java similarity index 92% rename from app/src/main/java/org/solovyev/android/calculator/FunctionCategory.java rename to app/src/main/java/org/solovyev/android/calculator/function/FunctionCategory.java index 6061a39c..06560ded 100644 --- a/app/src/main/java/org/solovyev/android/calculator/FunctionCategory.java +++ b/app/src/main/java/org/solovyev/android/calculator/function/FunctionCategory.java @@ -20,20 +20,25 @@ * Site: http://se.solovyev.org */ -package org.solovyev.android.calculator; +package org.solovyev.android.calculator.function; import android.support.annotation.StringRes; + +import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.entities.Category; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import javax.annotation.Nonnull; + import jscl.math.function.ArcTrigonometric; import jscl.math.function.Comparison; import jscl.math.function.Function; import jscl.math.function.Trigonometric; -import javax.annotation.Nonnull; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - -public enum FunctionCategory implements Category { +public enum FunctionCategory implements Category { my(R.string.c_fun_category_my) { @Override @@ -91,6 +96,4 @@ public enum FunctionCategory implements Category { public int title() { return title; } - - public abstract boolean isInCategory(@Nonnull Function function); } diff --git a/app/src/main/java/org/solovyev/android/calculator/PersistedEntity.java b/app/src/main/java/org/solovyev/android/calculator/function/OldFunction.java similarity index 51% rename from app/src/main/java/org/solovyev/android/calculator/PersistedEntity.java rename to app/src/main/java/org/solovyev/android/calculator/function/OldFunction.java index 0e5a98c6..100c944d 100644 --- a/app/src/main/java/org/solovyev/android/calculator/PersistedEntity.java +++ b/app/src/main/java/org/solovyev/android/calculator/function/OldFunction.java @@ -20,12 +20,42 @@ * Site: http://se.solovyev.org */ -package org.solovyev.android.calculator; +package org.solovyev.android.calculator.function; + +import org.simpleframework.xml.Element; +import org.simpleframework.xml.ElementList; +import org.simpleframework.xml.Root; +import org.simpleframework.xml.Transient; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; import javax.annotation.Nonnull; -public interface PersistedEntity { +@Root(name = "function") +public class OldFunction implements Serializable { + @Transient + public Integer id; + + @Element + public String name; + + @Element(name = "body") + public String content; + + @ElementList(type = String.class) @Nonnull - String getName(); + public List parameterNames = new ArrayList(); + + @Element + public boolean system; + + @Element(required = false) + @Nonnull + public String description = ""; + + public OldFunction() { + } } diff --git a/app/src/main/java/org/solovyev/android/calculator/model/OldFunctions.java b/app/src/main/java/org/solovyev/android/calculator/function/OldFunctions.java similarity index 66% rename from app/src/main/java/org/solovyev/android/calculator/model/OldFunctions.java rename to app/src/main/java/org/solovyev/android/calculator/function/OldFunctions.java index 3c255ab8..3fad7aa0 100644 --- a/app/src/main/java/org/solovyev/android/calculator/model/OldFunctions.java +++ b/app/src/main/java/org/solovyev/android/calculator/function/OldFunctions.java @@ -20,14 +20,12 @@ * Site: http://se.solovyev.org */ -package org.solovyev.android.calculator.model; +package org.solovyev.android.calculator.function; 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 java.util.ArrayList; import java.util.List; @@ -35,12 +33,13 @@ import java.util.List; import javax.annotation.Nonnull; @Root(name = "Functions") -public class OldFunctions implements PersistedEntitiesContainer { +public class OldFunctions { public static final String PREFS_KEY = "org.solovyev.android.calculator.CalculatorModel_functions"; - @ElementList(type = OldFunction.class) - private List functions = new ArrayList<>(); + @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") + @ElementList(type = OldFunction.class, name = "functions") + public List list = new ArrayList<>(); public OldFunctions() { } @@ -48,20 +47,16 @@ public class OldFunctions implements PersistedEntitiesContainer { @Nonnull public static List toCppFunctions(@Nonnull OldFunctions oldFunctions) { final List functions = new ArrayList<>(); - for (OldFunction oldFunction : oldFunctions.functions) { - final String name = oldFunction.getName(); - final String body = oldFunction.getContent(); + for (OldFunction oldFunction : oldFunctions.list) { + final String name = oldFunction.name; + final String body = oldFunction.content; if (TextUtils.isEmpty(name) || TextUtils.isEmpty(body)) { continue; } functions.add(CppFunction.builder(name, body) - .withParameters(oldFunction.getParameterNames()) - .withDescription(oldFunction.getDescription()).build()); + .withParameters(oldFunction.parameterNames) + .withDescription(oldFunction.description).build()); } return functions; } - - public List getEntities() { - return functions; - } } diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/BaseEntitiesFragment.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/BaseEntitiesFragment.java index 96d45b14..2894a272 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/BaseEntitiesFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/BaseEntitiesFragment.java @@ -31,27 +31,35 @@ import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.text.ClipboardManager; import android.text.TextUtils; -import android.view.*; +import android.view.ContextMenu; +import android.view.LayoutInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; import android.widget.TextView; -import butterknife.Bind; -import butterknife.ButterKnife; + import com.melnykov.fab.FloatingActionButton; + import org.solovyev.android.Check; import org.solovyev.android.calculator.BaseFragment; import org.solovyev.android.calculator.CalculatorFragmentType; -import org.solovyev.android.calculator.Category; +import org.solovyev.android.calculator.entities.Category; import org.solovyev.android.calculator.R; import org.solovyev.android.views.llm.DividerItemDecoration; import org.solovyev.common.math.MathEntity; import org.solovyev.common.text.Strings; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import butterknife.Bind; +import butterknife.ButterKnife; + public abstract class BaseEntitiesFragment extends BaseFragment { @@ -221,7 +229,7 @@ public abstract class BaseEntitiesFragment extends BaseFra public void bind(@Nonnull E entity) { this.entity = entity; - textView.setText(String.valueOf(entity)); + textView.setText(entity.getName()); final String description = getDescription(entity); if (!Strings.isEmpty(description)) { diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java index abbb097b..279c4a26 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java @@ -31,7 +31,7 @@ import org.solovyev.android.calculator.CalculatorEventListener; import org.solovyev.android.calculator.CalculatorEventType; import org.solovyev.android.calculator.CalculatorFragmentType; import org.solovyev.android.calculator.R; -import org.solovyev.android.calculator.VarCategory; +import org.solovyev.android.calculator.variables.VariablesCategory; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -57,11 +57,11 @@ public class CalculatorVarsActivity extends BaseActivity implements CalculatorEv final CalculatorFragmentType fragmentType = CalculatorFragmentType.variables; - for (VarCategory category : VarCategory.values()) { + for (VariablesCategory category : VariablesCategory.values()) { final Bundle fragmentParameters; - if (category == VarCategory.my && bundle != null) { + if (category == VariablesCategory.my && bundle != null) { BaseEntitiesFragment.putCategory(bundle, category.name()); fragmentParameters = bundle; } else { diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsActivity.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsActivity.java index b9d256dc..def4542d 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsActivity.java @@ -26,7 +26,7 @@ import android.os.Bundle; import android.os.Parcelable; import org.solovyev.android.calculator.BaseActivity; import org.solovyev.android.calculator.CalculatorFragmentType; -import org.solovyev.android.calculator.FunctionCategory; +import org.solovyev.android.calculator.function.FunctionCategory; import org.solovyev.android.calculator.R; import javax.annotation.Nullable; diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsFragment.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsFragment.java index 47a8309f..81491619 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsFragment.java @@ -31,6 +31,7 @@ import com.squareup.otto.Subscribe; import jscl.math.function.Function; import jscl.math.function.IFunction; import org.solovyev.android.calculator.*; +import org.solovyev.android.calculator.entities.Category; import org.solovyev.android.calculator.function.CppFunction; import org.solovyev.android.calculator.function.EditFunctionFragment; import org.solovyev.common.text.Strings; diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsActivity.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsActivity.java index 5e3a1181..5cca9877 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsActivity.java @@ -29,7 +29,7 @@ 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.OperatorCategory; +import org.solovyev.android.calculator.operators.OperatorCategory; import org.solovyev.android.calculator.R; import javax.annotation.Nonnull; diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsFragment.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsFragment.java index 765b1c9e..0c51c80c 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsFragment.java @@ -27,6 +27,7 @@ import android.view.ContextMenu; import android.view.MenuItem; import jscl.math.operator.Operator; import org.solovyev.android.calculator.*; +import org.solovyev.android.calculator.entities.Category; import org.solovyev.common.text.Strings; import javax.annotation.Nonnull; diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/VarEditDialogFragment.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/VarEditDialogFragment.java index a8b0036b..450b7080 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/VarEditDialogFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/VarEditDialogFragment.java @@ -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.OldVar; +import org.solovyev.android.calculator.variables.OldVar; import org.solovyev.common.text.Strings; import java.util.Arrays; diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/VarEditorSaver.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/VarEditorSaver.java index be260b5a..10b79aa8 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/VarEditorSaver.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/VarEditorSaver.java @@ -145,7 +145,7 @@ public class VarEditorSaver implements View.OnClickListene if (error != null) { Locator.getInstance().getNotifier().showMessage(error, MessageType.error); } else { - VariablesRegistry.saveVariable(mathRegistry, varBuilder, editedInstance, source, true); + VariablesRegistry.add(mathRegistry, varBuilder, editedInstance, source); } } } diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/VariablesFragment.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/VariablesFragment.java index 592bb9f4..a335dd55 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/VariablesFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/VariablesFragment.java @@ -29,7 +29,6 @@ import android.view.ContextMenu; import android.view.MenuItem; import android.view.View; -import com.melnykov.fab.FloatingActionButton; import com.squareup.otto.Bus; import org.solovyev.android.calculator.AppComponent; @@ -38,7 +37,7 @@ 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.entities.Category; import org.solovyev.android.calculator.Change; import org.solovyev.android.calculator.Keyboard; import org.solovyev.android.calculator.Locator; @@ -114,7 +113,6 @@ public class VariablesFragment extends BaseEntitiesFragment implement public void onViewCreated(View root, Bundle savedInstanceState) { super.onViewCreated(root, savedInstanceState); - final FloatingActionButton fab = (FloatingActionButton) root.findViewById(R.id.fab); fab.setVisibility(View.VISIBLE); fab.attachToRecyclerView(recyclerView); fab.setOnClickListener(new View.OnClickListener() { diff --git a/app/src/main/java/org/solovyev/android/calculator/model/EntityDao.java b/app/src/main/java/org/solovyev/android/calculator/model/EntityDao.java deleted file mode 100644 index 5122be79..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/model/EntityDao.java +++ /dev/null @@ -1,81 +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 android.content.SharedPreferences; -import org.simpleframework.xml.Serializer; -import org.simpleframework.xml.core.Persister; -import org.solovyev.android.calculator.PersistedEntitiesContainer; -import org.solovyev.android.calculator.PersistedEntity; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.io.StringWriter; - -public class EntityDao { - - @Nonnull - private final String preferenceString; - - @Nonnull - private final Class> persistenceContainerClass; - @Nonnull - private final SharedPreferences preferences; - - public EntityDao(@Nonnull String preferenceString, - @Nonnull Class> persistenceContainerClass, @Nonnull SharedPreferences preferences) { - this.preferenceString = preferenceString; - this.persistenceContainerClass = persistenceContainerClass; - this.preferences = preferences; - } - - public void save(@Nonnull PersistedEntitiesContainer container) { - final SharedPreferences.Editor editor = preferences.edit(); - - final StringWriter sw = new StringWriter(); - final Serializer serializer = new Persister(); - try { - serializer.write(container, sw); - } catch (Exception e) { - throw new RuntimeException(e); - } - - editor.putString(preferenceString, sw.toString()); - editor.apply(); - } - - @Nullable - public PersistedEntitiesContainer load() { - final String value = preferences.getString(preferenceString, null); - if (value != null) { - final Serializer serializer = new Persister(); - try { - return serializer.read(persistenceContainerClass, value); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - return null; - } -} diff --git a/app/src/main/java/org/solovyev/android/calculator/model/OldFunction.java b/app/src/main/java/org/solovyev/android/calculator/model/OldFunction.java deleted file mode 100644 index 64f62d13..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/model/OldFunction.java +++ /dev/null @@ -1,159 +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.Element; -import org.simpleframework.xml.ElementList; -import org.simpleframework.xml.Root; -import org.simpleframework.xml.Transient; -import org.solovyev.android.calculator.PersistedEntity; -import org.solovyev.common.math.MathEntity; -import org.solovyev.common.text.Strings; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; - -import javax.annotation.Nonnull; - -import jscl.math.function.IFunction; - -@Root(name = "function") -public class OldFunction implements IFunction, PersistedEntity, Serializable { - - @Transient - private Integer id; - - @Element - private String name; - - @Element(name = "body") - private String content; - - @ElementList(type = String.class) - @Nonnull - private List parameterNames = new ArrayList(); - - @Element - private boolean system; - - @Element(required = false) - @Nonnull - private String description = ""; - - public OldFunction() { - } - - public static OldFunction fromIFunction(@Nonnull IFunction function) { - final OldFunction result = new OldFunction(); - - copy(result, function); - - return result; - } - - private static void copy(@Nonnull OldFunction target, - @Nonnull IFunction source) { - target.name = source.getName(); - target.content = source.getContent(); - target.description = Strings.getNotEmpty(source.getDescription(), ""); - target.system = source.isSystem(); - if (source.isIdDefined()) { - target.id = source.getId(); - } - target.parameterNames = new ArrayList<>(source.getParameterNames()); - } - - @Override - public void copy(@Nonnull MathEntity mathEntity) { - if (mathEntity instanceof IFunction) { - copy(this, (IFunction) mathEntity); - } else { - throw new IllegalArgumentException("Trying to make a copy of unsupported type: " + mathEntity.getClass()); - } - } - - @Override - public String toJava() { - return String.valueOf(this.content); - } - - @Override - public String toString() { - return "AFunction{" + - "name='" + name + '\'' + - ", parameterNames=" + parameterNames + - ", content='" + content + '\'' + - '}'; - } - - @Nonnull - public String getName() { - return name; - } - - public void setName(@Nonnull String name) { - this.name = name; - } - - @Override - public boolean isSystem() { - return system; - } - - @Nonnull - @Override - public Integer getId() { - return this.id; - } - - @Override - public void setId(@Nonnull Integer id) { - this.id = id; - } - - @Override - public boolean isIdDefined() { - return this.id != null; - } - - @Nonnull - public String getContent() { - return content; - } - - public void setContent(@Nonnull String content) { - this.content = content; - } - - @Nonnull - @Override - public String getDescription() { - return this.description; - } - - @Nonnull - public List getParameterNames() { - return parameterNames; - } -} diff --git a/app/src/main/java/org/solovyev/android/calculator/OperatorCategory.java b/app/src/main/java/org/solovyev/android/calculator/operators/OperatorCategory.java similarity index 82% rename from app/src/main/java/org/solovyev/android/calculator/OperatorCategory.java rename to app/src/main/java/org/solovyev/android/calculator/operators/OperatorCategory.java index 315231b4..20c9eb90 100644 --- a/app/src/main/java/org/solovyev/android/calculator/OperatorCategory.java +++ b/app/src/main/java/org/solovyev/android/calculator/operators/OperatorCategory.java @@ -20,14 +20,23 @@ * Site: http://se.solovyev.org */ -package org.solovyev.android.calculator; +package org.solovyev.android.calculator.operators; import android.support.annotation.StringRes; -import jscl.math.operator.*; + +import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.entities.Category; import javax.annotation.Nonnull; -public enum OperatorCategory implements Category { +import jscl.math.operator.Derivative; +import jscl.math.operator.IndefiniteIntegral; +import jscl.math.operator.Integral; +import jscl.math.operator.Operator; +import jscl.math.operator.Product; +import jscl.math.operator.Sum; + +public enum OperatorCategory implements Category { my(R.string.c_fun_category_my) { @Override @@ -72,8 +81,6 @@ public enum OperatorCategory implements Category { this.title = title; } - public abstract boolean isInCategory(@Nonnull Operator operator); - @Override public int title() { return title; diff --git a/app/src/main/java/org/solovyev/android/calculator/variables/CppVariable.java b/app/src/main/java/org/solovyev/android/calculator/variables/CppVariable.java index a13c5800..6161741c 100644 --- a/app/src/main/java/org/solovyev/android/calculator/variables/CppVariable.java +++ b/app/src/main/java/org/solovyev/android/calculator/variables/CppVariable.java @@ -1,15 +1,171 @@ package org.solovyev.android.calculator.variables; +import android.support.annotation.NonNull; +import android.text.TextUtils; + import org.json.JSONException; import org.json.JSONObject; +import org.solovyev.android.Check; +import org.solovyev.android.calculator.function.CppFunction; +import org.solovyev.android.calculator.json.Json; import org.solovyev.android.calculator.json.Jsonable; +import org.solovyev.common.JBuilder; import javax.annotation.Nonnull; +import jscl.math.function.IConstant; + +import static com.google.common.base.Strings.nullToEmpty; + public class CppVariable implements Jsonable { + + public static final Json.Creator JSON_CREATOR = new Json.Creator() { + @NonNull + @Override + public CppVariable create(@NonNull JSONObject json) throws JSONException { + return new CppVariable(json); + } + }; + + private static final String JSON_NAME = "n"; + private static final String JSON_VALUE = "v"; + private static final String JSON_DESCRIPTION = "d"; + + protected int id = CppFunction.NO_ID; + @Nonnull + protected String name; + @Nonnull + protected String value = ""; + @Nonnull + protected String description = ""; + protected boolean system; + + private CppVariable(@Nonnull String name) { + this.name = name; + } + + protected CppVariable(@Nonnull CppVariable that) { + this.id = that.id; + this.name = that.name; + this.value = that.value; + this.description = that.description; + this.system = that.system; + } + + protected CppVariable(@NonNull IConstant that) { + id = that.isIdDefined() ? that.getId() : CppFunction.NO_ID; + name = that.getName(); + value = nullToEmpty(that.getValue()); + description = nullToEmpty(that.getDescription()); + system = that.isSystem(); + } + + private CppVariable(@NonNull JSONObject json) throws JSONException { + this.name = json.getString(JSON_NAME); + this.value = json.optString(JSON_DESCRIPTION); + this.description = json.optString(JSON_DESCRIPTION); + } + + @NonNull + public static CppVariable.Builder builder(@NonNull String name) { + return new Builder(name); + } + + @NonNull + public static Builder builder(@NonNull IConstant constant) { + return new Builder(constant); + } + @Nonnull @Override public JSONObject toJson() throws JSONException { - return null; + final JSONObject json = new JSONObject(); + json.put(JSON_NAME, name); + if (!TextUtils.isEmpty(value)) { + json.put(JSON_VALUE, value); + } + if (!TextUtils.isEmpty(description)) { + json.put(JSON_DESCRIPTION, description); + } + return json; + } + + @Nonnull + public JBuilder toJsclBuilder() { + return new JBuilder() { + @Nonnull + @Override + public IConstant create() { + return new JsclConstant(CppVariable.this); + } + }; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof CppVariable)) return false; + + CppVariable that = (CppVariable) o; + + if (id != that.id) return false; + if (!name.equals(that.name)) return false; + + return true; + } + + @Override + public int hashCode() { + int result = id; + result = 31 * result + name.hashCode(); + return result; + } + + public static class Builder { + @NonNull + private final CppVariable variable; + private boolean built; + + private Builder(@NonNull String name) { + variable = new CppVariable(name); + } + + private Builder(@NonNull IConstant constant) { + variable = new CppVariable(constant); + } + + @Nonnull + public Builder withDescription(@Nonnull String description) { + Check.isTrue(!built); + variable.description = description; + return this; + } + + @Nonnull + public Builder withValue(@NonNull String value) { + Check.isTrue(!built); + variable.value = value; + return this; + } + + @Nonnull + public Builder withSystem(boolean system) { + Check.isTrue(!built); + variable.system = system; + return this; + } + + @Nonnull + public Builder withId(int id) { + Check.isTrue(!built); + variable.id = id; + return this; + } + + @Nonnull + public CppVariable build() { + built = true; + return variable; + } } } diff --git a/app/src/main/java/org/solovyev/android/calculator/variables/JsclConstant.java b/app/src/main/java/org/solovyev/android/calculator/variables/JsclConstant.java new file mode 100644 index 00000000..2203f372 --- /dev/null +++ b/app/src/main/java/org/solovyev/android/calculator/variables/JsclConstant.java @@ -0,0 +1,116 @@ +package org.solovyev.android.calculator.variables; + +import com.google.common.base.Strings; + +import org.solovyev.android.calculator.function.CppFunction; +import org.solovyev.common.math.MathEntity; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import jscl.math.function.Constant; +import jscl.math.function.IConstant; + +class JsclConstant extends CppVariable implements IConstant { + + private Double doubleValue; + private Constant constant; + + JsclConstant(@Nonnull CppVariable variable) { + super(variable); + } + + @Nonnull + @Override + public Constant getConstant() { + if (constant == null) { + constant = new Constant(name); + } + return constant; + } + + @Nullable + @Override + public String getDescription() { + return description; + } + + @Override + public boolean isDefined() { + return !Strings.isNullOrEmpty(value); + } + + @Nullable + @Override + public String getValue() { + return value; + } + + @Nullable + @Override + public Double getDoubleValue() { + if (doubleValue != null) { + return doubleValue; + } + if (!Strings.isNullOrEmpty(value)) { + try { + doubleValue = Double.valueOf(value); + } catch (NumberFormatException e) { + // do nothing - string is not a double + } + } + return doubleValue; + } + + @Nonnull + @Override + public String toJava() { + return Strings.nullToEmpty(value); + } + + @Nonnull + @Override + public String getName() { + return name; + } + + @Override + public boolean isSystem() { + return system; + } + + @Nonnull + @Override + public Integer getId() { + return id == CppFunction.NO_ID ? null : id; + } + + @Override + public void setId(@Nonnull Integer id) { + this.id = id; + } + + @Override + public boolean isIdDefined() { + return id != CppFunction.NO_ID; + } + + @Override + public void copy(@Nonnull MathEntity o) { + if (!(o instanceof IConstant)) { + throw new IllegalArgumentException("Trying to make a copy of unsupported type: " + o.getClass()); + } + final IConstant that = ((IConstant) o); + this.name = that.getName(); + this.value = that.getValue(); + this.description = that.getDescription(); + this.system = that.isSystem(); + if (that.isIdDefined()) { + this.id = that.getId(); + } else { + this.id = CppFunction.NO_ID; + } + this.doubleValue = null; + this.constant = null; + } +} diff --git a/app/src/main/java/org/solovyev/android/calculator/model/OldVar.java b/app/src/main/java/org/solovyev/android/calculator/variables/OldVar.java similarity index 96% rename from app/src/main/java/org/solovyev/android/calculator/model/OldVar.java rename to app/src/main/java/org/solovyev/android/calculator/variables/OldVar.java index 4e198b9b..cb7d973f 100644 --- a/app/src/main/java/org/solovyev/android/calculator/model/OldVar.java +++ b/app/src/main/java/org/solovyev/android/calculator/variables/OldVar.java @@ -20,12 +20,12 @@ * Site: http://se.solovyev.org */ -package org.solovyev.android.calculator.model; +package org.solovyev.android.calculator.variables; import org.simpleframework.xml.Element; import org.simpleframework.xml.Root; import org.simpleframework.xml.Transient; -import org.solovyev.android.calculator.PersistedEntity; +import org.solovyev.android.calculator.model.MathEntityBuilder; import org.solovyev.common.JBuilder; import org.solovyev.common.math.MathEntity; import org.solovyev.common.text.Strings; @@ -38,7 +38,7 @@ import jscl.math.function.ExtendedConstant; import jscl.math.function.IConstant; @Root(name = "var") -public class OldVar implements IConstant, PersistedEntity { +public class OldVar implements IConstant { @Transient private Integer id; @@ -234,7 +234,7 @@ public class OldVar implements IConstant, PersistedEntity { return this; } - protected Builder setSystem(boolean system) { + public Builder setSystem(boolean system) { this.system = system; return this; } diff --git a/app/src/main/java/org/solovyev/android/calculator/model/OldVars.java b/app/src/main/java/org/solovyev/android/calculator/variables/OldVars.java similarity index 63% rename from app/src/main/java/org/solovyev/android/calculator/model/OldVars.java rename to app/src/main/java/org/solovyev/android/calculator/variables/OldVars.java index 33454ab6..29f753c8 100644 --- a/app/src/main/java/org/solovyev/android/calculator/model/OldVars.java +++ b/app/src/main/java/org/solovyev/android/calculator/variables/OldVars.java @@ -20,45 +20,43 @@ * Site: http://se.solovyev.org */ -package org.solovyev.android.calculator.model; +package org.solovyev.android.calculator.variables; + +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.variables.CppVariable; import java.util.ArrayList; import java.util.List; import javax.annotation.Nonnull; +import static com.google.common.base.Strings.nullToEmpty; + @Root(name = "vars") -public class OldVars implements PersistedEntitiesContainer { +public class OldVars { public static final String PREFS_KEY = "org.solovyev.android.calculator.CalculatorModel_vars"; - @ElementList(type = OldVar.class) - private List vars = new ArrayList(); + + @ElementList(type = OldVar.class, name = "vars") + public List list = new ArrayList(); public OldVars() { } - public List getEntities() { - return vars; - } - @Nonnull public static List toCppVariables(@Nonnull OldVars oldVariables) { final List variables = new ArrayList<>(); - /*for (OldVar oldVar : oldVariables.vars) { + for (OldVar oldVar : oldVariables.list) { final String name = oldVar.getName(); - final String body = oldVar.getContent(); - if (TextUtils.isEmpty(name) || TextUtils.isEmpty(body)) { + if (TextUtils.isEmpty(name)) { continue; } - variables.add(CppFunction.builder(name, body) - .withParameters(oldVar.getParameterNames()) - .withDescription(oldVar.getDescription()).build()); - }*/ + variables.add(CppVariable.builder(name) + .withValue(nullToEmpty(oldVar.getValue())) + .withDescription(nullToEmpty(oldVar.getDescription())).build()); + } return variables; } } diff --git a/app/src/main/java/org/solovyev/android/calculator/VarCategory.java b/app/src/main/java/org/solovyev/android/calculator/variables/VariablesCategory.java similarity index 71% rename from app/src/main/java/org/solovyev/android/calculator/VarCategory.java rename to app/src/main/java/org/solovyev/android/calculator/variables/VariablesCategory.java index 2fe7bc3f..c12d0e0c 100644 --- a/app/src/main/java/org/solovyev/android/calculator/VarCategory.java +++ b/app/src/main/java/org/solovyev/android/calculator/variables/VariablesCategory.java @@ -20,39 +20,40 @@ * Site: http://se.solovyev.org */ -package org.solovyev.android.calculator; +package org.solovyev.android.calculator.variables; import android.support.annotation.StringRes; -import jscl.math.function.IConstant; + +import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.entities.Category; import javax.annotation.Nonnull; -public enum VarCategory implements Category { +import jscl.math.function.IConstant; + +public enum VariablesCategory implements Category { my(R.string.c_var_my) { @Override - public boolean isInCategory(@Nonnull IConstant var) { - return !var.isSystem(); + public boolean isInCategory(@Nonnull IConstant variable) { + return !variable.isSystem(); } }, system(R.string.c_var_system) { @Override - public boolean isInCategory(@Nonnull IConstant var) { - return var.isSystem(); + public boolean isInCategory(@Nonnull IConstant variable) { + return variable.isSystem(); } }; @StringRes private final int title; - VarCategory(@StringRes int title) { + VariablesCategory(@StringRes int title) { this.title = title; } - public abstract boolean isInCategory(@Nonnull IConstant var); - - @Override public int title() { return title; diff --git a/app/src/main/res/layout/vars_fragment.xml b/app/src/main/res/layout/vars_fragment.xml deleted file mode 100644 index efe7ad47..00000000 --- a/app/src/main/res/layout/vars_fragment.xml +++ /dev/null @@ -1,52 +0,0 @@ - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java b/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java index 80983e75..1f04c15a 100644 --- a/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java +++ b/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java @@ -31,7 +31,6 @@ import org.mockito.Mockito; import org.robolectric.fakes.RoboSharedPreferences; import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.language.Languages; -import org.solovyev.android.calculator.model.EntityDao; import org.solovyev.android.calculator.plot.CalculatorPlotter; import java.io.ByteArrayInputStream; @@ -99,8 +98,6 @@ public class CalculatorTestUtils { @Nonnull static Engine newCalculatorEngine() { - final EntityDao entityDao = Mockito.mock(EntityDao.class); - final JsclMathEngine jsclEngine = JsclMathEngine.getInstance(); final VariablesRegistry variablesRegistry = new VariablesRegistry(jsclEngine); diff --git a/app/src/test/java/org/solovyev/android/calculator/FromJsclSimplifyTextProcessorTest.java b/app/src/test/java/org/solovyev/android/calculator/FromJsclSimplifyTextProcessorTest.java index 3ac2de5e..89d6f39e 100644 --- a/app/src/test/java/org/solovyev/android/calculator/FromJsclSimplifyTextProcessorTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/FromJsclSimplifyTextProcessorTest.java @@ -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.OldVar; +import org.solovyev.android.calculator.variables.OldVar; import org.solovyev.android.calculator.text.FromJsclSimplifyTextProcessor; import java.text.DecimalFormatSymbols; diff --git a/app/src/test/java/org/solovyev/android/calculator/model/AndroidEngineTest.java b/app/src/test/java/org/solovyev/android/calculator/model/AndroidEngineTest.java index 2206882e..6fd474d6 100644 --- a/app/src/test/java/org/solovyev/android/calculator/model/AndroidEngineTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/model/AndroidEngineTest.java @@ -31,6 +31,7 @@ import org.solovyev.android.calculator.CalculatorEvalException; import org.solovyev.android.calculator.CalculatorTestUtils; import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.jscl.JsclOperation; +import org.solovyev.android.calculator.variables.OldVar; import java.text.DecimalFormatSymbols; import java.util.Locale; diff --git a/app/src/test/java/org/solovyev/android/calculator/model/EvaluateTest.java b/app/src/test/java/org/solovyev/android/calculator/model/EvaluateTest.java index 15ad5cab..cbb5d4d2 100644 --- a/app/src/test/java/org/solovyev/android/calculator/model/EvaluateTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/model/EvaluateTest.java @@ -28,6 +28,7 @@ import org.solovyev.android.calculator.AbstractCalculatorTest; import org.solovyev.android.calculator.CalculatorTestUtils; import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.jscl.JsclOperation; +import org.solovyev.android.calculator.variables.OldVar; import jscl.AngleUnit; import jscl.MathEngine; diff --git a/app/src/test/java/org/solovyev/android/calculator/model/OldFunctionsTest.java b/app/src/test/java/org/solovyev/android/calculator/model/OldFunctionsTest.java index 987360ed..435f374c 100644 --- a/app/src/test/java/org/solovyev/android/calculator/model/OldFunctionsTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/model/OldFunctionsTest.java @@ -30,6 +30,8 @@ import org.junit.Test; import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; import org.solovyev.android.calculator.CalculatorTestUtils; +import org.solovyev.android.calculator.function.OldFunction; +import org.solovyev.android.calculator.function.OldFunctions; import org.solovyev.common.Objects; import org.solovyev.common.equals.CollectionEqualizer; @@ -145,13 +147,13 @@ public class OldFunctionsTest { private void assertEquals(@Nonnull final OldFunction expected, @Nonnull OldFunction actual) { //Assert.assertEquals(expected.getId(), actual.getId()); - Assert.assertEquals(expected.getContent(), actual.getContent()); - Assert.assertEquals(expected.getDescription(), actual.getDescription()); - Assert.assertEquals(expected.getName(), actual.getName()); - Assert.assertThat(actual.getParameterNames(), new BaseMatcher>() { + Assert.assertEquals(expected.content, actual.content); + Assert.assertEquals(expected.description, actual.description); + Assert.assertEquals(expected.name, actual.name); + Assert.assertThat(actual.parameterNames, new BaseMatcher>() { @Override public boolean matches(Object item) { - return Objects.areEqual(expected.getParameterNames(), (List) item, new CollectionEqualizer(null)); + return Objects.areEqual(expected.parameterNames, (List) item, new CollectionEqualizer(null)); } @Override diff --git a/app/src/test/java/org/solovyev/android/calculator/model/OldVarTest.java b/app/src/test/java/org/solovyev/android/calculator/model/OldVarTest.java index 86ddc77d..dd5f0336 100644 --- a/app/src/test/java/org/solovyev/android/calculator/model/OldVarTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/model/OldVarTest.java @@ -25,6 +25,8 @@ package org.solovyev.android.calculator.model; import org.junit.Test; import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; +import org.solovyev.android.calculator.variables.OldVar; +import org.solovyev.android.calculator.variables.OldVars; import java.io.StringWriter; @@ -59,9 +61,9 @@ public class OldVarTest { public void testXml() throws Exception { final OldVars vars = new OldVars(); OldVar first = new OldVar.Builder("e", Math.E).setDescription("description").setSystem(true).create(); - vars.getEntities().add(first); + vars.list.add(first); OldVar second = new OldVar.Builder(";", 3d).setSystem(true).create(); - vars.getEntities().add(second); + vars.list.add(second); final StringWriter sw = new StringWriter(); final Serializer serializer = new Persister(); @@ -70,8 +72,8 @@ public class OldVarTest { assertEquals(xml, sw.toString()); final OldVars result = serializer.read(OldVars.class, xml); - final IConstant actualFirst = result.getEntities().get(0); - final IConstant actualSecond = result.getEntities().get(1); + final IConstant actualFirst = result.list.get(0); + final IConstant actualSecond = result.list.get(1); areEqual(first, actualFirst); areEqual(second, actualSecond);