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

@@ -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

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

View File

@@ -0,0 +1,20 @@
/*
* 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;
/**
* User: serso
* Date: 12/22/11
* Time: 5:27 PM
*/
public interface MathPersistenceEntity {
@NotNull
String getName();
}

View File

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

View File

@@ -0,0 +1,79 @@
/*
* 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.android.calculator.MathPersistenceEntity;
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()]);
}
}

View File

@@ -0,0 +1,28 @@
package org.solovyev.android.calculator.model;
import jscl.math.function.CustomFunction;
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: 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;
}
}