Error in case of loading user data (functions, vars etc)
This commit is contained in:
@@ -11,8 +11,8 @@ import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.common.JBuilder;
|
||||
import org.solovyev.common.math.MathEntity;
|
||||
import org.solovyev.common.math.MathRegistry;
|
||||
import org.solovyev.common.msg.Message;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -64,32 +64,40 @@ public abstract class AbstractCalculatorMathRegistry<T extends MathEntity, P ext
|
||||
public synchronized void load() {
|
||||
final MathEntityPersistenceContainer<P> persistenceContainer = mathEntityDao.load();
|
||||
|
||||
final List<P> notCreatedEntities = new ArrayList<P>();
|
||||
|
||||
if (persistenceContainer != null) {
|
||||
for (P entity : persistenceContainer.getEntities()) {
|
||||
if (!contains(entity.getName())) {
|
||||
try {
|
||||
final JBuilder<? extends T> builder = createBuilder(entity);
|
||||
add(builder);
|
||||
} catch (ArithmeticException e) {
|
||||
} catch (RuntimeException e) {
|
||||
Locator.getInstance().getLogger().error(null, e.getLocalizedMessage(), e);
|
||||
if (e instanceof Message) {
|
||||
Locator.getInstance().getNotifier().showMessage((Message)e);
|
||||
}
|
||||
notCreatedEntities.add(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*Log.d(AndroidVarsRegistry.class.getName(), vars.size() + " variables registered!");
|
||||
for (Var var : vars) {
|
||||
Log.d(AndroidVarsRegistry.class.getName(), var.toString());
|
||||
}*/
|
||||
}
|
||||
try {
|
||||
if (!notCreatedEntities.isEmpty()) {
|
||||
final StringBuilder errorMessage = new StringBuilder(notCreatedEntities.size() * 100);
|
||||
for (P notCreatedEntity : notCreatedEntities) {
|
||||
errorMessage.append(notCreatedEntity).append("\n\n");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_message_dialog, MessageDialogData.newInstance(CalculatorMessages.newErrorMessage(CalculatorMessages.msg_007, errorMessage.toString()), null));
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
// just in case
|
||||
Locator.getInstance().getLogger().error(null, e.getLocalizedMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected abstract JBuilder<? extends T> createBuilder(@NotNull P entity);
|
||||
|
||||
|
||||
@Override
|
||||
public synchronized void save() {
|
||||
final MathEntityPersistenceContainer<P> container = createPersistenceContainer();
|
||||
|
@@ -159,6 +159,9 @@ public enum CalculatorEventType {
|
||||
show_create_matrix_dialog,
|
||||
show_create_function_dialog,
|
||||
|
||||
/** {@link DialogData} */
|
||||
show_message_dialog,
|
||||
|
||||
plot_graph,
|
||||
|
||||
/** {@link org.solovyev.android.calculator.plot.PlotData} */
|
||||
|
@@ -12,6 +12,7 @@ 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.function.FunctionBuilderAdapter;
|
||||
import org.solovyev.android.calculator.model.AFunction;
|
||||
import org.solovyev.android.calculator.model.Functions;
|
||||
import org.solovyev.android.calculator.model.MathEntityBuilder;
|
||||
@@ -46,20 +47,20 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
super.load();
|
||||
|
||||
add(new CustomFunction.Builder(true, "log", Arrays.asList("base", "x"), "ln(x)/ln(base)"));
|
||||
add(new CustomFunction.Builder(true, "√3", Arrays.asList("x"), "x^(1/3)"));
|
||||
add(new CustomFunction.Builder(true, "√4", Arrays.asList("x"), "x^(1/4)"));
|
||||
add(new CustomFunction.Builder(true, "√n", Arrays.asList("x", "n"), "x^(1/n)"));
|
||||
add(new CustomFunction.Builder(true, "log", Arrays.asList("base", "x"), "ln(x)/ln(base)"));
|
||||
add(new CustomFunction.Builder(true, "√3", Arrays.asList("x"), "x^(1/3)"));
|
||||
add(new CustomFunction.Builder(true, "√4", Arrays.asList("x"), "x^(1/4)"));
|
||||
add(new CustomFunction.Builder(true, "√n", Arrays.asList("x", "n"), "x^(1/n)"));
|
||||
add(new CustomFunction.Builder(true, "re", Arrays.asList("x"), "(x+conjugate(x))/2"));
|
||||
add(new CustomFunction.Builder(true, "im", Arrays.asList("x"), "(x-conjugate(x))/(2*i)"));
|
||||
|
||||
super.load();
|
||||
}
|
||||
|
||||
public static void saveFunction(@NotNull CalculatorMathRegistry<Function> registry,
|
||||
@NotNull MathEntityBuilder<? extends Function> builder,
|
||||
@Nullable IFunction editedInstance,
|
||||
@NotNull Object source, boolean save) throws CustomFunctionCalculationException {
|
||||
@NotNull Object source, boolean save) throws CustomFunctionCalculationException, AFunction.Builder.CreationException {
|
||||
final Function addedFunction = registry.add(builder);
|
||||
|
||||
if (save) {
|
||||
@@ -111,7 +112,7 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis
|
||||
@NotNull
|
||||
@Override
|
||||
protected JBuilder<? extends Function> createBuilder(@NotNull AFunction function) {
|
||||
return new CustomFunction.Builder(function);
|
||||
return new FunctionBuilderAdapter(new AFunction.Builder(function));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -16,4 +16,6 @@ public interface CalculatorLogger {
|
||||
|
||||
void error(@Nullable String tag, @Nullable String message, @NotNull Throwable e);
|
||||
|
||||
void error(@Nullable String tag, @Nullable String message);
|
||||
|
||||
}
|
||||
|
@@ -1,6 +1,8 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.common.msg.MessageType;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.ResourceBundle;
|
||||
@@ -45,12 +47,23 @@ public final class CalculatorMessages {
|
||||
/* Infinite loop is detected in expression*/
|
||||
public static final String msg_006 = "msg_6";
|
||||
|
||||
/** Some data could not be loaded. Contact authors of application with information below.\n\nUnable to load:\n{0} */
|
||||
public static final String msg_007 = "msg_7";
|
||||
|
||||
/* Error */
|
||||
public static final String syntax_error = "syntax_error";
|
||||
|
||||
/* Result copied to clipboard! */
|
||||
public static final String result_copied = "result_copied";
|
||||
|
||||
/* Text copied to clipboard! */
|
||||
public static final String text_copied = "text_copied";
|
||||
|
||||
/* Last calculated value */
|
||||
public static final String ans_description = "ans_description";
|
||||
|
||||
@NotNull
|
||||
public static CalculatorMessage newErrorMessage(@NotNull String messageCode, @Nullable Object... parameters ) {
|
||||
return new CalculatorMessage(messageCode, MessageType.error, parameters);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,22 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.common.msg.MessageType;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 1/20/13
|
||||
* Time: 12:47 PM
|
||||
*/
|
||||
public interface DialogData {
|
||||
|
||||
@NotNull
|
||||
String getMessage();
|
||||
|
||||
@NotNull
|
||||
MessageType getMessageType();
|
||||
|
||||
@Nullable
|
||||
String getTitle();
|
||||
}
|
@@ -0,0 +1,48 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.common.msg.Message;
|
||||
import org.solovyev.common.msg.MessageType;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 1/20/13
|
||||
* Time: 12:45 PM
|
||||
*/
|
||||
public class MessageDialogData implements DialogData {
|
||||
|
||||
@NotNull
|
||||
private Message message;
|
||||
|
||||
@Nullable
|
||||
private String title;
|
||||
|
||||
private MessageDialogData(@NotNull Message message, @Nullable String title) {
|
||||
this.message = message;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static DialogData newInstance(@NotNull Message message, @Nullable String title) {
|
||||
return new MessageDialogData(message, title);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getMessage() {
|
||||
return message.getLocalizedMessage();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public MessageType getMessageType() {
|
||||
return message.getMessageType();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.common.msg.MessageType;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 1/20/13
|
||||
* Time: 1:01 PM
|
||||
*/
|
||||
public class StringDialogData implements DialogData {
|
||||
|
||||
@NotNull
|
||||
private final String message;
|
||||
|
||||
@NotNull
|
||||
private final MessageType messageType;
|
||||
|
||||
@Nullable
|
||||
private final String title;
|
||||
|
||||
private StringDialogData(@NotNull String message, @NotNull MessageType messageType, @Nullable String title) {
|
||||
this.message = message;
|
||||
this.messageType = messageType;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static DialogData newInstance(@NotNull String message, @NotNull MessageType messageType, @Nullable String title) {
|
||||
return new StringDialogData(message, messageType, title);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public MessageType getMessageType() {
|
||||
return messageType;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
}
|
@@ -34,4 +34,9 @@ public class SystemOutCalculatorLogger implements CalculatorLogger {
|
||||
System.out.println(getTag(tag) + ": " + message);
|
||||
e.printStackTrace(System.out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(@Nullable String tag, @Nullable String message) {
|
||||
System.out.println(getTag(tag) + ": " + message);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,52 @@
|
||||
package org.solovyev.android.calculator.function;
|
||||
|
||||
import jscl.CustomFunctionCalculationException;
|
||||
import jscl.math.function.CustomFunction;
|
||||
import jscl.math.function.Function;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.model.AFunction;
|
||||
import org.solovyev.android.calculator.model.MathEntityBuilder;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 1/20/13
|
||||
* Time: 12:21 PM
|
||||
*/
|
||||
public final class FunctionBuilderAdapter implements MathEntityBuilder<Function> {
|
||||
|
||||
@NotNull
|
||||
private final AFunction.Builder nestedBuilder;
|
||||
|
||||
public FunctionBuilderAdapter(@NotNull AFunction.Builder nestedBuilder) {
|
||||
this.nestedBuilder = nestedBuilder;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public MathEntityBuilder<Function> setName(@NotNull String name) {
|
||||
nestedBuilder.setName(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public MathEntityBuilder<Function> setDescription(@Nullable String description) {
|
||||
nestedBuilder.setDescription(description);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public MathEntityBuilder<Function> setValue(@Nullable String value) {
|
||||
nestedBuilder.setValue(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Function create() throws CustomFunctionCalculationException, AFunction.Builder.CreationException{
|
||||
final AFunction function = nestedBuilder.create();
|
||||
return new CustomFunction.Builder(function).create();
|
||||
}
|
||||
}
|
@@ -13,8 +13,8 @@ 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.Locator;
|
||||
import org.solovyev.android.calculator.CalculatorParseException;
|
||||
import org.solovyev.android.calculator.Locator;
|
||||
import org.solovyev.android.calculator.MathPersistenceEntity;
|
||||
import org.solovyev.common.math.MathEntity;
|
||||
import org.solovyev.common.msg.Message;
|
||||
@@ -122,7 +122,16 @@ public class AFunction implements IFunction, MathPersistenceEntity, Serializable
|
||||
return String.valueOf(this.content);
|
||||
}
|
||||
|
||||
/*
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AFunction{" +
|
||||
"name='" + name + '\'' +
|
||||
", parameterNames=" + parameterNames +
|
||||
", content='" + content + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* GETTERS/SETTERS
|
||||
@@ -258,7 +267,7 @@ public class AFunction implements IFunction, MathPersistenceEntity, Serializable
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public AFunction create() {
|
||||
public AFunction create() throws AFunction.Builder.CreationException{
|
||||
final AFunction result;
|
||||
if (id != null) {
|
||||
result = new AFunction(id);
|
||||
|
@@ -229,10 +229,10 @@ public class Var implements IConstant, MathPersistenceEntity {
|
||||
return !StringUtils.isEmpty(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ExtendedConstant.toString(this);
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return ExtendedConstant.toString(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
@@ -250,4 +250,5 @@ public class Var implements IConstant, MathPersistenceEntity {
|
||||
public int hashCode() {
|
||||
return name.hashCode();
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -4,7 +4,9 @@ msg_3=Too long execution time - check the expression
|
||||
msg_4=Evaluation was cancelled
|
||||
msg_5=No parameters are specified for function: {0}
|
||||
msg_6=Infinite loop is detected in expression
|
||||
msg_7=Some user data could not be loaded. Please contact authors of application with information below.\n\nUnable to load:\n{0}
|
||||
|
||||
syntax_error=Error
|
||||
result_copied=Result copied to clipboard!
|
||||
text_copied=Text copied to clipboard!
|
||||
ans_description=Last calculated value
|
@@ -4,7 +4,10 @@ msg_3=Вычисление выражения занимает слишком м
|
||||
msg_4=Вычисление было отменено
|
||||
msg_5=Для функции {0} не определены параметры
|
||||
msg_6=В выражении найден Бесконечный цикл - проверьте выражение
|
||||
msg_7=Некоторые пользовательские данные не могут быть загружены. Пожалуйста, свяжитесь с авторами приложения с информацией ниже.\n\nНевозможно загрузить:\n{0}
|
||||
|
||||
|
||||
syntax_error=Ошибка
|
||||
result_copied=Результат скопирован в буфер!
|
||||
text_copied=Текст скопирован в буфер!
|
||||
ans_description=Последний посчитанный результат
|
Reference in New Issue
Block a user