registries

This commit is contained in:
Sergey Solovyev 2012-10-07 19:29:37 +04:00
parent d4cff84040
commit 78a1cc4942
32 changed files with 1441 additions and 1290 deletions

View File

@ -19,12 +19,24 @@
<dependencies> <dependencies>
<dependency>
<groupId>org.solovyev</groupId>
<artifactId>common-text</artifactId>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.0</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>com.intellij</groupId> <groupId>com.intellij</groupId>
<artifactId>annotations</artifactId> <artifactId>annotations</artifactId>

View File

@ -0,0 +1,148 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathEntity;
import org.solovyev.common.math.MathRegistry;
import java.util.List;
import java.util.Map;
/**
* User: serso
* Date: 10/30/11
* Time: 1:03 AM
*/
public abstract class AbstractCalculatorMathRegistry<T extends MathEntity, P extends MathPersistenceEntity> implements CalculatorMathRegistry<T> {
@NotNull
private final MathRegistry<T> mathRegistry;
@NotNull
private final String prefix;
@NotNull
private final MathEntityDao<P> mathEntityDao;
protected AbstractCalculatorMathRegistry(@NotNull MathRegistry<T> mathRegistry,
@NotNull String prefix,
@NotNull MathEntityDao<P> mathEntityDao) {
this.mathRegistry = mathRegistry;
this.prefix = prefix;
this.mathEntityDao = mathEntityDao;
}
@NotNull
protected abstract Map<String, String> getSubstitutes();
@Nullable
@Override
public String getDescription(@NotNull String mathEntityName) {
final String stringName;
final Map<String, String> substitutes = getSubstitutes();
final String substitute = substitutes.get(mathEntityName);
if (substitute == null) {
stringName = prefix + mathEntityName;
} else {
stringName = prefix + substitute;
}
return mathEntityDao.getDescription(stringName);
}
public synchronized void load() {
final MathEntityPersistenceContainer<P> persistenceContainer = mathEntityDao.load();
if (persistenceContainer != null) {
for (P entity : persistenceContainer.getEntities()) {
if (!contains(entity.getName())) {
add(createBuilder(entity));
}
}
}
/*Log.d(AndroidVarsRegistry.class.getName(), vars.size() + " variables registered!");
for (Var var : vars) {
Log.d(AndroidVarsRegistry.class.getName(), var.toString());
}*/
}
@NotNull
protected abstract JBuilder<? extends T> createBuilder(@NotNull P entity);
@Override
public synchronized void save() {
final MathEntityPersistenceContainer<P> container = createPersistenceContainer();
for (T entity : this.getEntities()) {
if (!entity.isSystem()) {
final P persistenceEntity = transform(entity);
if (persistenceEntity != null) {
container.getEntities().add(persistenceEntity);
}
}
}
this.mathEntityDao.save(container);
}
@Nullable
protected abstract P transform(@NotNull T entity);
@NotNull
protected abstract MathEntityPersistenceContainer<P> createPersistenceContainer();
@NotNull
@Override
public List<T> getEntities() {
return mathRegistry.getEntities();
}
@NotNull
@Override
public List<T> getSystemEntities() {
return mathRegistry.getSystemEntities();
}
@Override
public T add(@NotNull JBuilder<? extends T> JBuilder) {
return mathRegistry.add(JBuilder);
}
@Override
public void remove(@NotNull T var) {
mathRegistry.remove(var);
}
@NotNull
@Override
public List<String> getNames() {
return mathRegistry.getNames();
}
@Override
public boolean contains(@NotNull String name) {
return mathRegistry.contains(name);
}
@Override
public T get(@NotNull String name) {
return mathRegistry.get(name);
}
@Override
public T getById(@NotNull Integer id) {
return mathRegistry.getById(id);
}
}

View File

@ -0,0 +1,20 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* User: serso
* Date: 10/7/12
* Time: 6:43 PM
*/
public interface MathEntityDao<T extends MathPersistenceEntity> {
void save(@NotNull MathEntityPersistenceContainer<T> container);
@Nullable
MathEntityPersistenceContainer<T> load();
@Nullable
String getDescription(@NotNull String descriptionId);
}

View File

@ -1,4 +1,4 @@
package org.solovyev.android.calculator.model; package org.solovyev.android.calculator;
import java.util.List; import java.util.List;
@ -8,5 +8,7 @@ import java.util.List;
* Time: 5:03 PM * Time: 5:03 PM
*/ */
public interface MathEntityPersistenceContainer<T extends MathPersistenceEntity> { public interface MathEntityPersistenceContainer<T extends MathPersistenceEntity> {
public List<T> getEntities(); public List<T> getEntities();
} }

View File

@ -4,7 +4,7 @@
* or visit http://se.solovyev.org * or visit http://se.solovyev.org
*/ */
package org.solovyev.android.calculator.model; package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;

View File

