Delay math registry initialization
This commit is contained in:
@@ -37,7 +37,5 @@ public interface EntitiesRegistry<E extends MathEntity> extends MathRegistry<E>
|
||||
@Nullable
|
||||
Category getCategory(@Nonnull E entity);
|
||||
|
||||
void init();
|
||||
|
||||
void save();
|
||||
}
|
||||
|
@@ -23,9 +23,9 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import jscl.JsclMathEngine;
|
||||
import jscl.math.function.IConstant;
|
||||
|
||||
import org.simpleframework.xml.Serializer;
|
||||
import org.simpleframework.xml.core.Persister;
|
||||
import org.solovyev.android.Check;
|
||||
@@ -39,12 +39,16 @@ import org.solovyev.android.calculator.variables.OldVars;
|
||||
import org.solovyev.android.calculator.variables.VariableCategory;
|
||||
import org.solovyev.android.io.FileSaver;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import jscl.JsclMathEngine;
|
||||
import jscl.math.function.IConstant;
|
||||
|
||||
@Singleton
|
||||
public class VariablesRegistry extends BaseEntitiesRegistry<IConstant> {
|
||||
@@ -82,23 +86,20 @@ public class VariablesRegistry extends BaseEntitiesRegistry<IConstant> {
|
||||
bus.post(new RemovedEvent(variable));
|
||||
}
|
||||
|
||||
public void init() {
|
||||
@Override
|
||||
protected void onInit() {
|
||||
Check.isNotMainThread();
|
||||
|
||||
try {
|
||||
migrateOldVariables();
|
||||
migrateOldVariables();
|
||||
|
||||
for (CppVariable variable : loadEntities(CppVariable.JSON_CREATOR)) {
|
||||
addSafely(variable.toJsclConstant());
|
||||
}
|
||||
|
||||
addSafely("x");
|
||||
addSafely("y");
|
||||
addSafely("t");
|
||||
addSafely("j");
|
||||
} finally {
|
||||
setInitialized();
|
||||
for (CppVariable variable : loadEntities(CppVariable.JSON_CREATOR)) {
|
||||
addSafely(variable.toJsclConstant());
|
||||
}
|
||||
|
||||
addSafely("x");
|
||||
addSafely("y");
|
||||
addSafely("t");
|
||||
addSafely("j");
|
||||
}
|
||||
|
||||
private void migrateOldVariables() {
|
||||
|
@@ -27,7 +27,9 @@ import android.content.SharedPreferences;
|
||||
import android.os.Handler;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.StringRes;
|
||||
|
||||
import com.squareup.otto.Bus;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.solovyev.android.Check;
|
||||
@@ -41,14 +43,19 @@ import org.solovyev.android.io.FileSystem;
|
||||
import org.solovyev.common.math.MathEntity;
|
||||
import org.solovyev.common.math.MathRegistry;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
public abstract class BaseEntitiesRegistry<T extends MathEntity> implements EntitiesRegistry<T> {
|
||||
|
||||
@@ -103,8 +110,16 @@ public abstract class BaseEntitiesRegistry<T extends MathEntity> implements Enti
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
setInitialized();
|
||||
public final void init() {
|
||||
try {
|
||||
mathRegistry.init();
|
||||
onInit();
|
||||
} finally {
|
||||
setInitialized();
|
||||
}
|
||||
}
|
||||
|
||||
protected void onInit() {
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@@ -121,7 +136,7 @@ public abstract class BaseEntitiesRegistry<T extends MathEntity> implements Enti
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
protected final void setInitialized() {
|
||||
private final void setInitialized() {
|
||||
synchronized (lock) {
|
||||
Check.isTrue(!initialized);
|
||||
initialized = true;
|
||||
|
@@ -22,11 +22,10 @@
|
||||
|
||||
package org.solovyev.android.calculator.functions;
|
||||
|
||||
import static android.text.TextUtils.isEmpty;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import jscl.JsclMathEngine;
|
||||
import jscl.math.function.CustomFunction;
|
||||
import jscl.math.function.Function;
|
||||
import jscl.math.function.IFunction;
|
||||
|
||||
import org.simpleframework.xml.Serializer;
|
||||
import org.simpleframework.xml.core.Persister;
|
||||
import org.solovyev.android.Check;
|
||||
@@ -39,14 +38,22 @@ import org.solovyev.android.calculator.json.Jsonable;
|
||||
import org.solovyev.android.io.FileSaver;
|
||||
import org.solovyev.common.text.Strings;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
|
||||
import static android.text.TextUtils.isEmpty;
|
||||
import jscl.JsclMathEngine;
|
||||
import jscl.math.function.CustomFunction;
|
||||
import jscl.math.function.Function;
|
||||
import jscl.math.function.IFunction;
|
||||
|
||||
@Singleton
|
||||
public class FunctionsRegistry extends BaseEntitiesRegistry<Function> {
|
||||
@@ -95,26 +102,27 @@ public class FunctionsRegistry extends BaseEntitiesRegistry<Function> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
protected void onInit() {
|
||||
Check.isNotMainThread();
|
||||
try {
|
||||
migrateOldFunctions();
|
||||
migrateOldFunctions();
|
||||
|
||||
final List<CustomFunction.Builder> functions = new ArrayList<>();
|
||||
functions.add(new CustomFunction.Builder(true, "log", Arrays.asList("base", "x"), "ln(x)/ln(base)"));
|
||||
functions.add(new CustomFunction.Builder(true, "√3", Collections.singletonList("x"), "x^(1/3)"));
|
||||
functions.add(new CustomFunction.Builder(true, "√4", Collections.singletonList("x"), "x^(1/4)"));
|
||||
functions.add(new CustomFunction.Builder(true, "√n", Arrays.asList("x", "n"), "x^(1/n)"));
|
||||
functions.add(new CustomFunction.Builder(true, "re", Collections.singletonList("x"), "(x+conjugate(x))/2"));
|
||||
functions.add(new CustomFunction.Builder(true, "im", Collections.singletonList("x"), "(x-conjugate(x))/(2*i)"));
|
||||
final List<CustomFunction.Builder> functions = new ArrayList<>();
|
||||
functions.add(new CustomFunction.Builder(true, "log", Arrays.asList("base", "x"),
|
||||
"ln(x)/ln(base)"));
|
||||
functions.add(new CustomFunction.Builder(true, "√3", Collections.singletonList("x"),
|
||||
"x^(1/3)"));
|
||||
functions.add(new CustomFunction.Builder(true, "√4", Collections.singletonList("x"),
|
||||
"x^(1/4)"));
|
||||
functions.add(new CustomFunction.Builder(true, "√n", Arrays.asList("x", "n"), "x^(1/n)"));
|
||||
functions.add(new CustomFunction.Builder(true, "re", Collections.singletonList("x"),
|
||||
"(x+conjugate(x))/2"));
|
||||
functions.add(new CustomFunction.Builder(true, "im", Collections.singletonList("x"),
|
||||
"(x-conjugate(x))/(2*i)"));
|
||||
|
||||
for (CppFunction function : loadEntities(CppFunction.JSON_CREATOR)) {
|
||||
functions.add(function.toJsclBuilder());
|
||||
}
|
||||
addSafely(functions);
|
||||
} finally {
|
||||
setInitialized();
|
||||
for (CppFunction function : loadEntities(CppFunction.JSON_CREATOR)) {
|
||||
functions.add(function.toJsclBuilder());
|
||||
}
|
||||
addSafely(functions);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user