registries
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
@@ -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();
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -1,79 +1,78 @@
|
||||
/*
|
||||
* 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.math.edit;
|
||||
|
||||
import android.os.Bundle;
|
||||
import com.actionbarsherlock.app.SherlockFragmentActivity;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.*;
|
||||
import org.solovyev.android.calculator.about.CalculatorFragmentType;
|
||||
import org.solovyev.android.calculator.history.CalculatorHistoryActivity;
|
||||
import org.solovyev.android.calculator.model.AndroidFunctionsMathRegistry;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/21/11
|
||||
* Time: 10:33 PM
|
||||
*/
|
||||
public class CalculatorFunctionsActivity extends SherlockFragmentActivity implements CalculatorEventListener {
|
||||
|
||||
@NotNull
|
||||
private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryActivity.class.getSimpleName());
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
activityHelper.onCreate(this, savedInstanceState);
|
||||
|
||||
final CalculatorFragmentType fragmentType = CalculatorFragmentType.functions;
|
||||
|
||||
for (AndroidFunctionsMathRegistry.Category category : AndroidFunctionsMathRegistry.Category.getCategoriesByTabOrder()) {
|
||||
activityHelper.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), AbstractMathEntityListFragment.createBundleFor(category.name()), category.getCaptionId(), R.id.main_layout);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
|
||||
activityHelper.onSaveInstanceState(this, outState);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
|
||||
activityHelper.onResume(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
this.activityHelper.onPause(this);
|
||||
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
this.activityHelper.onDestroy(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||
switch (calculatorEventType) {
|
||||
case use_function:
|
||||
this.finish();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 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.math.edit;
|
||||
|
||||
import android.os.Bundle;
|
||||
import com.actionbarsherlock.app.SherlockFragmentActivity;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.*;
|
||||
import org.solovyev.android.calculator.about.CalculatorFragmentType;
|
||||
import org.solovyev.android.calculator.history.CalculatorHistoryActivity;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/21/11
|
||||
* Time: 10:33 PM
|
||||
*/
|
||||
public class CalculatorFunctionsActivity extends SherlockFragmentActivity implements CalculatorEventListener {
|
||||
|
||||
@NotNull
|
||||
private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryActivity.class.getSimpleName());
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
activityHelper.onCreate(this, savedInstanceState);
|
||||
|
||||
final CalculatorFragmentType fragmentType = CalculatorFragmentType.functions;
|
||||
|
||||
for (FunctionCategory category : FunctionCategory.getCategoriesByTabOrder()) {
|
||||
activityHelper.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), AbstractMathEntityListFragment.createBundleFor(category.name()), category.getCaptionId(), R.id.main_layout);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
|
||||
activityHelper.onSaveInstanceState(this, outState);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
|
||||
activityHelper.onResume(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
this.activityHelper.onPause(this);
|
||||
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
this.activityHelper.onDestroy(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||
switch (calculatorEventType) {
|
||||
case use_function:
|
||||
this.finish();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,78 +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 org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.simpleframework.xml.Element;
|
||||
import org.simpleframework.xml.Root;
|
||||
import org.solovyev.common.collections.CollectionsUtils;
|
||||
import org.solovyev.common.text.CollectionTransformations;
|
||||
import org.solovyev.common.text.StringMapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/22/11
|
||||
* Time: 5:25 PM
|
||||
*/
|
||||
|
||||
@Root
|
||||
public class AFunction implements MathPersistenceEntity {
|
||||
|
||||
@Element
|
||||
@NotNull
|
||||
private String name;
|
||||
|
||||
@Element
|
||||
@NotNull
|
||||
private String content;
|
||||
|
||||
|
||||
@Element(required = false)
|
||||
@Nullable
|
||||
private String parameterNames;
|
||||
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(@NotNull String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(@NotNull String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getParameterNames() {
|
||||
return parameterNames;
|
||||
}
|
||||
|
||||
public void setParameterNames(@Nullable String[] parameterNames) {
|
||||
this.parameterNames = CollectionTransformations.formatValue(CollectionsUtils.asList(parameterNames), ";", new StringMapper());
|
||||
}
|
||||
|
||||
public void setParameterNames(@Nullable String parameterNames) {
|
||||
this.parameterNames = parameterNames;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String[] getParameterNamesAsArray() {
|
||||
final List<String> parameterNamesAsList = CollectionTransformations.split(parameterNames, ";");
|
||||
return parameterNamesAsList.toArray(new String[parameterNamesAsList.size()]);
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
@@ -17,10 +17,7 @@ import jscl.math.function.Function;
|
||||
import jscl.math.function.IConstant;
|
||||
import jscl.math.operator.Operator;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.android.calculator.CalculatorEngine;
|
||||
import org.solovyev.android.calculator.CalculatorEngineImpl;
|
||||
import org.solovyev.android.calculator.CalculatorMathEngine;
|
||||
import org.solovyev.android.calculator.CalculatorMathRegistry;
|
||||
import org.solovyev.android.calculator.*;
|
||||
import org.solovyev.android.prefs.BooleanPreference;
|
||||
import org.solovyev.android.prefs.Preference;
|
||||
import org.solovyev.android.prefs.StringPreference;
|
||||
@@ -109,12 +106,12 @@ public class AndroidCalculatorEngine implements CalculatorEngine, SharedPreferen
|
||||
|
||||
this.lock = new Object();
|
||||
|
||||
final JsclMathEngine engine = JsclMathEngine.instance;
|
||||
final JsclMathEngine engine = JsclMathEngine.getInstance();
|
||||
this.calculatorEngine = new CalculatorEngineImpl(engine,
|
||||
new AndroidVarsRegistryImpl(engine.getConstantsRegistry(), application),
|
||||
new AndroidFunctionsMathRegistry(engine.getFunctionsRegistry(), application),
|
||||
new AndroidOperatorsMathRegistry(engine.getOperatorsRegistry(), application),
|
||||
new AndroidPostfixFunctionsRegistry(engine.getPostfixFunctionsRegistry(), application),
|
||||
new AndroidVarsRegistryImpl(engine.getConstantsRegistry(), new AndroidMathEntityDao<Var>(R.string.p_calc_vars, application, Vars.class)),
|
||||
new AndroidFunctionsMathRegistry(engine.getFunctionsRegistry(), new AndroidMathEntityDao<AFunction>(R.string.p_calc_functions, application, Functions.class)),
|
||||
new AndroidOperatorsMathRegistry(engine.getOperatorsRegistry(), new AndroidMathEntityDao<MathPersistenceEntity>(null, application, null)),
|
||||
new AndroidPostfixFunctionsRegistry(engine.getPostfixFunctionsRegistry(), new AndroidMathEntityDao<MathPersistenceEntity>(null, application, null)),
|
||||
this.lock);
|
||||
}
|
||||
|
||||
|
@@ -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();
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,180 +1,165 @@
|
||||
/*
|
||||
* 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.ArcTrigonometric;
|
||||
import jscl.math.function.Comparison;
|
||||
import jscl.math.function.Trigonometric;
|
||||
import jscl.math.operator.*;
|
||||
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:29 PM
|
||||
*/
|
||||
public class AndroidOperatorsMathRegistry extends AbstractAndroidMathRegistry<Operator, MathPersistenceEntity> {
|
||||
|
||||
@NotNull
|
||||
private static final Map<String, String> substitutes = new HashMap<String, String>();
|
||||
static {
|
||||
substitutes.put("Σ", "sum");
|
||||
substitutes.put("∏", "product");
|
||||
substitutes.put("∂", "derivative");
|
||||
substitutes.put("∫ab", "integral_ab");
|
||||
substitutes.put("∫", "integral");
|
||||
substitutes.put("Σ", "sum");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static final String OPERATOR_DESCRIPTION_PREFIX = "c_op_description_";
|
||||
|
||||
protected AndroidOperatorsMathRegistry(@NotNull MathRegistry<Operator> functionsRegistry,
|
||||
@NotNull Application application) {
|
||||
super(functionsRegistry, OPERATOR_DESCRIPTION_PREFIX, application);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected Map<String, String> getSubstitutes() {
|
||||
return substitutes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCategory(@NotNull Operator operator) {
|
||||
for (Category category : Category.values()) {
|
||||
if ( category.isInCategory(operator) ) {
|
||||
return category.name();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
// not supported yet
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected JBuilder<? extends Operator> createBuilder(@NotNull MathPersistenceEntity entity) {
|
||||
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
|
||||
public void save() {
|
||||
// not supported yet
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MathPersistenceEntity transform(@NotNull Operator entity) {
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected MathEntityPersistenceContainer<MathPersistenceEntity> createPersistenceContainer() {
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* STATIC
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
public static enum Category {
|
||||
|
||||
derivatives(R.string.derivatives, 100){
|
||||
@Override
|
||||
boolean isInCategory(@NotNull Operator operator) {
|
||||
return operator instanceof Derivative || operator instanceof Integral || operator instanceof IndefiniteIntegral;
|
||||
}
|
||||
},
|
||||
|
||||
other(R.string.other, 200) {
|
||||
@Override
|
||||
boolean isInCategory(@NotNull Operator operator) {
|
||||
return operator instanceof Sum || operator instanceof Product;
|
||||
}
|
||||
},
|
||||
|
||||
my(R.string.c_fun_category_my, 0) {
|
||||
@Override
|
||||
boolean isInCategory(@NotNull Operator operator) {
|
||||
return !operator.isSystem();
|
||||
}
|
||||
},
|
||||
|
||||
common(R.string.c_fun_category_common, 50) {
|
||||
@Override
|
||||
boolean isInCategory(@NotNull Operator operator) {
|
||||
for (Category category : values()) {
|
||||
if ( category != this ) {
|
||||
if ( category.isInCategory(operator) ) {
|
||||
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 Operator operator);
|
||||
|
||||
@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 operators is not implemented yet)
|
||||
result.remove(my);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 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 jscl.math.operator.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.android.calculator.*;
|
||||
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:29 PM
|
||||
*/
|
||||
public class AndroidOperatorsMathRegistry extends AbstractCalculatorMathRegistry<Operator, MathPersistenceEntity> {
|
||||
|
||||
@NotNull
|
||||
private static final Map<String, String> substitutes = new HashMap<String, String>();
|
||||
static {
|
||||
substitutes.put("Σ", "sum");
|
||||
substitutes.put("∏", "product");
|
||||
substitutes.put("∂", "derivative");
|
||||
substitutes.put("∫ab", "integral_ab");
|
||||
substitutes.put("∫", "integral");
|
||||
substitutes.put("Σ", "sum");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static final String OPERATOR_DESCRIPTION_PREFIX = "c_op_description_";
|
||||
|
||||
protected AndroidOperatorsMathRegistry(@NotNull MathRegistry<Operator> functionsRegistry,
|
||||
@NotNull MathEntityDao<MathPersistenceEntity> mathEntityDao) {
|
||||
super(functionsRegistry, OPERATOR_DESCRIPTION_PREFIX, mathEntityDao);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected Map<String, String> getSubstitutes() {
|
||||
return substitutes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCategory(@NotNull Operator operator) {
|
||||
for (Category category : Category.values()) {
|
||||
if ( category.isInCategory(operator) ) {
|
||||
return category.name();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
// not supported yet
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected JBuilder<? extends Operator> createBuilder(@NotNull MathPersistenceEntity entity) {
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
// not supported yet
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MathPersistenceEntity transform(@NotNull Operator entity) {
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected MathEntityPersistenceContainer<MathPersistenceEntity> createPersistenceContainer() {
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* STATIC
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
public static enum Category {
|
||||
|
||||
derivatives(R.string.derivatives, 100){
|
||||
@Override
|
||||
boolean isInCategory(@NotNull Operator operator) {
|
||||
return operator instanceof Derivative || operator instanceof Integral || operator instanceof IndefiniteIntegral;
|
||||
}
|
||||
},
|
||||
|
||||
other(R.string.other, 200) {
|
||||
@Override
|
||||
boolean isInCategory(@NotNull Operator operator) {
|
||||
return operator instanceof Sum || operator instanceof Product;
|
||||
}
|
||||
},
|
||||
|
||||
my(R.string.c_fun_category_my, 0) {
|
||||
@Override
|
||||
boolean isInCategory(@NotNull Operator operator) {
|
||||
return !operator.isSystem();
|
||||
}
|
||||
},
|
||||
|
||||
common(R.string.c_fun_category_common, 50) {
|
||||
@Override
|
||||
boolean isInCategory(@NotNull Operator operator) {
|
||||
for (Category category : values()) {
|
||||
if ( category != this ) {
|
||||
if ( category.isInCategory(operator) ) {
|
||||
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 Operator operator);
|
||||
|
||||
@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 operators is not implemented yet)
|
||||
result.remove(my);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,96 +1,88 @@
|
||||
/*
|
||||
* 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.operator.Operator;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.common.JBuilder;
|
||||
import org.solovyev.common.math.MathRegistry;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 11/19/11
|
||||
* Time: 1:48 PM
|
||||
*/
|
||||
public class AndroidPostfixFunctionsRegistry extends AbstractAndroidMathRegistry<Operator, MathPersistenceEntity> {
|
||||
|
||||
@NotNull
|
||||
private static final Map<String, String> substitutes = new HashMap<String, String>();
|
||||
static {
|
||||
substitutes.put("%", "percent");
|
||||
substitutes.put("!", "factorial");
|
||||
substitutes.put("!!", "double_factorial");
|
||||
substitutes.put("°", "degree");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static final String POSTFIX_FUNCTION_DESCRIPTION_PREFIX = "c_pf_description_";
|
||||
|
||||
protected AndroidPostfixFunctionsRegistry(@NotNull MathRegistry<Operator> functionsRegistry,
|
||||
@NotNull Application application) {
|
||||
super(functionsRegistry, POSTFIX_FUNCTION_DESCRIPTION_PREFIX, application);
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected Map<String, String> getSubstitutes() {
|
||||
return substitutes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCategory(@NotNull Operator operator) {
|
||||
for (AndroidOperatorsMathRegistry.Category category : AndroidOperatorsMathRegistry.Category.values()) {
|
||||
if ( category.isInCategory(operator) ) {
|
||||
return category.name();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
// not supported yet
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected JBuilder<? extends Operator> createBuilder(@NotNull MathPersistenceEntity entity) {
|
||||
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
|
||||
public void save() {
|
||||
// not supported yet
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MathPersistenceEntity transform(@NotNull Operator entity) {
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected MathEntityPersistenceContainer<MathPersistenceEntity> createPersistenceContainer() {
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 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 jscl.math.operator.Operator;
|
||||
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.math.MathRegistry;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 11/19/11
|
||||
* Time: 1:48 PM
|
||||
*/
|
||||
public class AndroidPostfixFunctionsRegistry extends AbstractCalculatorMathRegistry<Operator, MathPersistenceEntity> {
|
||||
|
||||
@NotNull
|
||||
private static final Map<String, String> substitutes = new HashMap<String, String>();
|
||||
static {
|
||||
substitutes.put("%", "percent");
|
||||
substitutes.put("!", "factorial");
|
||||
substitutes.put("!!", "double_factorial");
|
||||
substitutes.put("°", "degree");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static final String POSTFIX_FUNCTION_DESCRIPTION_PREFIX = "c_pf_description_";
|
||||
|
||||
protected AndroidPostfixFunctionsRegistry(@NotNull MathRegistry<Operator> functionsRegistry,
|
||||
@NotNull MathEntityDao<MathPersistenceEntity> mathEntityDao) {
|
||||
super(functionsRegistry, POSTFIX_FUNCTION_DESCRIPTION_PREFIX, mathEntityDao);
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected Map<String, String> getSubstitutes() {
|
||||
return substitutes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCategory(@NotNull Operator operator) {
|
||||
for (AndroidOperatorsMathRegistry.Category category : AndroidOperatorsMathRegistry.Category.values()) {
|
||||
if ( category.isInCategory(operator) ) {
|
||||
return category.name();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
// not supported yet
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected JBuilder<? extends Operator> createBuilder(@NotNull MathPersistenceEntity entity) {
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
// not supported yet
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MathPersistenceEntity transform(@NotNull Operator entity) {
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected MathEntityPersistenceContainer<MathPersistenceEntity> createPersistenceContainer() {
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
|
@@ -6,10 +6,11 @@
|
||||
|
||||
package org.solovyev.android.calculator.model;
|
||||
|
||||
import android.app.Application;
|
||||
import jscl.math.function.IConstant;
|
||||
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.math.MathRegistry;
|
||||
|
||||
@@ -21,7 +22,7 @@ import java.util.Map;
|
||||
* Date: 9/29/11
|
||||
* Time: 4:57 PM
|
||||
*/
|
||||
class AndroidVarsRegistryImpl extends AbstractAndroidMathRegistry<IConstant, Var> {
|
||||
class AndroidVarsRegistryImpl extends AbstractCalculatorMathRegistry<IConstant, Var> {
|
||||
|
||||
@NotNull
|
||||
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,
|
||||
@NotNull Application application) {
|
||||
super(mathRegistry, "c_var_description_", application);
|
||||
@NotNull MathEntityDao<Var> mathEntityDao) {
|
||||
super(mathRegistry, "c_var_description_", mathEntityDao);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -66,24 +67,13 @@ class AndroidVarsRegistryImpl extends AbstractAndroidMathRegistry<IConstant, Var
|
||||
return new Var.Builder(entity);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected Class<? extends MathEntityPersistenceContainer<Var>> getPersistenceContainerClass() {
|
||||
return Vars.class;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@NotNull
|
||||
@Override
|
||||
protected MathEntityPersistenceContainer<Var> createPersistenceContainer() {
|
||||
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) ) {
|
||||
add(new Var.Builder(name, (String)null));
|
||||
}
|
||||
|
@@ -1,29 +0,0 @@
|
||||
package org.solovyev.android.calculator.model;
|
||||
|
||||
import jscl.math.function.CustomFunction;
|
||||
import jscl.math.function.Function;
|
||||
import jscl.math.function.IConstant;
|
||||
import org.simpleframework.xml.ElementList;
|
||||
import org.simpleframework.xml.Root;
|
||||
|
||||
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> {
|
||||
|
||||
@ElementList(type = CustomFunction.class)
|
||||
private List<AFunction> functions = new ArrayList<AFunction>();
|
||||
|
||||
public Functions() {
|
||||
}
|
||||
|
||||
public List<AFunction> getEntities() {
|
||||
return functions;
|
||||
}
|
||||
}
|
@@ -1,12 +0,0 @@
|
||||
package org.solovyev.android.calculator.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/22/11
|
||||
* Time: 5:03 PM
|
||||
*/
|
||||
public interface MathEntityPersistenceContainer<T extends MathPersistenceEntity> {
|
||||
public List<T> getEntities();
|
||||
}
|
@@ -1,20 +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 org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/22/11
|
||||
* Time: 5:27 PM
|
||||
*/
|
||||
public interface MathPersistenceEntity {
|
||||
|
||||
@NotNull
|
||||
String getName();
|
||||
}
|
@@ -14,6 +14,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import org.simpleframework.xml.Element;
|
||||
import org.simpleframework.xml.Root;
|
||||
import org.simpleframework.xml.Transient;
|
||||
import org.solovyev.android.calculator.MathPersistenceEntity;
|
||||
import org.solovyev.common.JBuilder;
|
||||
import org.solovyev.common.math.MathEntity;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
|
@@ -1,8 +1,8 @@
|
||||
package org.solovyev.android.calculator.model;
|
||||
|
||||
import jscl.math.function.IConstant;
|
||||
import org.simpleframework.xml.ElementList;
|
||||
import org.simpleframework.xml.Root;
|
||||
import org.solovyev.android.calculator.MathEntityPersistenceContainer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@@ -6,7 +6,6 @@
|
||||
|
||||
package org.solovyev.android.calculator.plot;
|
||||
|
||||
import android.util.Log;
|
||||
import jscl.math.Expression;
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.JsclInteger;
|
||||
@@ -375,7 +374,7 @@ public final class PlotUtils {
|
||||
// double dydx1 = dy2 / dx1;
|
||||
|
||||
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");
|
||||
series.add(point.getX1() + point.getAbsDx2() / 2, MathHelper.NULL_VALUE);
|
||||
point.clearHistory();
|
||||
|
Reference in New Issue
Block a user