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 @Nonnull
private static final AtomicLong SEQUENCER = new AtomicLong(NO_SEQUENCE); private static final AtomicLong SEQUENCER = new AtomicLong(NO_SEQUENCE);
@Nonnull @Nonnull
private final ToJsclTextProcessor preprocessor = ToJsclTextProcessor.getInstance();
@Nonnull
private final SharedPreferences preferences; private final SharedPreferences preferences;
@Nonnull @Nonnull
private final Bus bus; private final Bus bus;
@ -88,6 +86,8 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis
Editor editor; Editor editor;
@Inject @Inject
JsclMathEngine mathEngine; JsclMathEngine mathEngine;
@Inject
ToJsclTextProcessor preprocessor;
@Inject @Inject
public Calculator(@Nonnull SharedPreferences preferences, @Nonnull Bus bus, @Named(AppModule.THREAD_UI) @Nonnull Executor ui, @Named(AppModule.THREAD_BACKGROUND) @Nonnull Executor background) { 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 org.solovyev.common.search.StartsWithFinder;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@Singleton
public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, String> { public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, String> {
@Nonnull @Nonnull
private static final Integer MAX_DEPTH = 20; private static final Integer MAX_DEPTH = 20;
@Nonnull @Inject
private static final ToJsclTextProcessor instance = new ToJsclTextProcessor(); public ToJsclTextProcessor() {
private ToJsclTextProcessor() {
}
@Nonnull
public static ToJsclTextProcessor getInstance() {
return instance;
} }
private static PreparedExpression processWithDepth(@Nonnull String s, int depth, @Nonnull List<IConstant> undefinedVars) throws ParseException { 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 android.widget.PopupWindow;
import butterknife.Bind; import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import dagger.Lazy;
import jscl.math.function.IConstant; import jscl.math.function.IConstant;
import org.solovyev.android.Activities; import org.solovyev.android.Activities;
import org.solovyev.android.Check; import org.solovyev.android.Check;
@ -96,6 +97,8 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF
FunctionsRegistry functionsRegistry; FunctionsRegistry functionsRegistry;
@Inject @Inject
VariablesRegistry variablesRegistry; VariablesRegistry variablesRegistry;
@Inject
Lazy<ToJsclTextProcessor> toJsclTextProcessor;
@Nullable @Nullable
private CppVariable variable; private CppVariable variable;
@ -133,6 +136,15 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF
App.showDialog(create(variable), "variable-editor", fm); 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 @Override
public void onCreate(@android.support.annotation.Nullable Bundle savedInstanceState) { public void onCreate(@android.support.annotation.Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -220,7 +232,7 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF
final String value = valueView.getText().toString(); final String value = valueView.getText().toString();
if (!Strings.isEmpty(value)) { if (!Strings.isEmpty(value)) {
// value is not empty => must be a number // 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); setError(valueLabel, R.string.c_value_is_not_a_number);
return false; return false;
} }

View File

@ -55,15 +55,6 @@ public class VariablesFragment extends BaseEntitiesFragment<IConstant> {
@Inject @Inject
Bus bus; 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 @Override
protected void inject(@Nonnull AppComponent component) { protected void inject(@Nonnull AppComponent component) {
super.inject(component); super.inject(component);