registries

This commit is contained in:
Sergey Solovyev
2012-10-07 20:11:29 +04:00
parent 52e6b4b79d
commit 5f6480ba4e
10 changed files with 14 additions and 17 deletions

View File

@@ -1,8 +1,7 @@
package org.solovyev.android.calculator.model;
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.R;
/**
* User: serso

View File

@@ -1,89 +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;
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 CalculatorFunctionsMathRegistry 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 CalculatorFunctionsMathRegistry(@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

@@ -4,11 +4,10 @@
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator.model;
package org.solovyev.android.calculator;
import jscl.math.operator.Operator;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.*;
import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathRegistry;
@@ -34,7 +33,7 @@ public class CalculatorPostfixFunctionsRegistry extends AbstractCalculatorMathRe
@NotNull
private static final String POSTFIX_FUNCTION_DESCRIPTION_PREFIX = "c_pf_description_";
protected CalculatorPostfixFunctionsRegistry(@NotNull MathRegistry<Operator> functionsRegistry,
public CalculatorPostfixFunctionsRegistry(@NotNull MathRegistry<Operator> functionsRegistry,
@NotNull MathEntityDao<MathPersistenceEntity> mathEntityDao) {
super(functionsRegistry, POSTFIX_FUNCTION_DESCRIPTION_PREFIX, mathEntityDao);
}

View File

@@ -4,13 +4,12 @@
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator.model;
package org.solovyev.android.calculator;
import jscl.math.function.IConstant;
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.model.Var;
import org.solovyev.android.calculator.model.Vars;
import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathRegistry;
@@ -22,7 +21,7 @@ import java.util.Map;
* Date: 9/29/11
* Time: 4:57 PM
*/
class CalculatorVarsRegistry extends AbstractCalculatorMathRegistry<IConstant, Var> {
public class CalculatorVarsRegistry extends AbstractCalculatorMathRegistry<IConstant, Var> {
@NotNull
private static final Map<String, String> substitutes = new HashMap<String, String>();
@@ -34,7 +33,7 @@ class CalculatorVarsRegistry extends AbstractCalculatorMathRegistry<IConstant, V
substitutes.put("NaN", "nan");
}
protected CalculatorVarsRegistry(@NotNull MathRegistry<IConstant> mathRegistry,
public CalculatorVarsRegistry(@NotNull MathRegistry<IConstant> mathRegistry,
@NotNull MathEntityDao<Var> mathEntityDao) {
super(mathRegistry, "c_var_description_", mathEntityDao);
}

View File

@@ -1,92 +0,0 @@
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

@@ -1,4 +1,4 @@
package org.solovyev.android.calculator.model;
package org.solovyev.android.calculator;
import jscl.math.function.IConstant;
import org.jetbrains.annotations.NotNull;
@@ -17,14 +17,14 @@ public enum VarCategory {
system(100){
@Override
boolean isInCategory(@NotNull IConstant var) {
public boolean isInCategory(@NotNull IConstant var) {
return var.isSystem();
}
},
my(0) {
@Override
boolean isInCategory(@NotNull IConstant var) {
public boolean isInCategory(@NotNull IConstant var) {
return !var.isSystem();
}
};
@@ -35,7 +35,7 @@ public enum VarCategory {
this.tabOrder = tabOrder;
}
abstract boolean isInCategory(@NotNull IConstant var);
public abstract boolean isInCategory(@NotNull IConstant var);
@NotNull
public static List<VarCategory> getCategoriesByTabOrder() {

View File

@@ -14,8 +14,8 @@ 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.AndroidVarCategory;
import org.solovyev.android.calculator.model.VarCategory;
import org.solovyev.android.calculator.AndroidVarCategory;
import org.solovyev.android.calculator.VarCategory;
/**
* User: serso

View File

@@ -1,29 +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.solovyev.common.JBuilder;
import org.solovyev.common.math.MathEntity;
/**
* User: serso
* Date: 12/22/11
* Time: 9:21 PM
*/
public interface MathEntityBuilder<T extends MathEntity> extends JBuilder<T> {
@NotNull
public MathEntityBuilder<T> setName(@NotNull String name);
@NotNull
public MathEntityBuilder<T> setDescription(@Nullable String description);
@NotNull
public MathEntityBuilder<T> setValue(@Nullable String value);
}

View File

@@ -1,253 +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 jscl.math.function.Constant;
import jscl.math.function.ExtendedConstant;
import jscl.math.function.IConstant;
import org.jetbrains.annotations.NotNull;
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;
/**
* User: serso
* Date: 9/28/11
* Time: 11:22 PM
*/
@Root
public class Var implements IConstant, MathPersistenceEntity {
@Transient
private Integer id;
@Element
@NotNull
private String name;
@Element(required = false)
@Nullable
private String value;
@Element
private boolean system;
@Element(required = false)
@Nullable
private String description;
@Transient
private Constant constant;
public static class Builder implements JBuilder<Var>, MathEntityBuilder<Var> {
@NotNull
private String name;
@Nullable
private String value;
private boolean system = false;
@Nullable
private String description;
@Nullable
private Integer id;
public Builder() {
}
public Builder(@NotNull Var var) {
this.name = var.name;
this.value = var.value;
this.system = var.system;
this.description = var.description;
this.id = var.id;
}
public Builder(@NotNull IConstant iConstant) {
this.name = iConstant.getName();
this.value = iConstant.getValue();
this.system = iConstant.isSystem();
this.description = iConstant.getDescription();
if (iConstant.isIdDefined()) {
this.id = iConstant.getId();
}
}
public Builder(@NotNull String name, @NotNull Double value) {
this(name, String.valueOf(value));
}
public Builder(@NotNull String name, @Nullable String value) {
this.name = name;
this.value = value;
}
@NotNull
public Builder setName(@NotNull String name) {
this.name = name;
return this;
}
@NotNull
public Builder setValue(@Nullable String value) {
this.value = value;
return this;
}
protected Builder setSystem(boolean system) {
this.system = system;
return this;
}
@NotNull
public Builder setDescription(@Nullable String description) {
this.description = description;
return this;
}
@NotNull
public Var create() {
final Var result;
if (id != null) {
result = new Var(id);
} else {
result = new Var();
}
result.name = name;
result.value = value;
result.system = system;
result.description = description;
return result;
}
}
private Var() {
}
private Var(@NotNull Integer id) {
this.id = id;
}
public void copy(@NotNull MathEntity o) {
if (o instanceof IConstant) {
final IConstant that = ((IConstant) o);
this.name = that.getName();
this.value = that.getValue();
this.description = that.getDescription();
this.system = that.isSystem();
if (that.isIdDefined()) {
this.id = that.getId();
}
} else {
throw new IllegalArgumentException("Trying to make a copy of unsupported type: " + o.getClass());
}
}
@Nullable
public Double getDoubleValue() {
Double result = null;
if (value != null) {
try {
result = Double.valueOf(value);
} catch (NumberFormatException e) {
// do nothing - string is not a double
}
}
return result;
}
@Nullable
public String getValue() {
return value;
}
@NotNull
@Override
public String toJava() {
return String.valueOf(value);
}
public boolean isSystem() {
return system;
}
@NotNull
@Override
public Integer getId() {
return this.id;
}
@Override
public boolean isIdDefined() {
return this.id != null;
}
@Override
public void setId(@NotNull Integer id) {
this.id = id;
}
@NotNull
public String getName() {
return name;
}
@NotNull
@Override
public Constant getConstant() {
if (constant == null) {
constant = new Constant(this.name);
}
return constant;
}
@Nullable
public String getDescription() {
return description;
}
@Override
public boolean isDefined() {
return !StringUtils.isEmpty(value);
}
@Override
public String toString() {
return ExtendedConstant.toString(this);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Var var = (Var) o;
if (!name.equals(var.name)) return false;
return true;
}
@Override
public int hashCode() {
return name.hashCode();
}
}

View File

@@ -1,28 +0,0 @@
package org.solovyev.android.calculator.model;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.solovyev.android.calculator.MathEntityPersistenceContainer;
import java.util.ArrayList;
import java.util.List;
/**
* User: serso
* Date: 9/29/11
* Time: 5:19 PM
*/
@Root
public class Vars implements MathEntityPersistenceContainer<Var> {
@ElementList(type = Var.class)
private List<Var> vars = new ArrayList<Var>();
public Vars() {
}
public List<Var> getEntities() {
return vars;
}
}