This commit is contained in:
Sergey Solovyev
2012-11-15 17:41:13 +04:00
parent b91c8bc19d
commit 39b45c509e
12 changed files with 335 additions and 86 deletions

View File

@@ -68,4 +68,7 @@ public interface Calculator extends CalculatorEventContainer, HistoryControl<Cal
@NotNull
CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Long sequenceId);
@NotNull
PreparedExpression prepareExpression(@NotNull String expression) throws CalculatorParseException;
}

View File

@@ -122,7 +122,7 @@ public enum CalculatorEventType {
// @NotNull Function
function_added,
// @NotNull Change<Function>
// @NotNull Change<IFunction>
function_changed,
/*

View File

@@ -9,6 +9,7 @@ package org.solovyev.android.calculator;
import jscl.CustomFunctionCalculationException;
import jscl.math.function.CustomFunction;
import jscl.math.function.Function;
import jscl.math.function.IFunction;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.model.AFunction;
@@ -16,8 +17,8 @@ import org.solovyev.android.calculator.model.Functions;
import org.solovyev.android.calculator.model.MathEntityBuilder;
import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathRegistry;
import org.solovyev.common.text.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@@ -52,7 +53,7 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis
public static void saveFunction(@NotNull CalculatorMathRegistry<Function> registry,
@NotNull MathEntityBuilder<? extends Function> builder,
@Nullable Function editedInstance,
@Nullable IFunction editedInstance,
@NotNull Object source, boolean save) throws CustomFunctionCalculationException {
final Function addedFunction = registry.add(builder);
@@ -84,22 +85,34 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis
return null;
}
@Nullable
@Override
public String getDescription(@NotNull String functionName) {
final Function function = get(functionName);
String result = null;
if ( function instanceof CustomFunction ) {
result = ((CustomFunction) function).getDescription();
}
if (StringUtils.isEmpty(result) ) {
result = super.getDescription(functionName);
}
return result;
}
@NotNull
@Override
protected JBuilder<? extends Function> createBuilder(@NotNull AFunction entity) {
CustomFunction.Builder builder = new CustomFunction.Builder(entity.getName(), entity.getParameterNames(), entity.getContent());
builder.setDescription(entity.getDescription());
return builder;
protected JBuilder<? extends Function> createBuilder(@NotNull AFunction function) {
return new CustomFunction.Builder(function);
}
@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(new ArrayList<String>(((CustomFunction) entity).getParameterNames()));
return result;
protected AFunction transform(@NotNull Function function) {
if (function instanceof CustomFunction) {
return AFunction.fromIFunction((CustomFunction) function);
} else {
return null;
}

View File

@@ -169,9 +169,9 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
if (StringUtils.isEmpty(expression)) {
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_result, CalculatorOutputImpl.newEmptyOutput(operation));
} else {
preparedExpression = preprocessor.process(expression);
preparedExpression = prepareExpression(expression);
final String jsclExpression = preparedExpression.toString();
final String jsclExpression = preparedExpression.toString();
try {
@@ -204,7 +204,13 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
}
}
@NotNull
@NotNull
@Override
public PreparedExpression prepareExpression(@NotNull String expression) throws CalculatorParseException {
return preprocessor.process(expression);
}
@NotNull
private CalculatorEventData newCalculationEventData(@NotNull JsclOperation operation,
@NotNull String expression,
@NotNull Long calculationId) {

View File

@@ -13,13 +13,19 @@ import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Transient;
import org.solovyev.android.calculator.CalculatorLocatorImpl;
import org.solovyev.android.calculator.CalculatorParseException;
import org.solovyev.android.calculator.MathPersistenceEntity;
import org.solovyev.common.math.MathEntity;
import org.solovyev.common.msg.Message;
import org.solovyev.common.msg.MessageType;
import org.solovyev.common.text.StringUtils;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
/**
* User: serso
@@ -28,7 +34,7 @@ import java.util.List;
*/
@Root(name = "function")
public class AFunction implements IFunction, MathPersistenceEntity {
public class AFunction implements IFunction, MathPersistenceEntity, Serializable {
/*
**********************************************************************
@@ -74,6 +80,14 @@ public class AFunction implements IFunction, MathPersistenceEntity {
this.id = id;
}
public static AFunction fromIFunction(@NotNull IFunction function) {
final AFunction result = new AFunction();
copy(result, function);
return result;
}
/*
**********************************************************************
*
@@ -85,19 +99,24 @@ public class AFunction implements IFunction, MathPersistenceEntity {
@Override
public void copy(@NotNull MathEntity mathEntity) {
if (mathEntity instanceof IFunction) {
final IFunction that = ((IFunction) mathEntity);
this.name = that.getName();
this.content = that.getContent();
this.description = StringUtils.getNotEmpty(this.getDescription(), "");
this.system = that.isSystem();
if (that.isIdDefined()) {
this.id = that.getId();
}
copy(this, (IFunction) mathEntity);
} else {
throw new IllegalArgumentException("Trying to make a copy of unsupported type: " + mathEntity.getClass());
}
}
private static void copy(@NotNull AFunction target,
@NotNull IFunction source) {
target.name = source.getName();
target.content = source.getContent();
target.description = StringUtils.getNotEmpty(source.getDescription(), "");
target.system = source.isSystem();
if (source.isIdDefined()) {
target.id = source.getId();
}
target.parameterNames = new ArrayList<String>(source.getParameterNames());
}
@Override
public String toJava() {
return String.valueOf(this.content);
@@ -248,12 +267,57 @@ public class AFunction implements IFunction, MathPersistenceEntity {
}
result.name = name;
result.content = value;
try {
result.content = CalculatorLocatorImpl.getInstance().getCalculator().prepareExpression(value).toString();
} catch (CalculatorParseException e) {
throw new CreationException(e);
}
result.system = system;
result.description = StringUtils.getNotEmpty(description, "");
result.parameterNames = new ArrayList<String>(parameterNames);
return result;
}
public static class CreationException extends RuntimeException implements Message {
@NotNull
private final CalculatorParseException message;
public CreationException(@NotNull CalculatorParseException cause) {
super(cause);
message = cause;
}
@NotNull
@Override
public String getMessageCode() {
return message.getMessageCode();
}
@NotNull
@Override
public List<Object> getParameters() {
return message.getParameters();
}
@NotNull
@Override
public MessageType getMessageType() {
return message.getMessageType();
}
@Override
@Nullable
public String getLocalizedMessage() {
return message.getLocalizedMessage();
}
@NotNull
@Override
public String getLocalizedMessage(@NotNull Locale locale) {
return message.getLocalizedMessage(locale);
}
}
}
}