DI for ToJsclTextProcessor

This commit is contained in:
serso 2016-02-28 13:54:02 +01:00
parent 467789d323
commit 3edaa64d2d
4 changed files with 20 additions and 21 deletions

View File

@ -70,8 +70,6 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis
@Nonnull
private static final AtomicLong SEQUENCER = new AtomicLong(NO_SEQUENCE);
@Nonnull
private final ToJsclTextProcessor preprocessor = ToJsclTextProcessor.getInstance();
@Nonnull
private final SharedPreferences preferences;
@Nonnull
private final Bus bus;
@ -88,6 +86,8 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis
Editor editor;
@Inject
JsclMathEngine mathEngine;
@Inject
ToJsclTextProcessor preprocessor;
@Inject
public Calculator(@Nonnull SharedPreferences preferences, @Nonnull Bus bus, @Named(AppModule.THREAD_UI) @Nonnull Executor ui, @Named(AppModule.THREAD_BACKGROUND) @Nonnull Executor background) {

View File

@ -30,23 +30,19 @@ import org.solovyev.common.msg.MessageType;
import org.solovyev.common.search.StartsWithFinder;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.List;
@Singleton
public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, String> {
@Nonnull
private static final Integer MAX_DEPTH = 20;
@Nonnull
private static final ToJsclTextProcessor instance = new ToJsclTextProcessor();
private ToJsclTextProcessor() {
}
@Nonnull
public static ToJsclTextProcessor getInstance() {
return instance;
@Inject
public ToJsclTextProcessor() {
}
private static PreparedExpression processWithDepth(@Nonnull String s, int depth, @Nonnull List<IConstant> undefinedVars) throws ParseException {

View File

@ -43,6 +43,7 @@ import android.widget.EditText;
import android.widget.PopupWindow;
import butterknife.Bind;
import butterknife.ButterKnife;
import dagger.Lazy;
import jscl.math.function.IConstant;
import org.solovyev.android.Activities;
import org.solovyev.android.Check;
@ -96,6 +97,8 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF
FunctionsRegistry functionsRegistry;
@Inject
VariablesRegistry variablesRegistry;
@Inject
Lazy<ToJsclTextProcessor> toJsclTextProcessor;
@Nullable
private CppVariable variable;
@ -133,6 +136,15 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF
App.showDialog(create(variable), "variable-editor", fm);
}
public boolean isValidValue(@Nonnull String value) {
try {
final PreparedExpression pe = toJsclTextProcessor.get().process(value);
return !pe.hasUndefinedVariables();
} catch (RuntimeException e) {
return false;
}
}
@Override
public void onCreate(@android.support.annotation.Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -220,7 +232,7 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF
final String value = valueView.getText().toString();
if (!Strings.isEmpty(value)) {
// value is not empty => must be a number
if (!VariablesFragment.isValidValue(value)) {
if (!isValidValue(value)) {
setError(valueLabel, R.string.c_value_is_not_a_number);
return false;
}

View File

@ -55,15 +55,6 @@ public class VariablesFragment extends BaseEntitiesFragment<IConstant> {
@Inject
Bus bus;
public static boolean isValidValue(@Nonnull String value) {
try {
final PreparedExpression pe = ToJsclTextProcessor.getInstance().process(value);
return !pe.hasUndefinedVariables();
} catch (RuntimeException e) {
return false;
}
}
@Override
protected void inject(@Nonnull AppComponent component) {
super.inject(component);