This commit is contained in:
serso 2016-01-20 17:04:17 +01:00
parent 7faef42dea
commit 591cfa4648
15 changed files with 95 additions and 150 deletions

View File

@ -22,17 +22,19 @@
package org.solovyev.android.calculator;
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;
public abstract class BaseEntitiesRegistry<T extends MathEntity, P extends MathPersistenceEntity> implements EntitiesRegistry<T> {
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public abstract class BaseEntitiesRegistry<T extends MathEntity, P extends PersistedEntity> implements EntitiesRegistry<T> {
@Nonnull
private final MathRegistry<T> mathRegistry;
@ -41,14 +43,14 @@ public abstract class BaseEntitiesRegistry<T extends MathEntity, P extends MathP
private final String prefix;
@Nonnull
private final MathEntityDao<P> mathEntityDao;
private final EntityDao<P> entityDao;
protected BaseEntitiesRegistry(@Nonnull MathRegistry<T> mathRegistry,
@Nonnull String prefix,
@Nonnull MathEntityDao<P> mathEntityDao) {
@Nonnull EntityDao<P> entityDao) {
this.mathRegistry = mathRegistry;
this.prefix = prefix;
this.mathEntityDao = mathEntityDao;
this.entityDao = entityDao;
}
@ -68,11 +70,11 @@ public abstract class BaseEntitiesRegistry<T extends MathEntity, P extends MathP
stringName = prefix + substitute;
}
return mathEntityDao.getDescription(stringName);
return entityDao.getDescription(stringName);
}
public synchronized void load() {
final MathEntityPersistenceContainer<P> persistenceContainer = mathEntityDao.load();
final PersistedEntitiesContainer<P> persistenceContainer = entityDao.load();
final List<P> notCreatedEntities = new ArrayList<P>();
@ -110,7 +112,7 @@ public abstract class BaseEntitiesRegistry<T extends MathEntity, P extends MathP
@Override
public synchronized void save() {
final MathEntityPersistenceContainer<P> container = createPersistenceContainer();
final PersistedEntitiesContainer<P> container = createPersistenceContainer();
for (T entity : this.getEntities()) {
if (!entity.isSystem()) {
@ -121,14 +123,14 @@ public abstract class BaseEntitiesRegistry<T extends MathEntity, P extends MathP
}
}
this.mathEntityDao.save(container);
this.entityDao.save(container);
}
@Nullable
protected abstract P transform(@Nonnull T entity);
@Nonnull
protected abstract MathEntityPersistenceContainer<P> createPersistenceContainer();
protected abstract PersistedEntitiesContainer<P> createPersistenceContainer();
@Nonnull
@Override

View File

@ -27,7 +27,7 @@ import android.content.SharedPreferences;
import com.squareup.otto.Bus;
import org.solovyev.android.calculator.model.AndroidMathEntityDao;
import org.solovyev.android.calculator.model.EntityDao;
import org.solovyev.android.calculator.model.Functions;
import org.solovyev.android.calculator.model.Vars;
import org.solovyev.android.prefs.BooleanPreference;
@ -92,10 +92,10 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
this.mathEngine.setRoundResult(true);
this.mathEngine.setUseGroupingSeparator(true);
this.varsRegistry = new VarsRegistry(mathEngine.getConstantsRegistry(), new AndroidMathEntityDao<>("org.solovyev.android.calculator.CalculatorModel_vars", application, Vars.class));
this.functionsRegistry = new FunctionsRegistry(mathEngine.getFunctionsRegistry(), new AndroidMathEntityDao<>("org.solovyev.android.calculator.CalculatorModel_functions", application, Functions.class));
this.operatorsRegistry = new OperatorsRegistry(mathEngine.getOperatorsRegistry(), new AndroidMathEntityDao<>(null, application, null));
this.postfixFunctionsRegistry = new PostfixFunctionsRegistry(mathEngine.getPostfixFunctionsRegistry(), new AndroidMathEntityDao<>(null, application, null));
this.varsRegistry = new VarsRegistry(mathEngine.getConstantsRegistry(), new EntityDao<>("org.solovyev.android.calculator.CalculatorModel_vars", application, Vars.class));
this.functionsRegistry = new FunctionsRegistry(mathEngine.getFunctionsRegistry(), new EntityDao<>("org.solovyev.android.calculator.CalculatorModel_functions", application, Functions.class));
this.operatorsRegistry = new OperatorsRegistry(mathEngine.getOperatorsRegistry(), new EntityDao<>(null, application, null));
this.postfixFunctionsRegistry = new PostfixFunctionsRegistry(mathEngine.getPostfixFunctionsRegistry(), new EntityDao<>(null, application, null));
}
private static void migratePreference(@Nonnull SharedPreferences preferences, @Nonnull BooleanPreference preference, @Nonnull String oldKey, @Nonnull SharedPreferences.Editor editor) {

View File

@ -22,25 +22,28 @@
package org.solovyev.android.calculator;
import jscl.CustomFunctionCalculationException;
import jscl.math.function.CustomFunction;
import jscl.math.function.Function;
import jscl.math.function.IFunction;
import org.solovyev.android.calculator.function.FunctionBuilderAdapter;
import org.solovyev.android.calculator.model.AFunction;
import org.solovyev.android.calculator.model.Functions;
import org.solovyev.android.calculator.model.MathEntityBuilder;
import org.solovyev.android.calculator.model.EntityDao;
import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathRegistry;
import org.solovyev.common.text.Strings;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import jscl.CustomFunctionCalculationException;
import jscl.math.function.CustomFunction;
import jscl.math.function.Function;
import jscl.math.function.IFunction;
public class FunctionsRegistry extends BaseEntitiesRegistry<Function, AFunction> {
@Nonnull
@ -53,8 +56,8 @@ public class FunctionsRegistry extends BaseEntitiesRegistry<Function, AFunction>
}
public FunctionsRegistry(@Nonnull MathRegistry<Function> functionsRegistry,
@Nonnull MathEntityDao<AFunction> mathEntityDao) {
super(functionsRegistry, FUNCTION_DESCRIPTION_PREFIX, mathEntityDao);
@Nonnull EntityDao<AFunction> entityDao) {
super(functionsRegistry, FUNCTION_DESCRIPTION_PREFIX, entityDao);
}
public static void saveFunction(@Nonnull EntitiesRegistry<Function> registry,
@ -138,7 +141,7 @@ public class FunctionsRegistry extends BaseEntitiesRegistry<Function, AFunction>
@Nonnull
@Override
protected MathEntityPersistenceContainer<AFunction> createPersistenceContainer() {
protected PersistedEntitiesContainer<AFunction> createPersistenceContainer() {
return new Functions();
}
}

View File

@ -1,42 +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 javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* User: serso
* Date: 10/7/12
* Time: 6:43 PM
*/
public interface MathEntityDao<T extends MathPersistenceEntity> {
void save(@Nonnull MathEntityPersistenceContainer<T> container);
@Nullable
MathEntityPersistenceContainer<T> load();
@Nullable
String getDescription(@Nonnull String descriptionId);
}

View File

@ -22,15 +22,18 @@
package org.solovyev.android.calculator;
import jscl.math.operator.Operator;
import org.solovyev.android.calculator.model.EntityDao;
import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathRegistry;
import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Map;
public class OperatorsRegistry extends BaseEntitiesRegistry<Operator, MathPersistenceEntity> {
import javax.annotation.Nonnull;
import jscl.math.operator.Operator;
public class OperatorsRegistry extends BaseEntitiesRegistry<Operator, PersistedEntity> {
@Nonnull
private static final Map<String, String> substitutes = new HashMap<String, String>();
@ -47,8 +50,8 @@ public class OperatorsRegistry extends BaseEntitiesRegistry<Operator, MathPersis
}
public OperatorsRegistry(@Nonnull MathRegistry<Operator> functionsRegistry,
@Nonnull MathEntityDao<MathPersistenceEntity> mathEntityDao) {
super(functionsRegistry, OPERATOR_DESCRIPTION_PREFIX, mathEntityDao);
@Nonnull EntityDao<PersistedEntity> entityDao) {
super(functionsRegistry, OPERATOR_DESCRIPTION_PREFIX, entityDao);
}
@Nonnull
@ -74,7 +77,7 @@ public class OperatorsRegistry extends BaseEntitiesRegistry<Operator, MathPersis
@Nonnull
@Override
protected JBuilder<? extends Operator> createBuilder(@Nonnull MathPersistenceEntity entity) {
protected JBuilder<? extends Operator> createBuilder(@Nonnull PersistedEntity entity) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@ -84,22 +87,13 @@ public class OperatorsRegistry extends BaseEntitiesRegistry<Operator, MathPersis
}
@Override
protected MathPersistenceEntity transform(@Nonnull Operator entity) {
protected PersistedEntity transform(@Nonnull Operator entity) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Nonnull
@Override
protected MathEntityPersistenceContainer<MathPersistenceEntity> createPersistenceContainer() {
protected PersistedEntitiesContainer<PersistedEntity> createPersistenceContainer() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
/*
**********************************************************************
*
* STATIC
*
**********************************************************************
*/
}

View File

@ -24,13 +24,6 @@ package org.solovyev.android.calculator;
import java.util.List;
/**
* User: serso
* Date: 12/22/11
* Time: 5:03 PM
*/
public interface MathEntityPersistenceContainer<T extends MathPersistenceEntity> {
public List<T> getEntities();
public interface PersistedEntitiesContainer<T extends PersistedEntity> {
List<T> getEntities();
}

View File

@ -24,12 +24,7 @@ package org.solovyev.android.calculator;
import javax.annotation.Nonnull;
/**
* User: serso
* Date: 12/22/11
* Time: 5:27 PM
*/
public interface MathPersistenceEntity {
public interface PersistedEntity {
@Nonnull
String getName();

View File

@ -22,15 +22,18 @@
package org.solovyev.android.calculator;
import jscl.math.operator.Operator;
import org.solovyev.android.calculator.model.EntityDao;
import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathRegistry;
import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Map;
public class PostfixFunctionsRegistry extends BaseEntitiesRegistry<Operator, MathPersistenceEntity> {
import javax.annotation.Nonnull;
import jscl.math.operator.Operator;
public class PostfixFunctionsRegistry extends BaseEntitiesRegistry<Operator, PersistedEntity> {
@Nonnull
private static final Map<String, String> substitutes = new HashMap<String, String>();
@ -45,8 +48,8 @@ public class PostfixFunctionsRegistry extends BaseEntitiesRegistry<Operator, Mat
}
public PostfixFunctionsRegistry(@Nonnull MathRegistry<Operator> functionsRegistry,
@Nonnull MathEntityDao<MathPersistenceEntity> mathEntityDao) {
super(functionsRegistry, POSTFIX_FUNCTION_DESCRIPTION_PREFIX, mathEntityDao);
@Nonnull EntityDao<PersistedEntity> entityDao) {
super(functionsRegistry, POSTFIX_FUNCTION_DESCRIPTION_PREFIX, entityDao);
}
@ -73,7 +76,7 @@ public class PostfixFunctionsRegistry extends BaseEntitiesRegistry<Operator, Mat
@Nonnull
@Override
protected JBuilder<? extends Operator> createBuilder(@Nonnull MathPersistenceEntity entity) {
protected JBuilder<? extends Operator> createBuilder(@Nonnull PersistedEntity entity) {
throw new UnsupportedOperationException();
}
@ -83,13 +86,13 @@ public class PostfixFunctionsRegistry extends BaseEntitiesRegistry<Operator, Mat
}
@Override
protected MathPersistenceEntity transform(@Nonnull Operator entity) {
protected PersistedEntity transform(@Nonnull Operator entity) {
return null;
}
@Nonnull
@Override
protected MathEntityPersistenceContainer<MathPersistenceEntity> createPersistenceContainer() {
protected PersistedEntitiesContainer<PersistedEntity> createPersistenceContainer() {
throw new UnsupportedOperationException();
}
}

View File

@ -22,19 +22,22 @@
package org.solovyev.android.calculator;
import jscl.math.function.IConstant;
import org.solovyev.android.calculator.model.MathEntityBuilder;
import org.solovyev.android.calculator.model.EntityDao;
import org.solovyev.android.calculator.model.Var;
import org.solovyev.android.calculator.model.Vars;
import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathEntity;
import org.solovyev.common.math.MathRegistry;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import jscl.math.function.IConstant;
public class VarsRegistry extends BaseEntitiesRegistry<IConstant, Var> {
@Nonnull
@ -52,8 +55,8 @@ public class VarsRegistry extends BaseEntitiesRegistry<IConstant, Var> {
}
public VarsRegistry(@Nonnull MathRegistry<IConstant> mathRegistry,
@Nonnull MathEntityDao<Var> mathEntityDao) {
super(mathRegistry, "c_var_description_", mathEntityDao);
@Nonnull EntityDao<Var> entityDao) {
super(mathRegistry, "c_var_description_", entityDao);
}
public static <T extends MathEntity> void saveVariable(@Nonnull EntitiesRegistry<T> registry,
@ -97,7 +100,7 @@ public class VarsRegistry extends BaseEntitiesRegistry<IConstant, Var> {
@Nonnull
@Override
protected MathEntityPersistenceContainer<Var> createPersistenceContainer() {
protected PersistedEntitiesContainer<Var> createPersistenceContainer() {
return new Vars();
}

View File

@ -22,29 +22,31 @@
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.CalculatorParseException;
import org.solovyev.android.calculator.Locator;
import org.solovyev.android.calculator.MathPersistenceEntity;
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 javax.annotation.Nullable;
import jscl.math.function.IFunction;
@Root(name = "function")
public class AFunction implements IFunction, MathPersistenceEntity, Serializable {
public class AFunction implements IFunction, PersistedEntity, Serializable {
@Transient
private Integer id;

View File

@ -27,21 +27,19 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import org.solovyev.android.calculator.CalculatorApplication;
import org.solovyev.android.calculator.MathEntityDao;
import org.solovyev.android.calculator.MathEntityPersistenceContainer;
import org.solovyev.android.calculator.MathPersistenceEntity;
import org.solovyev.android.calculator.PersistedEntitiesContainer;
import org.solovyev.android.calculator.PersistedEntity;
import java.io.StringWriter;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.StringWriter;
public class AndroidMathEntityDao<T extends MathPersistenceEntity> implements MathEntityDao<T> {
@Nonnull
private static final String TAG = AndroidMathEntityDao.class.getSimpleName();
public class EntityDao<T extends PersistedEntity> {
@Nullable
private final String preferenceString;
@ -50,18 +48,17 @@ public class AndroidMathEntityDao<T extends MathPersistenceEntity> implements Ma
private final Context context;
@Nullable
private final Class<? extends MathEntityPersistenceContainer<T>> persistenceContainerClass;
private final Class<? extends PersistedEntitiesContainer<T>> persistenceContainerClass;
public AndroidMathEntityDao(@Nullable String preferenceString,
public EntityDao(@Nullable String preferenceString,
@Nonnull Application application,
@Nullable Class<? extends MathEntityPersistenceContainer<T>> persistenceContainerClass) {
@Nullable Class<? extends PersistedEntitiesContainer<T>> persistenceContainerClass) {
this.preferenceString = preferenceString;
this.context = application;
this.persistenceContainerClass = persistenceContainerClass;
}
@Override
public void save(@Nonnull MathEntityPersistenceContainer<T> container) {
public void save(@Nonnull PersistedEntitiesContainer<T> container) {
if (preferenceString != null) {
final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
final SharedPreferences.Editor editor = settings.edit();
@ -81,8 +78,7 @@ public class AndroidMathEntityDao<T extends MathPersistenceEntity> implements Ma
}
@Nullable
@Override
public MathEntityPersistenceContainer<T> load() {
public PersistedEntitiesContainer<T> load() {
if (persistenceContainerClass != null && preferenceString != null) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

View File

@ -24,18 +24,13 @@ package org.solovyev.android.calculator.model;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.solovyev.android.calculator.MathEntityPersistenceContainer;
import org.solovyev.android.calculator.PersistedEntitiesContainer;
import java.util.ArrayList;
import java.util.List;
/**
* User: serso
* Date: 12/22/11
* Time: 5:15 PM
*/
@Root
public class Functions implements MathEntityPersistenceContainer<AFunction> {
public class Functions implements PersistedEntitiesContainer<AFunction> {
@ElementList(type = AFunction.class)
private List<AFunction> functions = new ArrayList<AFunction>();

View File

@ -25,7 +25,7 @@ package org.solovyev.android.calculator.model;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Transient;
import org.solovyev.android.calculator.MathPersistenceEntity;
import org.solovyev.android.calculator.PersistedEntity;
import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathEntity;
import org.solovyev.common.text.Strings;
@ -44,7 +44,7 @@ import jscl.math.function.IConstant;
*/
@Root
public class Var implements IConstant, MathPersistenceEntity {
public class Var implements IConstant, PersistedEntity {
@Transient
private Integer id;

View File

@ -24,7 +24,7 @@ package org.solovyev.android.calculator.model;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.solovyev.android.calculator.MathEntityPersistenceContainer;
import org.solovyev.android.calculator.PersistedEntitiesContainer;
import java.util.ArrayList;
import java.util.List;
@ -36,7 +36,7 @@ import java.util.List;
*/
@Root
public class Vars implements MathEntityPersistenceContainer<Var> {
public class Vars implements PersistedEntitiesContainer<Var> {
@ElementList(type = Var.class)
private List<Var> vars = new ArrayList<Var>();

View File

@ -31,6 +31,7 @@ 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;
@ -98,14 +99,14 @@ public class CalculatorTestUtils {
@Nonnull
static Engine newCalculatorEngine() {
final MathEntityDao mathEntityDao = Mockito.mock(MathEntityDao.class);
final EntityDao entityDao = Mockito.mock(EntityDao.class);
final JsclMathEngine jsclEngine = JsclMathEngine.getInstance();
final VarsRegistry varsRegistry = new VarsRegistry(jsclEngine.getConstantsRegistry(), mathEntityDao);
final FunctionsRegistry functionsRegistry = new FunctionsRegistry(jsclEngine.getFunctionsRegistry(), mathEntityDao);
final OperatorsRegistry operatorsRegistry = new OperatorsRegistry(jsclEngine.getOperatorsRegistry(), mathEntityDao);
final PostfixFunctionsRegistry postfixFunctionsRegistry = new PostfixFunctionsRegistry(jsclEngine.getPostfixFunctionsRegistry(), mathEntityDao);
final VarsRegistry varsRegistry = new VarsRegistry(jsclEngine.getConstantsRegistry(), entityDao);
final FunctionsRegistry functionsRegistry = new FunctionsRegistry(jsclEngine.getFunctionsRegistry(), entityDao);
final OperatorsRegistry operatorsRegistry = new OperatorsRegistry(jsclEngine.getOperatorsRegistry(), entityDao);
final PostfixFunctionsRegistry postfixFunctionsRegistry = new PostfixFunctionsRegistry(jsclEngine.getPostfixFunctionsRegistry(), entityDao);
return new Engine(jsclEngine, varsRegistry, functionsRegistry, operatorsRegistry, postfixFunctionsRegistry);
}