@ -354,7 +354,7 @@ public enum MathType {
final String s = CollectionsUtils.find(mathType.getTokens(), startsWithFinder); final String s = CollectionsUtils.find(mathType.getTokens(), startsWithFinder);
if (s != null) { if (s != null) {
if ( s.length() == 1 ) { if ( s.length() == 1 ) {
if (hexMode || JsclMathEngine.instance.getNumeralBase() == NumeralBase.hex) { if (hexMode || JsclMathEngine.getInstance().getNumeralBase() == NumeralBase.hex) {
final Character ch = s.charAt(0); final Character ch = s.charAt(0);
if ( NumeralBase.hex.getAcceptableCharacters().contains(ch) ) { if ( NumeralBase.hex.getAcceptableCharacters().contains(ch) ) {
return new Result(MathType.digit, s); return new Result(MathType.digit, s);

View File

@ -10,6 +10,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.simpleframework.xml.Element; import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root; import org.simpleframework.xml.Root;
import org.solovyev.android.calculator.MathPersistenceEntity;
import org.solovyev.common.collections.CollectionsUtils; import org.solovyev.common.collections.CollectionsUtils;
import org.solovyev.common.text.CollectionTransformations; import org.solovyev.common.text.CollectionTransformations;
import org.solovyev.common.text.StringMapper; import org.solovyev.common.text.StringMapper;

View File

@ -1,10 +1,9 @@
package org.solovyev.android.calculator.model; package org.solovyev.android.calculator.model;
import jscl.math.function.CustomFunction; import jscl.math.function.CustomFunction;
import jscl.math.function.Function;
import jscl.math.function.IConstant;
import org.simpleframework.xml.ElementList; import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root; import org.simpleframework.xml.Root;
import org.solovyev.android.calculator.MathEntityPersistenceContainer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;

View File

@ -0,0 +1,32 @@
package org.solovyev.android.calculator;
import jscl.JsclMathEngine;
import org.jetbrains.annotations.NotNull;
import org.mockito.Mockito;
import org.solovyev.android.calculator.history.CalculatorHistory;
/**
* User: serso
* Date: 10/7/12
* Time: 6:30 PM
*/
public class AbstractCalculatorTest {
protected static void staticSetUp() throws Exception {
CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class));
}
protected void setUp() throws Exception {
CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class));
}
@NotNull
private static CalculatorEngineImpl newCalculatorEngine() {
final CalculatorMathRegistry varsRegistry = Mockito.mock(CalculatorMathRegistry.class);
//Mockito.when(varsRegistry.get())
final CalculatorEngineImpl result = new CalculatorEngineImpl(JsclMathEngine.getInstance(), varsRegistry, Mockito.mock(CalculatorMathRegistry.class), Mockito.mock(CalculatorMathRegistry.class), Mockito.mock(CalculatorMathRegistry.class), null);
result.init();
return result;
}
}

View File

@ -10,14 +10,15 @@ import org.junit.Test;
* Date: 21.09.12 * Date: 21.09.12
* Time: 12:44 * Time: 12:44
*/ */
public class CalculatorEditorImplTest { public class CalculatorEditorImplTest extends AbstractCalculatorTest {
@NotNull @NotNull
private CalculatorEditor calculatorEditor; private CalculatorEditor calculatorEditor;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
this.calculatorEditor = new CalculatorEditorImpl(new CalculatorImpl()); super.setUp();
this.calculatorEditor = new CalculatorEditorImpl(CalculatorLocatorImpl.getInstance().getCalculator());
} }
@Test @Test

View File

@ -9,18 +9,18 @@ package org.solovyev.android.calculator.math;
import junit.framework.Assert; import junit.framework.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.solovyev.android.calculator.CalculatorLocatorImpl; import org.solovyev.android.calculator.AbstractCalculatorTest;
/** /**
* User: serso * User: serso
* Date: 10/5/11 * Date: 10/5/11
* Time: 1:25 AM * Time: 1:25 AM
*/ */
public class MathTypeTest { public class MathTypeTest extends AbstractCalculatorTest {
@BeforeClass @BeforeClass
public static void setUp() throws Exception { public static void staticSetUp() throws Exception {
CalculatorLocatorImpl.getInstance().getEngine().init(); AbstractCalculatorTest.staticSetUp();
} }
@Test @Test

View File

@ -156,6 +156,13 @@
<artifactId>annotations</artifactId> <artifactId>annotations</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.pivotallabs</groupId>
<artifactId>robolectric</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -0,0 +1,39 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* User: serso
* Date: 10/7/12
* Time: 7:17 PM
*/
public enum AndroidFunctionCategory {
trigonometric(R.string.c_fun_category_trig),
hyperbolic_trigonometric(R.string.c_fun_category_hyper_trig),
comparison(R.string.c_fun_category_comparison),
my(R.string.c_fun_category_my),
common(R.string.c_fun_category_common);
private final int captionId;
AndroidFunctionCategory(int captionId) {
this.captionId = captionId;
}
public int getCaptionId() {
return captionId;
}
@Nullable
public AndroidFunctionCategory valueOf( @NotNull FunctionCategory functionCategory ) {
for (AndroidFunctionCategory androidFunctionCategory : values()) {
if ( androidFunctionCategory.name().equals(functionCategory.name()) ) {
return androidFunctionCategory;
}
}
return null;
}
}

View File

@ -0,0 +1,89 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator;
import jscl.math.function.CustomFunction;
import jscl.math.function.Function;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.model.AFunction;
import org.solovyev.android.calculator.model.Functions;
import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathRegistry;
import java.util.HashMap;
import java.util.Map;
/**
* User: serso
* Date: 11/17/11
* Time: 11:28 PM
*/
public class AndroidFunctionsMathRegistry extends AbstractCalculatorMathRegistry<Function, AFunction> {
@NotNull
private static final Map<String, String> substitutes = new HashMap<String, String>();
static {
substitutes.put("", "sqrt");
}
@NotNull
private static final String FUNCTION_DESCRIPTION_PREFIX = "c_fun_description_";
public AndroidFunctionsMathRegistry(@NotNull MathRegistry<Function> functionsRegistry,
@NotNull MathEntityDao<AFunction> mathEntityDao) {
super(functionsRegistry, FUNCTION_DESCRIPTION_PREFIX, mathEntityDao);
}
@Override
public void load() {
super.load();
add(new CustomFunction.Builder(true, "log", new String[]{"base", "x"}, "ln(x)/ln(base)"));
}
@NotNull
@Override
protected Map<String, String> getSubstitutes() {
return substitutes;
}
@Override
public String getCategory(@NotNull Function function) {
for (FunctionCategory category : FunctionCategory.values()) {
if ( category.isInCategory(function) ) {
return category.name();
}
}
return null;
}
@NotNull
@Override
protected JBuilder<? extends Function> createBuilder(@NotNull AFunction entity) {
return new CustomFunction.Builder(entity.getName(), entity.getParameterNamesAsArray(), entity.getContent());
}
@Override
protected AFunction transform(@NotNull Function entity) {
if (entity instanceof CustomFunction) {
final AFunction result = new AFunction();
result.setName(entity.getName());
result.setContent(((CustomFunction) entity).getContent());
result.setParameterNames(((CustomFunction) entity).getParameterNames());
return result;
} else {
return null;
}
}
@NotNull
@Override
protected MathEntityPersistenceContainer<AFunction> createPersistenceContainer() {
return new Functions();
}
}

View File

@ -0,0 +1,92 @@
package org.solovyev.android.calculator;
import jscl.math.function.ArcTrigonometric;
import jscl.math.function.Comparison;
import jscl.math.function.Function;
import jscl.math.function.Trigonometric;
import org.jetbrains.annotations.NotNull;
import org.solovyev.common.collections.CollectionsUtils;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* User: serso
* Date: 10/7/12
* Time: 7:15 PM
*/
public enum FunctionCategory {
trigonometric(100){
@Override
public boolean isInCategory(@NotNull Function function) {
return (function instanceof Trigonometric || function instanceof ArcTrigonometric) && !hyperbolic_trigonometric.isInCategory(function);
}
},
hyperbolic_trigonometric(300) {
private final List<String> names = Arrays.asList("sinh", "cosh", "tanh", "coth", "asinh", "acosh", "atanh", "acoth");
@Override
public boolean isInCategory(@NotNull Function function) {
return names.contains(function.getName());
}
},
comparison(200) {
@Override
public boolean isInCategory(@NotNull Function function) {
return function instanceof Comparison;
}
},
my(0) {
@Override
public boolean isInCategory(@NotNull Function function) {
return !function.isSystem();
}
},
common(50) {
@Override
public boolean isInCategory(@NotNull Function function) {
for (FunctionCategory category : values()) {
if ( category != this ) {
if ( category.isInCategory(function) ) {
return false;
}
}
}
return true;
}
};
private final int tabOrder;
FunctionCategory(int tabOrder) {
this.tabOrder = tabOrder;
}
public abstract boolean isInCategory(@NotNull Function function);
@NotNull
public static List<FunctionCategory> getCategoriesByTabOrder() {
final List<FunctionCategory> result = CollectionsUtils.asList(FunctionCategory.values());
Collections.sort(result, new Comparator<FunctionCategory>() {
@Override
public int compare(FunctionCategory category, FunctionCategory category1) {
return category.tabOrder - category1.tabOrder;
}
});
// todo serso: current solution (as creating functions is not implemented yet)
result.remove(my);
return result;
}
}

View File

@ -13,7 +13,6 @@ import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.about.CalculatorFragmentType; import org.solovyev.android.calculator.about.CalculatorFragmentType;
import org.solovyev.android.calculator.history.CalculatorHistoryActivity; import org.solovyev.android.calculator.history.CalculatorHistoryActivity;
import org.solovyev.android.calculator.model.AndroidFunctionsMathRegistry;
/** /**
* User: serso * User: serso
@ -33,7 +32,7 @@ public class CalculatorFunctionsActivity extends SherlockFragmentActivity implem
final CalculatorFragmentType fragmentType = CalculatorFragmentType.functions; final CalculatorFragmentType fragmentType = CalculatorFragmentType.functions;
for (AndroidFunctionsMathRegistry.Category category : AndroidFunctionsMathRegistry.Category.getCategoriesByTabOrder()) { for (FunctionCategory category : FunctionCategory.getCategoriesByTabOrder()) {
activityHelper.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), AbstractMathEntityListFragment.createBundleFor(category.name()), category.getCaptionId(), R.id.main_layout); activityHelper.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), AbstractMathEntityListFragment.createBundleFor(category.name()), category.getCaptionId(), R.id.main_layout);
} }

View File

@ -1,198 +0,0 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator.model;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import org.solovyev.android.calculator.CalculatorMathRegistry;
import org.solovyev.android.calculator.R;
import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathEntity;
import org.solovyev.common.math.MathRegistry;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;
/**
* User: serso
* Date: 10/30/11
* Time: 1:03 AM
*/
public abstract class AbstractAndroidMathRegistry<T extends MathEntity, P extends MathPersistenceEntity> implements CalculatorMathRegistry<T> {
@NotNull
private final MathRegistry<T> mathRegistry;
@NotNull
private final String prefix;
@NotNull
private final Context context;
protected AbstractAndroidMathRegistry(@NotNull MathRegistry<T> mathRegistry,
@NotNull String prefix,
@NotNull Application application) {
this.mathRegistry = mathRegistry;
this.prefix = prefix;
this.context = application;
}
@NotNull
protected abstract Map<String, String> getSubstitutes();
@Nullable
@Override
public String getDescription(@NotNull String mathEntityName) {
final String stringName;
final Map<String, String> substitutes = getSubstitutes();
final String substitute = substitutes.get(mathEntityName);
if (substitute == null) {
stringName = prefix + mathEntityName;
} else {
stringName = prefix + substitute;
}
final Resources resources = context.getResources();
final int stringId = resources.getIdentifier(stringName, "string", R.class.getPackage().getName());
try {
return resources.getString(stringId);
} catch (Resources.NotFoundException e) {
return null;
}
}
public synchronized void load() {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences != null) {
final Integer preferenceStringId = getPreferenceStringId();
if (preferenceStringId != null) {
final String value = preferences.getString(context.getString(preferenceStringId), null);
if (value != null) {
final Serializer serializer = new Persister();
try {
final MathEntityPersistenceContainer<P> persistenceContainer = serializer.read(getPersistenceContainerClass(), value);
for (P entity : persistenceContainer.getEntities()) {
if (!contains(entity.getName())) {
add(createBuilder(entity));
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
/*Log.d(AndroidVarsRegistry.class.getName(), vars.size() + " variables registered!");
for (Var var : vars) {
Log.d(AndroidVarsRegistry.class.getName(), var.toString());
}*/
}
@NotNull
protected abstract JBuilder<? extends T> createBuilder(@NotNull P entity);
@NotNull
protected abstract Class<? extends MathEntityPersistenceContainer<P>> getPersistenceContainerClass();
@Nullable
protected abstract Integer getPreferenceStringId();
@Override
public synchronized void save() {
final Integer preferenceStringId = getPreferenceStringId();
if (preferenceStringId != null) {
final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
final SharedPreferences.Editor editor = settings.edit();
final MathEntityPersistenceContainer<P> container = createPersistenceContainer();
for (T entity : this.getEntities()) {
if (!entity.isSystem()) {
final P persistenceEntity = transform(entity);
if (persistenceEntity != null) {
container.getEntities().add(persistenceEntity);
}
}
}
final StringWriter sw = new StringWriter();
final Serializer serializer = new Persister();
try {
serializer.write(container, sw);
} catch (Exception e) {
throw new RuntimeException(e);
}
editor.putString(context.getString(preferenceStringId), sw.toString());
editor.commit();
}
}
@Nullable
protected abstract P transform(@NotNull T entity);
@NotNull
protected abstract MathEntityPersistenceContainer<P> createPersistenceContainer();
@NotNull
@Override
public List<T> getEntities() {
return mathRegistry.getEntities();
}
@NotNull
@Override
public List<T> getSystemEntities() {
return mathRegistry.getSystemEntities();
}
@Override
public T add(@NotNull JBuilder<? extends T> JBuilder) {
return mathRegistry.add(JBuilder);
}
@Override
public void remove(@NotNull T var) {
mathRegistry.remove(var);
}
@NotNull
@Override
public List<String> getNames() {
return mathRegistry.getNames();
}
@Override
public boolean contains(@NotNull String name) {
return mathRegistry.contains(name);
}
@Override
public T get(@NotNull String name) {
return mathRegistry.get(name);
}
@Override
public T getById(@NotNull Integer id) {
return mathRegistry.getById(id);
}
}

View File

@ -17,10 +17,7 @@ import jscl.math.function.Function;
import jscl.math.function.IConstant; import jscl.math.function.IConstant;
import jscl.math.operator.Operator; import jscl.math.operator.Operator;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.CalculatorEngine; import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.CalculatorEngineImpl;
import org.solovyev.android.calculator.CalculatorMathEngine;
import org.solovyev.android.calculator.CalculatorMathRegistry;
import org.solovyev.android.prefs.BooleanPreference; import org.solovyev.android.prefs.BooleanPreference;
import org.solovyev.android.prefs.Preference; import org.solovyev.android.prefs.Preference;
import org.solovyev.android.prefs.StringPreference; import org.solovyev.android.prefs.StringPreference;
@ -109,12 +106,12 @@ public class AndroidCalculatorEngine implements CalculatorEngine, SharedPreferen
this.lock = new Object(); this.lock = new Object();
final JsclMathEngine engine = JsclMathEngine.instance; final JsclMathEngine engine = JsclMathEngine.getInstance();
this.calculatorEngine = new CalculatorEngineImpl(engine, this.calculatorEngine = new CalculatorEngineImpl(engine,
new AndroidVarsRegistryImpl(engine.getConstantsRegistry(), application), new AndroidVarsRegistryImpl(engine.getConstantsRegistry(), new AndroidMathEntityDao<Var>(R.string.p_calc_vars, application, Vars.class)),
new AndroidFunctionsMathRegistry(engine.getFunctionsRegistry(), application), new AndroidFunctionsMathRegistry(engine.getFunctionsRegistry(), new AndroidMathEntityDao<AFunction>(R.string.p_calc_functions, application, Functions.class)),
new AndroidOperatorsMathRegistry(engine.getOperatorsRegistry(), application), new AndroidOperatorsMathRegistry(engine.getOperatorsRegistry(), new AndroidMathEntityDao<MathPersistenceEntity>(null, application, null)),
new AndroidPostfixFunctionsRegistry(engine.getPostfixFunctionsRegistry(), application), new AndroidPostfixFunctionsRegistry(engine.getPostfixFunctionsRegistry(), new AndroidMathEntityDao<MathPersistenceEntity>(null, application, null)),
this.lock); this.lock);
} }

View File

@ -1,180 +0,0 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator.model;
import android.app.Application;
import jscl.math.function.*;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.R;
import org.solovyev.common.JBuilder;
import org.solovyev.common.collections.CollectionsUtils;
import org.solovyev.common.math.MathRegistry;
import java.util.*;
/**
* User: serso
* Date: 11/17/11
* Time: 11:28 PM
*/
public class AndroidFunctionsMathRegistry extends AbstractAndroidMathRegistry<Function, AFunction> {
public static enum Category {
trigonometric(R.string.c_fun_category_trig, 100){
@Override
boolean isInCategory(@NotNull Function function) {
return (function instanceof Trigonometric || function instanceof ArcTrigonometric) && !hyperbolic_trigonometric.isInCategory(function);
}
},
hyperbolic_trigonometric(R.string.c_fun_category_hyper_trig, 300) {
private final List<String> names = Arrays.asList("sinh", "cosh", "tanh", "coth", "asinh", "acosh", "atanh", "acoth");
@Override
boolean isInCategory(@NotNull Function function) {
return names.contains(function.getName());
}
},
comparison(R.string.c_fun_category_comparison, 200) {
@Override
boolean isInCategory(@NotNull Function function) {
return function instanceof Comparison;
}
},
my(R.string.c_fun_category_my, 0) {
@Override
boolean isInCategory(@NotNull Function function) {
return !function.isSystem();
}
},
common(R.string.c_fun_category_common, 50) {
@Override
boolean isInCategory(@NotNull Function function) {
for (Category category : values()) {
if ( category != this ) {
if ( category.isInCategory(function) ) {
return false;
}
}
}
return true;
}
};
private final int captionId;
private final int tabOrder;
Category(int captionId, int tabOrder) {
this.captionId = captionId;
this.tabOrder = tabOrder;
}
public int getCaptionId() {
return captionId;
}
abstract boolean isInCategory(@NotNull Function function);
@NotNull
public static List<Category> getCategoriesByTabOrder() {
final List<Category> result = CollectionsUtils.asList(Category.values());
Collections.sort(result, new Comparator<Category>() {
@Override
public int compare(Category category, Category category1) {
return category.tabOrder - category1.tabOrder;
}
});
// todo serso: current solution (as creating functions is not implemented yet)
result.remove(my);
return result;
}
}
@NotNull
private static final Map<String, String> substitutes = new HashMap<String, String>();
static {
substitutes.put("", "sqrt");
}
@NotNull
private static final String FUNCTION_DESCRIPTION_PREFIX = "c_fun_description_";
public AndroidFunctionsMathRegistry(@NotNull MathRegistry<Function> functionsRegistry,
@NotNull Application application) {
super(functionsRegistry, FUNCTION_DESCRIPTION_PREFIX, application);
}
@Override
public void load() {
super.load();
add(new CustomFunction.Builder(true, "log", new String[]{"base", "x"}, "ln(x)/ln(base)"));
}
@NotNull
@Override
protected Map<String, String> getSubstitutes() {
return substitutes;
}
@Override
public String getCategory(@NotNull Function function) {
for (Category category : Category.values()) {
if ( category.isInCategory(function) ) {
return category.name();
}
}
return null;
}
@NotNull
@Override
protected JBuilder<? extends Function> createBuilder(@NotNull AFunction entity) {
return new CustomFunction.Builder(entity.getName(), entity.getParameterNamesAsArray(), entity.getContent());
}
@NotNull
@Override
protected Class<? extends MathEntityPersistenceContainer<AFunction>> getPersistenceContainerClass() {
return Functions.class;
}
@Override
protected Integer getPreferenceStringId() {
return R.string.p_calc_functions;
}
@Override
protected AFunction transform(@NotNull Function entity) {
if (entity instanceof CustomFunction) {
final AFunction result = new AFunction();
result.setName(entity.getName());
result.setContent(((CustomFunction) entity).getContent());
result.setParameterNames(((CustomFunction) entity).getParameterNames());
return result;
} else {
return null;
}
}
@NotNull
@Override
protected MathEntityPersistenceContainer<AFunction> createPersistenceContainer() {
return new Functions();
}
}

View File

@ -0,0 +1,95 @@
package org.solovyev.android.calculator.model;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import org.solovyev.android.calculator.MathEntityDao;
import org.solovyev.android.calculator.MathEntityPersistenceContainer;
import org.solovyev.android.calculator.MathPersistenceEntity;
import org.solovyev.android.calculator.R;
import java.io.StringWriter;
/**
* User: serso
* Date: 10/7/12
* Time: 6:46 PM
*/
public class AndroidMathEntityDao<T extends MathPersistenceEntity> implements MathEntityDao<T> {
@Nullable
private final Integer preferenceStringId;
@NotNull
private final Context context;
@Nullable
private final Class<? extends MathEntityPersistenceContainer<T>> persistenceContainerClass;
public AndroidMathEntityDao(@Nullable Integer preferenceStringId,
@NotNull Application application,
@Nullable Class<? extends MathEntityPersistenceContainer<T>> persistenceContainerClass) {
this.preferenceStringId = preferenceStringId;
this.context = application;
this.persistenceContainerClass = persistenceContainerClass;
}
@Override
public void save(@NotNull MathEntityPersistenceContainer<T> container) {
if (preferenceStringId != null) {
final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
final SharedPreferences.Editor editor = settings.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(context.getString(preferenceStringId), sw.toString());
editor.commit();
}
}
@Nullable
@Override
public MathEntityPersistenceContainer<T> load() {
if (persistenceContainerClass != null && preferenceStringId != null) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences != null) {
final String value = preferences.getString(context.getString(preferenceStringId), null);
if (value != null) {
final Serializer serializer = new Persister();
try {
return serializer.read(persistenceContainerClass, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
return null;
}
@Nullable
public String getDescription(@NotNull String descriptionId) {
final Resources resources = context.getResources();
final int stringId = resources.getIdentifier(descriptionId, "string", R.class.getPackage().getName());
try {
return resources.getString(stringId);
} catch (Resources.NotFoundException e) {
return null;
}
}
}

View File

@ -6,13 +6,9 @@
package org.solovyev.android.calculator.model; package org.solovyev.android.calculator.model;
import android.app.Application;
import jscl.math.function.ArcTrigonometric;
import jscl.math.function.Comparison;
import jscl.math.function.Trigonometric;
import jscl.math.operator.*; import jscl.math.operator.*;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.*;
import org.solovyev.common.JBuilder; import org.solovyev.common.JBuilder;
import org.solovyev.common.collections.CollectionsUtils; import org.solovyev.common.collections.CollectionsUtils;
import org.solovyev.common.math.MathRegistry; import org.solovyev.common.math.MathRegistry;
@ -24,7 +20,7 @@ import java.util.*;
* Date: 11/17/11 * Date: 11/17/11
* Time: 11:29 PM * Time: 11:29 PM
*/ */
public class AndroidOperatorsMathRegistry extends AbstractAndroidMathRegistry<Operator, MathPersistenceEntity> { public class AndroidOperatorsMathRegistry extends AbstractCalculatorMathRegistry<Operator, MathPersistenceEntity> {
@NotNull @NotNull
private static final Map<String, String> substitutes = new HashMap<String, String>(); private static final Map<String, String> substitutes = new HashMap<String, String>();
@ -41,8 +37,8 @@ public class AndroidOperatorsMathRegistry extends AbstractAndroidMathRegistry<Op
private static final String OPERATOR_DESCRIPTION_PREFIX = "c_op_description_"; private static final String OPERATOR_DESCRIPTION_PREFIX = "c_op_description_";
protected AndroidOperatorsMathRegistry(@NotNull MathRegistry<Operator> functionsRegistry, protected AndroidOperatorsMathRegistry(@NotNull MathRegistry<Operator> functionsRegistry,
@NotNull Application application) { @NotNull MathEntityDao<MathPersistenceEntity> mathEntityDao) {
super(functionsRegistry, OPERATOR_DESCRIPTION_PREFIX, application); super(functionsRegistry, OPERATOR_DESCRIPTION_PREFIX, mathEntityDao);
} }
@NotNull @NotNull
@ -72,17 +68,6 @@ public class AndroidOperatorsMathRegistry extends AbstractAndroidMathRegistry<Op
return null; //To change body of implemented methods use File | Settings | File Templates. return null; //To change body of implemented methods use File | Settings | File Templates.
} }
@NotNull
@Override
protected Class<? extends MathEntityPersistenceContainer<MathPersistenceEntity>> getPersistenceContainerClass() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
protected Integer getPreferenceStringId() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override @Override
public void save() { public void save() {
// not supported yet // not supported yet

View File

@ -6,9 +6,12 @@
package org.solovyev.android.calculator.model; package org.solovyev.android.calculator.model;
import android.app.Application;
import jscl.math.operator.Operator; import jscl.math.operator.Operator;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.AbstractCalculatorMathRegistry;
import org.solovyev.android.calculator.MathEntityDao;
import org.solovyev.android.calculator.MathEntityPersistenceContainer;
import org.solovyev.android.calculator.MathPersistenceEntity;
import org.solovyev.common.JBuilder; import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathRegistry; import org.solovyev.common.math.MathRegistry;
@ -20,7 +23,7 @@ import java.util.Map;
* Date: 11/19/11 * Date: 11/19/11
* Time: 1:48 PM * Time: 1:48 PM
*/ */
public class AndroidPostfixFunctionsRegistry extends AbstractAndroidMathRegistry<Operator, MathPersistenceEntity> { public class AndroidPostfixFunctionsRegistry extends AbstractCalculatorMathRegistry<Operator, MathPersistenceEntity> {
@NotNull @NotNull
private static final Map<String, String> substitutes = new HashMap<String, String>(); private static final Map<String, String> substitutes = new HashMap<String, String>();
@ -35,8 +38,8 @@ public class AndroidPostfixFunctionsRegistry extends AbstractAndroidMathRegistry
private static final String POSTFIX_FUNCTION_DESCRIPTION_PREFIX = "c_pf_description_"; private static final String POSTFIX_FUNCTION_DESCRIPTION_PREFIX = "c_pf_description_";
protected AndroidPostfixFunctionsRegistry(@NotNull MathRegistry<Operator> functionsRegistry, protected AndroidPostfixFunctionsRegistry(@NotNull MathRegistry<Operator> functionsRegistry,
@NotNull Application application) { @NotNull MathEntityDao<MathPersistenceEntity> mathEntityDao) {
super(functionsRegistry, POSTFIX_FUNCTION_DESCRIPTION_PREFIX, application); super(functionsRegistry, POSTFIX_FUNCTION_DESCRIPTION_PREFIX, mathEntityDao);
} }
@ -67,17 +70,6 @@ public class AndroidPostfixFunctionsRegistry extends AbstractAndroidMathRegistry
return null; //To change body of implemented methods use File | Settings | File Templates. return null; //To change body of implemented methods use File | Settings | File Templates.
} }
@NotNull
@Override
protected Class<? extends MathEntityPersistenceContainer<MathPersistenceEntity>> getPersistenceContainerClass() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
protected Integer getPreferenceStringId() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override @Override
public void save() { public void save() {
// not supported yet // not supported yet

View File

@ -6,10 +6,11 @@
package org.solovyev.android.calculator.model; package org.solovyev.android.calculator.model;
import android.app.Application;
import jscl.math.function.IConstant; import jscl.math.function.IConstant;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.AbstractCalculatorMathRegistry;
import org.solovyev.android.calculator.MathEntityDao;
import org.solovyev.android.calculator.MathEntityPersistenceContainer;
import org.solovyev.common.JBuilder; import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathRegistry; import org.solovyev.common.math.MathRegistry;
@ -21,7 +22,7 @@ import java.util.Map;
* Date: 9/29/11 * Date: 9/29/11
* Time: 4:57 PM * Time: 4:57 PM
*/ */
class AndroidVarsRegistryImpl extends AbstractAndroidMathRegistry<IConstant, Var> { class AndroidVarsRegistryImpl extends AbstractCalculatorMathRegistry<IConstant, Var> {
@NotNull @NotNull
private static final Map<String, String> substitutes = new HashMap<String, String>(); private static final Map<String, String> substitutes = new HashMap<String, String>();
@ -34,8 +35,8 @@ class AndroidVarsRegistryImpl extends AbstractAndroidMathRegistry<IConstant, Var
} }
protected AndroidVarsRegistryImpl(@NotNull MathRegistry<IConstant> mathRegistry, protected AndroidVarsRegistryImpl(@NotNull MathRegistry<IConstant> mathRegistry,
@NotNull Application application) { @NotNull MathEntityDao<Var> mathEntityDao) {
super(mathRegistry, "c_var_description_", application); super(mathRegistry, "c_var_description_", mathEntityDao);
} }
@NotNull @NotNull
@ -66,23 +67,12 @@ class AndroidVarsRegistryImpl extends AbstractAndroidMathRegistry<IConstant, Var
return new Var.Builder(entity); return new Var.Builder(entity);
} }
@NotNull
@Override
protected Class<? extends MathEntityPersistenceContainer<Var>> getPersistenceContainerClass() {
return Vars.class;
}
@NotNull @NotNull
@Override @Override
protected MathEntityPersistenceContainer<Var> createPersistenceContainer() { protected MathEntityPersistenceContainer<Var> createPersistenceContainer() {
return new Vars(); return new Vars();
} }
@NotNull
protected Integer getPreferenceStringId() {
return R.string.p_calc_vars;
}
private void tryToAddAuxVar(@NotNull String name) { private void tryToAddAuxVar(@NotNull String name) {
if ( !contains(name) ) { if ( !contains(name) ) {
add(new Var.Builder(name, (String)null)); add(new Var.Builder(name, (String)null));

View File

@ -14,6 +14,7 @@ import org.jetbrains.annotations.Nullable;
import org.simpleframework.xml.Element; import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root; import org.simpleframework.xml.Root;
import org.simpleframework.xml.Transient; import org.simpleframework.xml.Transient;
import org.solovyev.android.calculator.MathPersistenceEntity;
import org.solovyev.common.JBuilder; import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathEntity; import org.solovyev.common.math.MathEntity;
import org.solovyev.common.text.StringUtils; import org.solovyev.common.text.StringUtils;

View File

@ -1,8 +1,8 @@
package org.solovyev.android.calculator.model; package org.solovyev.android.calculator.model;
import jscl.math.function.IConstant;
import org.simpleframework.xml.ElementList; import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root; import org.simpleframework.xml.Root;
import org.solovyev.android.calculator.MathEntityPersistenceContainer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;

View File

@ -6,7 +6,6 @@
package org.solovyev.android.calculator.plot; package org.solovyev.android.calculator.plot;
import android.util.Log;
import jscl.math.Expression; import jscl.math.Expression;
import jscl.math.Generic; import jscl.math.Generic;
import jscl.math.JsclInteger; import jscl.math.JsclInteger;
@ -375,7 +374,7 @@ public final class PlotUtils {
// double dydx1 = dy2 / dx1; // double dydx1 = dy2 / dx1;
if ( dy2 > MAX_Y_DIFF && dx2 < MAX_X_DIFF && isDifferentSign(point.getY2(), point.getY1()) && isDifferentSign(point.getDyDx1(), point.getDyDx2())) { if ( dy2 > MAX_Y_DIFF && dx2 < MAX_X_DIFF && isDifferentSign(point.getY2(), point.getY1()) && isDifferentSign(point.getDyDx1(), point.getDyDx2())) {
Log.d(CalculatorPlotActivity.class.getName(), "Singularity: " + point); //Log.d(CalculatorPlotActivity.class.getName(), "Singularity: " + point);
//Log.d(CalculatorPlotActivity.class.getName(), String.valueOf(prevX + Math.abs(x - prevX) / 2) + ", null"); //Log.d(CalculatorPlotActivity.class.getName(), String.valueOf(prevX + Math.abs(x - prevX) / 2) + ", null");
series.add(point.getX1() + point.getAbsDx2() / 2, MathHelper.NULL_VALUE); series.add(point.getX1() + point.getAbsDx2() / 2, MathHelper.NULL_VALUE);
point.clearHistory(); point.clearHistory();

View File

@ -31,7 +31,7 @@ public class FromJsclNumericTextProcessorTest {
public void testCreateResultForComplexNumber() throws Exception { public void testCreateResultForComplexNumber() throws Exception {
final FromJsclNumericTextProcessor cm = new FromJsclNumericTextProcessor(); final FromJsclNumericTextProcessor cm = new FromJsclNumericTextProcessor();
final JsclMathEngine me = JsclMathEngine.instance; final JsclMathEngine me = JsclMathEngine.getInstance();
final AngleUnit defaultAngleUnits = me.getAngleUnits(); final AngleUnit defaultAngleUnits = me.getAngleUnits();
Assert.assertEquals("1.22133+23 123i", cm.process(Expression.valueOf("1.22133232+23123*i").numeric())); Assert.assertEquals("1.22133+23 123i", cm.process(Expression.valueOf("1.22133232+23123*i").numeric()));

View File

@ -36,7 +36,7 @@ public class NumeralBaseTest {
public void testConversion() throws Exception { public void testConversion() throws Exception {
CSVReader reader = null; CSVReader reader = null;
try { try {
final MathEngine me = JsclMathEngine.instance; final MathEngine me = JsclMathEngine.getInstance();
reader = new CSVReader(new InputStreamReader(NumeralBaseTest.class.getResourceAsStream("/org/solovyev/android/calculator/model/nb_table.csv")), '\t'); reader = new CSVReader(new InputStreamReader(NumeralBaseTest.class.getResourceAsStream("/org/solovyev/android/calculator/model/nb_table.csv")), '\t');

View File

@ -152,15 +152,15 @@ public class ToJsclTextProcessorTest {
public void testNumeralBases() throws Exception { public void testNumeralBases() throws Exception {
final TextProcessor<PreparedExpression, String> processor = ToJsclTextProcessor.getInstance(); final TextProcessor<PreparedExpression, String> processor = ToJsclTextProcessor.getInstance();
final NumeralBase defaultNumeralBase = JsclMathEngine.instance.getNumeralBase(); final NumeralBase defaultNumeralBase = JsclMathEngine.getInstance().getNumeralBase();
try{ try{
JsclMathEngine.instance.setNumeralBase(NumeralBase.bin); JsclMathEngine.getInstance().setNumeralBase(NumeralBase.bin);
Assert.assertEquals("101", JsclMathEngine.instance.evaluate("10+11")); Assert.assertEquals("101", JsclMathEngine.getInstance().evaluate("10+11"));
JsclMathEngine.instance.setNumeralBase(NumeralBase.hex); JsclMathEngine.getInstance().setNumeralBase(NumeralBase.hex);
Assert.assertEquals("56CE+CAD", processor.process("56CE+CAD").getExpression()); Assert.assertEquals("56CE+CAD", processor.process("56CE+CAD").getExpression());
} finally { } finally {
JsclMathEngine.instance.setNumeralBase(defaultNumeralBase); JsclMathEngine.getInstance().setNumeralBase(defaultNumeralBase);
} }
} }
} }

View File

@ -0,0 +1,19 @@
package org.solovyev.android.calculator.plot;
import com.xtremelabs.robolectric.RobolectricTestRunner;
import org.jetbrains.annotations.NotNull;
import org.junit.runners.model.InitializationError;
import java.io.File;
/**
* User: serso
* Date: 10/7/12
* Time: 5:36 PM
*/
public class CalculatorppTestRunner extends RobolectricTestRunner {
public CalculatorppTestRunner(@NotNull Class<?> testClass) throws InitializationError {
super(testClass, new File("calculatorpp"));
}
}

View File

@ -12,13 +12,13 @@ import junit.framework.Assert;
import org.achartengine.model.XYSeries; import org.achartengine.model.XYSeries;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
/** /**
* User: serso * User: serso
* Date: 12/5/11 * Date: 12/5/11
* Time: 9:07 PM * Time: 9:07 PM
*/ */
public class PlotUtilsTest { public class PlotUtilsTest {
@Test @Test

12
pom.xml
View File

@ -88,7 +88,7 @@
<dependency> <dependency>
<groupId>org.solovyev</groupId> <groupId>org.solovyev</groupId>
<artifactId>jscl</artifactId> <artifactId>jscl</artifactId>
<version>0.0.2</version> <version>0.0.3</version>
<exclusions> <exclusions>
<exclusion> <exclusion>
<artifactId>xercesImpl</artifactId> <artifactId>xercesImpl</artifactId>
@ -234,6 +234,16 @@
<version>1.5</version> <version>1.5</version>
</plugin> </plugin>
<!-- as some classes are singletons we must run each test in separate JVM-->
<!-- <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.2</version>
<configuration>
<forkMode>perTest</forkMode>
</configuration>
</plugin>-->
</plugins> </plugins>
</pluginManagement> </pluginManagement>