Remove Locator class
This commit is contained in:
@@ -41,7 +41,6 @@ import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.text.Spannable;
|
||||
import android.text.SpannableString;
|
||||
import android.text.SpannableStringBuilder;
|
||||
import android.text.style.ForegroundColorSpan;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
@@ -60,9 +59,7 @@ import org.solovyev.android.wizard.Wizards;
|
||||
import org.solovyev.common.JPredicate;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Formatter;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@@ -79,16 +76,6 @@ public final class App {
|
||||
|
||||
public static final String TAG = "C++";
|
||||
|
||||
@Nonnull
|
||||
public static String subTag(@Nonnull String subTag) {
|
||||
return sub(TAG, subTag);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static String sub(@Nonnull String tag, @Nonnull String subTag) {
|
||||
return tag + "/" + subTag;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private static Languages languages;
|
||||
@Nonnull
|
||||
|
@@ -28,10 +28,7 @@ import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import com.squareup.otto.Bus;
|
||||
import com.squareup.otto.Subscribe;
|
||||
import jscl.JsclArithmeticException;
|
||||
import jscl.JsclMathEngine;
|
||||
import jscl.NumeralBase;
|
||||
import jscl.NumeralBaseException;
|
||||
import jscl.*;
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.function.Constants;
|
||||
import jscl.math.function.IConstant;
|
||||
@@ -65,16 +62,12 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis
|
||||
|
||||
public static final long NO_SEQUENCE = -1;
|
||||
|
||||
@Nonnull
|
||||
private final CalculatorEventContainer calculatorEventContainer = new ListCalculatorEventContainer();
|
||||
@Nonnull
|
||||
private static final AtomicLong SEQUENCER = new AtomicLong(NO_SEQUENCE);
|
||||
@Nonnull
|
||||
private final SharedPreferences preferences;
|
||||
@Nonnull
|
||||
private final Bus bus;
|
||||
@Nonnull
|
||||
private final Executor ui;
|
||||
final Bus bus;
|
||||
@Nonnull
|
||||
private final Executor background;
|
||||
|
||||
@@ -85,15 +78,14 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis
|
||||
@Inject
|
||||
Editor editor;
|
||||
@Inject
|
||||
JsclMathEngine mathEngine;
|
||||
Engine engine;
|
||||
@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) {
|
||||
public Calculator(@Nonnull SharedPreferences preferences, @Nonnull Bus bus, @Named(AppModule.THREAD_BACKGROUND) @Nonnull Executor background) {
|
||||
this.preferences = preferences;
|
||||
this.bus = bus;
|
||||
this.ui = ui;
|
||||
this.background = background;
|
||||
bus.register(this);
|
||||
preferences.registerOnSharedPreferenceChangeListener(this);
|
||||
@@ -109,12 +101,6 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis
|
||||
return to.toString(value);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private CalculatorEventData nextEventData() {
|
||||
final long eventId = nextSequence();
|
||||
return CalculatorEventDataImpl.newInstance(eventId, eventId);
|
||||
}
|
||||
|
||||
public void evaluate() {
|
||||
final EditorState state = editor.getState();
|
||||
evaluate(JsclOperation.numeric, state.getTextString());
|
||||
@@ -143,7 +129,7 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis
|
||||
}
|
||||
|
||||
public void init(@Nonnull Executor init) {
|
||||
Locator.getInstance().getEngine().init(init);
|
||||
engine.init(init);
|
||||
setCalculateOnFly(Preferences.Calculations.calculateOnFly.getPreference(preferences));
|
||||
}
|
||||
|
||||
@@ -180,6 +166,7 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis
|
||||
pe = prepare(e);
|
||||
|
||||
try {
|
||||
final MathEngine mathEngine = engine.getMathEngine();
|
||||
mathEngine.setMessageRegistry(mr);
|
||||
|
||||
final Generic result = o.evaluateGeneric(pe.value, mathEngine);
|
||||
@@ -188,7 +175,7 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
result.toString();
|
||||
|
||||
final String stringResult = o.getFromProcessor().process(result);
|
||||
final String stringResult = o.getFromProcessor(engine).process(result);
|
||||
bus.post(new CalculationFinishedEvent(o, e, sequence, result, stringResult, collectMessages(mr)));
|
||||
|
||||
} catch (JsclArithmeticException exception) {
|
||||
@@ -252,7 +239,7 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis
|
||||
public void convert(@Nonnull final DisplayState state, @Nonnull final NumeralBase to) {
|
||||
final Generic value = state.getResult();
|
||||
Check.isNotNull(value);
|
||||
final NumeralBase from = mathEngine.getNumeralBase();
|
||||
final NumeralBase from = engine.getMathEngine().getNumeralBase();
|
||||
if (from == to) {
|
||||
return;
|
||||
}
|
||||
@@ -282,24 +269,6 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis
|
||||
}
|
||||
}
|
||||
|
||||
public void fireCalculatorEvent(@Nonnull final CalculatorEventData calculatorEventData, @Nonnull final CalculatorEventType calculatorEventType, @Nullable final Object data) {
|
||||
ui.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
calculatorEventContainer.fireCalculatorEvent(calculatorEventData, calculatorEventType, data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public CalculatorEventData fireCalculatorEvent(@Nonnull final CalculatorEventType calculatorEventType, @Nullable final Object data) {
|
||||
final CalculatorEventData eventData = nextEventData();
|
||||
|
||||
fireCalculatorEvent(eventData, calculatorEventType, data);
|
||||
|
||||
return eventData;
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onEditorChanged(@Nonnull Editor.ChangedEvent e) {
|
||||
if (!calculateOnFly) {
|
||||
@@ -324,8 +293,8 @@ public class Calculator implements SharedPreferences.OnSharedPreferenceChangeLis
|
||||
updateAnsVariable(text);
|
||||
}
|
||||
|
||||
private void updateAnsVariable(@NonNull String value) {
|
||||
final VariablesRegistry variablesRegistry = Locator.getInstance().getEngine().getVariablesRegistry();
|
||||
void updateAnsVariable(@NonNull String value) {
|
||||
final VariablesRegistry variablesRegistry = engine.getVariablesRegistry();
|
||||
final IConstant variable = variablesRegistry.get(Constants.ANS);
|
||||
|
||||
final CppVariable.Builder b = variable != null ? CppVariable.builder(variable) : CppVariable.builder(Constants.ANS);
|
||||
|
@@ -137,8 +137,6 @@ public class CalculatorApplication extends android.app.Application implements Sh
|
||||
languages.updateContextLocale(this, true);
|
||||
App.getGa().reportInitially(preferences);
|
||||
|
||||
Locator.getInstance().init(engine);
|
||||
|
||||
calculator.init(initThread);
|
||||
|
||||
initThread.execute(new Runnable() {
|
||||
|
@@ -1,79 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 serso aka se.solovyev
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
* Contact details
|
||||
*
|
||||
* Email: se.solovyev@gmail.com
|
||||
* Site: http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 16:39
|
||||
*/
|
||||
public interface CalculatorEventContainer {
|
||||
|
||||
void addCalculatorEventListener(@Nonnull CalculatorEventListener calculatorEventListener);
|
||||
|
||||
void removeCalculatorEventListener(@Nonnull CalculatorEventListener calculatorEventListener);
|
||||
|
||||
void fireCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData, @Nonnull CalculatorEventType calculatorEventType, @Nullable Object data);
|
||||
|
||||
void fireCalculatorEvents(@Nonnull List<CalculatorEvent> calculatorEvents);
|
||||
|
||||
class CalculatorEvent {
|
||||
|
||||
@Nonnull
|
||||
private CalculatorEventData calculatorEventData;
|
||||
|
||||
@Nonnull
|
||||
private CalculatorEventType calculatorEventType;
|
||||
|
||||
@Nullable
|
||||
private Object data;
|
||||
|
||||
public CalculatorEvent(@Nonnull CalculatorEventData calculatorEventData,
|
||||
@Nonnull CalculatorEventType calculatorEventType,
|
||||
@Nullable Object data) {
|
||||
this.calculatorEventData = calculatorEventData;
|
||||
this.calculatorEventType = calculatorEventType;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public CalculatorEventData getCalculatorEventData() {
|
||||
return calculatorEventData;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public CalculatorEventType getCalculatorEventType() {
|
||||
return calculatorEventType;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Object getData() {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 serso aka se.solovyev
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
* Contact details
|
||||
*
|
||||
* Email: se.solovyev@gmail.com
|
||||
* Site: http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import java.util.EventListener;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 16:39
|
||||
*/
|
||||
public interface CalculatorEventListener extends EventListener {
|
||||
|
||||
void onCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData, @Nonnull CalculatorEventType calculatorEventType, @Nullable Object data);
|
||||
|
||||
}
|
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 serso aka se.solovyev
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
* Contact details
|
||||
*
|
||||
* Email: se.solovyev@gmail.com
|
||||
* Site: http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
public enum CalculatorEventType {
|
||||
|
||||
;
|
||||
|
||||
}
|
@@ -1,34 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 serso aka se.solovyev
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
* Contact details
|
||||
*
|
||||
* Email: se.solovyev@gmail.com
|
||||
* Site: http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public interface CalculatorLocator {
|
||||
|
||||
void init(@Nonnull Engine engine);
|
||||
|
||||
@Nonnull
|
||||
Engine getEngine();
|
||||
|
||||
}
|
@@ -1,36 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 serso aka se.solovyev
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
* Contact details
|
||||
*
|
||||
* Email: se.solovyev@gmail.com
|
||||
* Site: http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/22/12
|
||||
* Time: 7:13 PM
|
||||
*/
|
||||
public final class CalculatorUtils {
|
||||
|
||||
private CalculatorUtils() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
}
|
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 serso aka se.solovyev
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
* Contact details
|
||||
*
|
||||
* Email: se.solovyev@gmail.com
|
||||
* Site: http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/1/12
|
||||
* Time: 11:16 PM
|
||||
*/
|
||||
public interface Change<T> {
|
||||
|
||||
@Nonnull
|
||||
T getOldValue();
|
||||
|
||||
@Nonnull
|
||||
T getNewValue();
|
||||
|
||||
}
|
@@ -1,64 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 serso aka se.solovyev
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
* Contact details
|
||||
*
|
||||
* Email: se.solovyev@gmail.com
|
||||
* Site: http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/1/12
|
||||
* Time: 11:18 PM
|
||||
*/
|
||||
public class ChangeImpl<T> implements Change<T> {
|
||||
|
||||
@Nonnull
|
||||
private T oldValue;
|
||||
|
||||
@Nonnull
|
||||
private T newValue;
|
||||
|
||||
private ChangeImpl() {
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static <T> Change<T> newInstance(@Nonnull T oldValue, @Nonnull T newValue) {
|
||||
final ChangeImpl<T> result = new ChangeImpl<T>();
|
||||
|
||||
result.oldValue = oldValue;
|
||||
result.newValue = newValue;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public T getOldValue() {
|
||||
return this.oldValue;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public T getNewValue() {
|
||||
return this.newValue;
|
||||
}
|
||||
}
|
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 serso aka se.solovyev
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
* Contact details
|
||||
*
|
||||
* Email: se.solovyev@gmail.com
|
||||
* Site: http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.solovyev.common.JPredicate;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/3/11
|
||||
* Time: 12:54 AM
|
||||
*/
|
||||
public class CharacterAtPositionFinder implements JPredicate<Character> {
|
||||
|
||||
@Nonnull
|
||||
private final String targetString;
|
||||
private int i;
|
||||
|
||||
public CharacterAtPositionFinder(@Nonnull String targetString, int i) {
|
||||
this.targetString = targetString;
|
||||
this.i = i;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean apply(@Nullable Character s) {
|
||||
return s != null && s.equals(targetString.charAt(i));
|
||||
}
|
||||
|
||||
public void setI(int i) {
|
||||
this.i = i;
|
||||
}
|
||||
}
|
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 serso aka se.solovyev
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
* Contact details
|
||||
*
|
||||
* Email: se.solovyev@gmail.com
|
||||
* Site: http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.graphics.PointF;
|
||||
import android.view.MotionEvent;
|
||||
import org.solovyev.android.views.dragbutton.DirectionDragButton;
|
||||
import org.solovyev.android.views.dragbutton.DragButton;
|
||||
import org.solovyev.android.views.dragbutton.DragDirection;
|
||||
import org.solovyev.android.views.dragbutton.SimpleDragListener;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class DigitButtonDragProcessor implements SimpleDragListener.DragProcessor {
|
||||
|
||||
@Nonnull
|
||||
private Keyboard keyboard;
|
||||
|
||||
public DigitButtonDragProcessor(@Nonnull Keyboard keyboard) {
|
||||
this.keyboard = keyboard;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean processDragEvent(@Nonnull DragDirection direction, @Nonnull DragButton button, @Nonnull PointF startPoint, @Nonnull MotionEvent motionEvent) {
|
||||
final String text = ((DirectionDragButton) button).getText(direction);
|
||||
return keyboard.buttonPressed(text);
|
||||
}
|
||||
}
|
@@ -92,6 +92,8 @@ public class DisplayFragment extends BaseFragment implements View.OnClickListene
|
||||
Plotter plotter;
|
||||
@Inject
|
||||
Calculator calculator;
|
||||
@Inject
|
||||
Engine engine;
|
||||
|
||||
public DisplayFragment() {
|
||||
super(R.layout.cpp_app_display);
|
||||
@@ -149,8 +151,7 @@ public class DisplayFragment extends BaseFragment implements View.OnClickListene
|
||||
|
||||
protected boolean isMenuItemVisible(@NonNull ConversionMenuItem menuItem,
|
||||
@Nonnull Generic generic) {
|
||||
final NumeralBase fromNumeralBase =
|
||||
Locator.getInstance().getEngine().getMathEngine().getNumeralBase();
|
||||
final NumeralBase fromNumeralBase = engine.getMathEngine().getNumeralBase();
|
||||
if (fromNumeralBase != menuItem.toNumeralBase) {
|
||||
return calculator.canConvert(generic, fromNumeralBase, menuItem.toNumeralBase);
|
||||
}
|
||||
|
@@ -30,8 +30,6 @@ public class FloatingCalculatorKeyboard extends BaseFloatingKeyboard {
|
||||
private final List<String> parameterNames;
|
||||
@NonNull
|
||||
private final SimpleDragListener dragListener;
|
||||
@NonNull
|
||||
private final String multiplicationSign = Locator.getInstance().getEngine().getMultiplicationSign();
|
||||
|
||||
public FloatingCalculatorKeyboard(@NonNull User user, @NonNull List<String> parameterNames) {
|
||||
super(user);
|
||||
@@ -76,7 +74,7 @@ public class FloatingCalculatorKeyboard extends BaseFloatingKeyboard {
|
||||
addButton(row, 0, "7");
|
||||
addButton(row, 0, "8");
|
||||
addButton(row, 0, "9").setText("π", up).setText("e", down);
|
||||
addOperationButton(row, R.id.cpp_kb_button_multiply, Locator.getInstance().getEngine().getMultiplicationSign()).setText("^n", up).setText("^2", down);
|
||||
addOperationButton(row, R.id.cpp_kb_button_multiply, "×").setText("^n", up).setText("^2", down);
|
||||
addOperationButton(row, R.id.cpp_kb_button_plus, "+");
|
||||
addButton(row, R.id.cpp_kb_button_clear, "C");
|
||||
|
||||
@@ -107,7 +105,7 @@ public class FloatingCalculatorKeyboard extends BaseFloatingKeyboard {
|
||||
addButton(row, 0, "7");
|
||||
addButton(row, 0, "8");
|
||||
addButton(row, 0, "9").setText("π", up).setText("e", down);
|
||||
addOperationButton(row, R.id.cpp_kb_button_multiply, multiplicationSign).setText("^n", up).setText("^2", down);
|
||||
addOperationButton(row, R.id.cpp_kb_button_multiply, "×").setText("^n", up).setText("^2", down);
|
||||
addButton(row, R.id.cpp_kb_button_clear, "C");
|
||||
|
||||
row = makeRow();
|
||||
@@ -186,7 +184,7 @@ public class FloatingCalculatorKeyboard extends BaseFloatingKeyboard {
|
||||
user.insertOperator('-');
|
||||
break;
|
||||
case R.id.cpp_kb_button_multiply:
|
||||
user.insertOperator(multiplicationSign);
|
||||
user.insertOperator("×");
|
||||
break;
|
||||
case R.id.cpp_kb_button_functions_constants:
|
||||
user.showFunctionsConstants(v);
|
||||
|
@@ -49,6 +49,8 @@ public class Keyboard implements SharedPreferences.OnSharedPreferenceChangeListe
|
||||
@Inject
|
||||
Calculator calculator;
|
||||
@Inject
|
||||
Engine engine;
|
||||
@Inject
|
||||
Lazy<Clipboard> clipboard;
|
||||
@Inject
|
||||
Lazy<Bus> bus;
|
||||
@@ -81,7 +83,7 @@ public class Keyboard implements SharedPreferences.OnSharedPreferenceChangeListe
|
||||
int cursorPositionOffset = 0;
|
||||
final StringBuilder textToBeInserted = new StringBuilder(text);
|
||||
|
||||
MathType.getType(text, 0, false, mathType);
|
||||
MathType.getType(text, 0, false, mathType, engine);
|
||||
switch (mathType.type) {
|
||||
case function:
|
||||
textToBeInserted.append("()");
|
||||
|
@@ -1,83 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 serso aka se.solovyev
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
* Contact details
|
||||
*
|
||||
* Email: se.solovyev@gmail.com
|
||||
* Site: http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import org.solovyev.common.listeners.JListeners;
|
||||
import org.solovyev.common.listeners.Listeners;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 16:42
|
||||
*/
|
||||
public class ListCalculatorEventContainer implements CalculatorEventContainer {
|
||||
|
||||
@Nonnull
|
||||
private static final String TAG = "CalculatorEventData";
|
||||
|
||||
@Nonnull
|
||||
private final JListeners<CalculatorEventListener> listeners = Listeners.newWeakRefListeners();
|
||||
|
||||
@Override
|
||||
public void addCalculatorEventListener(@Nonnull CalculatorEventListener calculatorEventListener) {
|
||||
listeners.addListener(calculatorEventListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeCalculatorEventListener(@Nonnull CalculatorEventListener calculatorEventListener) {
|
||||
listeners.removeListener(calculatorEventListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fireCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData, @Nonnull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||
fireCalculatorEvents(Arrays.asList(new CalculatorEvent(calculatorEventData, calculatorEventType, data)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fireCalculatorEvents(@Nonnull List<CalculatorEvent> calculatorEvents) {
|
||||
final Collection<CalculatorEventListener> listeners = this.listeners.getListeners();
|
||||
|
||||
//final CalculatorLogger logger = Locator.getInstance().getLogger();
|
||||
|
||||
for (CalculatorEvent e : calculatorEvents) {
|
||||
//Locator.getInstance().getLogger().debug(TAG, "Event fired: " + e.getCalculatorEventType());
|
||||
for (CalculatorEventListener listener : listeners) {
|
||||
/*long startTime = System.currentTimeMillis();*/
|
||||
listener.onCalculatorEvent(e.getCalculatorEventData(), e.getCalculatorEventType(), e.getData());
|
||||
/* long endTime = System.currentTimeMillis();
|
||||
long totalTime = (endTime - startTime);
|
||||
if ( totalTime > 300 ) {
|
||||
logger.debug(TAG + "_" + e.getCalculatorEventData().getEventId(), "Started event: " + e.getCalculatorEventType() + " with data: " + e.getData() + " for: " + listener.getClass().getSimpleName());
|
||||
logger.debug(TAG + "_" + e.getCalculatorEventData().getEventId(), "Total time, ms: " + totalTime);
|
||||
}*/
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 serso aka se.solovyev
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
* Contact details
|
||||
*
|
||||
* Email: se.solovyev@gmail.com
|
||||
* Site: http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class Locator implements CalculatorLocator {
|
||||
|
||||
@Nonnull
|
||||
private static final Locator instance = new Locator();
|
||||
@Nonnull
|
||||
private Engine engine;
|
||||
|
||||
public Locator() {
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static CalculatorLocator getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(@Nonnull Engine engine) {
|
||||
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Engine getEngine() {
|
||||
return engine;
|
||||
}
|
||||
|
||||
}
|
@@ -23,15 +23,6 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.text.SpannableStringBuilder;
|
||||
|
||||
import org.solovyev.android.calculator.math.MathType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import jscl.MathContext;
|
||||
import jscl.MathEngine;
|
||||
import jscl.NumeralBase;
|
||||
@@ -40,6 +31,12 @@ import jscl.text.DoubleParser;
|
||||
import jscl.text.JsclIntegerParser;
|
||||
import jscl.text.ParseException;
|
||||
import jscl.text.Parser;
|
||||
import org.solovyev.android.calculator.math.MathType;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class NumberBuilder extends BaseNumberBuilder {
|
||||
|
||||
@@ -178,7 +175,7 @@ public class NumberBuilder extends BaseNumberBuilder {
|
||||
|
||||
// let's get rid of unnecessary characters (grouping separators, + after E)
|
||||
final List<String> tokens = new ArrayList<String>();
|
||||
tokens.addAll(MathType.grouping_separator.getTokens());
|
||||
tokens.addAll(MathType.grouping_separator.getTokens(engine));
|
||||
// + after E can be omitted: 10+E = 10E (NOTE: - cannot be omitted )
|
||||
tokens.add("+");
|
||||
for (String groupingSeparator : tokens) {
|
||||
|
@@ -22,11 +22,6 @@
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import static org.solovyev.android.Android.isPhoneModel;
|
||||
import static org.solovyev.android.DeviceModel.samsung_galaxy_s;
|
||||
import static org.solovyev.android.DeviceModel.samsung_galaxy_s_2;
|
||||
import static org.solovyev.android.prefs.IntegerPreference.DEF_VALUE;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
@@ -39,7 +34,8 @@ import android.support.annotation.LayoutRes;
|
||||
import android.support.annotation.StyleRes;
|
||||
import android.util.SparseArray;
|
||||
import android.view.ContextThemeWrapper;
|
||||
|
||||
import jscl.AngleUnit;
|
||||
import jscl.NumeralBase;
|
||||
import org.solovyev.android.Check;
|
||||
import org.solovyev.android.calculator.about.AboutActivity;
|
||||
import org.solovyev.android.calculator.functions.FunctionsActivity;
|
||||
@@ -50,22 +46,19 @@ import org.solovyev.android.calculator.operators.OperatorsActivity;
|
||||
import org.solovyev.android.calculator.preferences.PreferencesActivity;
|
||||
import org.solovyev.android.calculator.variables.VariablesActivity;
|
||||
import org.solovyev.android.calculator.wizard.WizardActivity;
|
||||
import org.solovyev.android.prefs.BooleanPreference;
|
||||
import org.solovyev.android.prefs.IntegerPreference;
|
||||
import org.solovyev.android.prefs.NumberToStringPreference;
|
||||
import org.solovyev.android.prefs.Preference;
|
||||
import org.solovyev.android.prefs.StringPreference;
|
||||
|
||||
import jscl.AngleUnit;
|
||||
import jscl.NumeralBase;
|
||||
import org.solovyev.android.prefs.*;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.text.DecimalFormatSymbols;
|
||||
import java.util.EnumMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import static org.solovyev.android.Android.isPhoneModel;
|
||||
import static org.solovyev.android.DeviceModel.samsung_galaxy_s;
|
||||
import static org.solovyev.android.DeviceModel.samsung_galaxy_s_2;
|
||||
import static org.solovyev.android.prefs.IntegerPreference.DEF_VALUE;
|
||||
|
||||
public final class Preferences {
|
||||
|
||||
|
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 serso aka se.solovyev
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
* Contact details
|
||||
*
|
||||
* Email: se.solovyev@gmail.com
|
||||
* Site: http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class SystemErrorReporter implements ErrorReporter {
|
||||
|
||||
@Override
|
||||
public void onException(@Nonnull Throwable e) {
|
||||
e.printStackTrace(System.out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(@Nullable String message) {
|
||||
System.out.println(message);
|
||||
}
|
||||
}
|
@@ -40,30 +40,33 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
|
||||
|
||||
@Nonnull
|
||||
private static final Integer MAX_DEPTH = 20;
|
||||
|
||||
@Inject
|
||||
Engine engine;
|
||||
|
||||
@Inject
|
||||
public ToJsclTextProcessor() {
|
||||
}
|
||||
|
||||
private static PreparedExpression processWithDepth(@Nonnull String s, int depth, @Nonnull List<IConstant> undefinedVars) throws ParseException {
|
||||
return replaceVariables(processExpression(s).toString(), depth, undefinedVars);
|
||||
private static PreparedExpression processWithDepth(@Nonnull String s, int depth, @Nonnull List<IConstant> undefinedVars, @Nonnull Engine engine) throws ParseException {
|
||||
return replaceVariables(processExpression(s, engine).toString(), depth, undefinedVars, engine);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private static StringBuilder processExpression(@Nonnull String s) throws ParseException {
|
||||
private static StringBuilder processExpression(@Nonnull String s, @Nonnull Engine engine) throws ParseException {
|
||||
final StringBuilder result = new StringBuilder();
|
||||
final MathType.Results results = new MathType.Results();
|
||||
|
||||
MathType.Result mathTypeResult = null;
|
||||
MathType.Result mathTypeBefore = null;
|
||||
|
||||
final LiteNumberBuilder nb = new LiteNumberBuilder(Locator.getInstance().getEngine());
|
||||
final LiteNumberBuilder nb = new LiteNumberBuilder(engine);
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
if (s.charAt(i) == ' ') continue;
|
||||
|
||||
results.release(mathTypeBefore);
|
||||
mathTypeBefore = mathTypeResult == null ? null : mathTypeResult;
|
||||
mathTypeResult = MathType.getType(s, i, nb.isHexMode(), results.obtain());
|
||||
mathTypeResult = MathType.getType(s, i, nb.isHexMode(), engine);
|
||||
|
||||
nb.process(mathTypeResult);
|
||||
|
||||
@@ -80,7 +83,7 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
|
||||
(mathTypeBefore.type == MathType.function || mathTypeBefore.type == MathType.operator) &&
|
||||
App.find(MathType.groupSymbols, s, i) != null) {
|
||||
final String functionName = mathTypeBefore.match;
|
||||
final Function function = Locator.getInstance().getEngine().getFunctionsRegistry().get(functionName);
|
||||
final Function function = engine.getFunctionsRegistry().get(functionName);
|
||||
if (function == null || function.getMinParameters() > 0) {
|
||||
throw new ParseException(i, s, new CalculatorMessage(CalculatorMessages.msg_005, MessageType.error, mathTypeBefore.match));
|
||||
}
|
||||
@@ -92,7 +95,7 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private static PreparedExpression replaceVariables(@Nonnull final String s, int depth, @Nonnull List<IConstant> undefinedVars) throws ParseException {
|
||||
private static PreparedExpression replaceVariables(@Nonnull final String s, int depth, @Nonnull List<IConstant> undefinedVars, @Nonnull Engine engine) throws ParseException {
|
||||
if (depth >= MAX_DEPTH) {
|
||||
throw new ParseException(s, new CalculatorMessage(CalculatorMessages.msg_006, MessageType.error));
|
||||
} else {
|
||||
@@ -106,13 +109,13 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
|
||||
startsWithFinder.setI(i);
|
||||
|
||||
int offset = 0;
|
||||
String functionName = App.find(MathType.function.getTokens(), startsWithFinder);
|
||||
String functionName = App.find(MathType.function.getTokens(engine), startsWithFinder);
|
||||
if (functionName == null) {
|
||||
String operatorName = App.find(MathType.operator.getTokens(), startsWithFinder);
|
||||
String operatorName = App.find(MathType.operator.getTokens(engine), startsWithFinder);
|
||||
if (operatorName == null) {
|
||||
String varName = App.find(Locator.getInstance().getEngine().getVariablesRegistry().getNames(), startsWithFinder);
|
||||
String varName = App.find(engine.getVariablesRegistry().getNames(), startsWithFinder);
|
||||
if (varName != null) {
|
||||
final IConstant var = Locator.getInstance().getEngine().getVariablesRegistry().get(varName);
|
||||
final IConstant var = engine.getVariablesRegistry().get(varName);
|
||||
if (var != null) {
|
||||
if (!var.isDefined()) {
|
||||
undefinedVars.add(var);
|
||||
@@ -127,7 +130,7 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
|
||||
// NOTE: append varName as JSCL engine will convert it to double if needed
|
||||
result.append(varName);
|
||||
} else {
|
||||
result.append("(").append(processWithDepth(value, depth, undefinedVars)).append(")");
|
||||
result.append("(").append(processWithDepth(value, depth, undefinedVars, engine)).append(")");
|
||||
}
|
||||
offset = varName.length();
|
||||
}
|
||||
@@ -156,6 +159,6 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
|
||||
@Override
|
||||
@Nonnull
|
||||
public PreparedExpression process(@Nonnull String s) throws ParseException {
|
||||
return processWithDepth(s, 0, new ArrayList<IConstant>());
|
||||
return processWithDepth(s, 0, new ArrayList<IConstant>(), engine);
|
||||
}
|
||||
}
|
||||
|
@@ -23,9 +23,11 @@
|
||||
package org.solovyev.android.calculator.jscl;
|
||||
|
||||
|
||||
import android.support.annotation.Nullable;
|
||||
import jscl.MathEngine;
|
||||
import jscl.math.Generic;
|
||||
import jscl.text.ParseException;
|
||||
import org.solovyev.android.calculator.Engine;
|
||||
import org.solovyev.android.calculator.text.DummyTextProcessor;
|
||||
import org.solovyev.android.calculator.text.FromJsclSimplifyTextProcessor;
|
||||
import org.solovyev.android.calculator.text.TextProcessor;
|
||||
@@ -34,26 +36,44 @@ import javax.annotation.Nonnull;
|
||||
|
||||
public enum JsclOperation {
|
||||
|
||||
simplify,
|
||||
elementary,
|
||||
numeric;
|
||||
simplify {
|
||||
@Nonnull
|
||||
@Override
|
||||
TextProcessor<String, Generic> makeFromProcessor(@Nonnull Engine engine) {
|
||||
return new FromJsclSimplifyTextProcessor(engine);
|
||||
}
|
||||
},
|
||||
elementary {
|
||||
@Nonnull
|
||||
@Override
|
||||
TextProcessor<String, Generic> makeFromProcessor(@Nonnull Engine engine) {
|
||||
return DummyTextProcessor.instance;
|
||||
}
|
||||
},
|
||||
numeric {
|
||||
@Nonnull
|
||||
@Override
|
||||
TextProcessor<String, Generic> makeFromProcessor(@Nonnull Engine engine) {
|
||||
return FromJsclNumericTextProcessor.instance;
|
||||
}
|
||||
};
|
||||
|
||||
@Nullable
|
||||
TextProcessor<String, Generic> fromProcessor;
|
||||
|
||||
JsclOperation() {
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
abstract TextProcessor<String, Generic> makeFromProcessor(@Nonnull Engine engine);
|
||||
|
||||
|
||||
@Nonnull
|
||||
public TextProcessor<String, Generic> getFromProcessor() {
|
||||
switch (this) {
|
||||
case simplify:
|
||||
return FromJsclSimplifyTextProcessor.instance;
|
||||
case elementary:
|
||||
return DummyTextProcessor.instance;
|
||||
case numeric:
|
||||
return FromJsclNumericTextProcessor.instance;
|
||||
default:
|
||||
throw new UnsupportedOperationException();
|
||||
public TextProcessor<String, Generic> getFromProcessor(@Nonnull Engine engine) {
|
||||
if (fromProcessor == null) {
|
||||
fromProcessor = makeFromProcessor(engine);
|
||||
}
|
||||
return fromProcessor;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
|
@@ -22,11 +22,13 @@
|
||||
|
||||
package org.solovyev.android.calculator.math;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import jscl.JsclMathEngine;
|
||||
import jscl.NumeralBase;
|
||||
import jscl.math.function.Constants;
|
||||
import org.solovyev.android.Check;
|
||||
import org.solovyev.android.calculator.App;
|
||||
import org.solovyev.android.calculator.Locator;
|
||||
import org.solovyev.android.calculator.Engine;
|
||||
import org.solovyev.android.calculator.ParseException;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
@@ -39,20 +41,12 @@ import java.util.List;
|
||||
|
||||
public enum MathType {
|
||||
|
||||
numeral_base(50, true, false, MathGroupType.number) {
|
||||
|
||||
private final List<String> tokens = new ArrayList<>(10);
|
||||
numeral_base(50, true, false, MathGroupType.number, new ArrayList<String>()) {
|
||||
{
|
||||
for (NumeralBase numeralBase : NumeralBase.values()) {
|
||||
tokens.add(numeralBase.getJsclPrefix());
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public List<String> getTokens() {
|
||||
return tokens;
|
||||
}
|
||||
},
|
||||
|
||||
dot(200, true, true, MathGroupType.number, ".") {
|
||||
@@ -74,8 +68,8 @@ public enum MathType {
|
||||
postfix_function(400, false, true, MathGroupType.function) {
|
||||
@Nonnull
|
||||
@Override
|
||||
public List<String> getTokens() {
|
||||
return Locator.getInstance().getEngine().getPostfixFunctionsRegistry().getNames();
|
||||
public List<String> getTokens(@NonNull Engine engine) {
|
||||
return engine.getPostfixFunctionsRegistry().getNames();
|
||||
}
|
||||
},
|
||||
|
||||
@@ -151,26 +145,47 @@ public enum MathType {
|
||||
},
|
||||
|
||||
function(1000, true, true, MathGroupType.function) {
|
||||
@Nonnull
|
||||
@Override
|
||||
public List<String> getTokens(@NonNull Engine engine) {
|
||||
return engine.getFunctionsRegistry().getNames();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public List<String> getTokens() {
|
||||
return Locator.getInstance().getEngine().getFunctionsRegistry().getNames();
|
||||
Check.shouldNotHappen();
|
||||
return super.getTokens();
|
||||
}
|
||||
},
|
||||
|
||||
operator(1050, true, true, MathGroupType.function) {
|
||||
@Nonnull
|
||||
@Override
|
||||
public List<String> getTokens(@NonNull Engine engine) {
|
||||
return engine.getOperatorsRegistry().getNames();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public List<String> getTokens() {
|
||||
return Locator.getInstance().getEngine().getOperatorsRegistry().getNames();
|
||||
Check.shouldNotHappen();
|
||||
return super.getTokens();
|
||||
}
|
||||
},
|
||||
|
||||
constant(1100, true, true, MathGroupType.other) {
|
||||
@Nonnull
|
||||
@Override
|
||||
public List<String> getTokens(@NonNull Engine engine) {
|
||||
return engine.getVariablesRegistry().getNames();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public List<String> getTokens() {
|
||||
return Locator.getInstance().getEngine().getVariablesRegistry().getNames();
|
||||
Check.shouldNotHappen();
|
||||
return super.getTokens();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -179,9 +194,7 @@ public enum MathType {
|
||||
}
|
||||
},
|
||||
|
||||
digit(1125, true, true, MathGroupType.number) {
|
||||
|
||||
private final List<String> tokens = new ArrayList<>(16);
|
||||
digit(1125, true, true, MathGroupType.number, new ArrayList<String>()) {
|
||||
{
|
||||
for (Character character : NumeralBase.hex.getAcceptableCharacters()) {
|
||||
tokens.add(character.toString());
|
||||
@@ -192,12 +205,6 @@ public enum MathType {
|
||||
public boolean isNeedMultiplicationSignBefore(@Nonnull MathType mathTypeBefore) {
|
||||
return super.isNeedMultiplicationSignBefore(mathTypeBefore) && mathTypeBefore != digit && mathTypeBefore != dot /*&& mathTypeBefore != numeral_base*/;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public List<String> getTokens() {
|
||||
return tokens;
|
||||
}
|
||||
},
|
||||
|
||||
comma(1150, false, false, MathGroupType.other, ","),
|
||||
@@ -222,7 +229,7 @@ public enum MathType {
|
||||
public final static String INFINITY_JSCL = "Infinity";
|
||||
private static List<MathType> mathTypesByPriority;
|
||||
@Nonnull
|
||||
private final List<String> tokens;
|
||||
protected final List<String> tokens;
|
||||
@Nonnull
|
||||
private final Integer priority;
|
||||
private final boolean needMultiplicationSignBefore;
|
||||
@@ -246,7 +253,7 @@ public enum MathType {
|
||||
this.needMultiplicationSignBefore = needMultiplicationSignBefore;
|
||||
this.needMultiplicationSignAfter = needMultiplicationSignAfter;
|
||||
this.groupType = groupType;
|
||||
this.tokens = java.util.Collections.unmodifiableList(tokens);
|
||||
this.tokens = tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -255,15 +262,16 @@ public enum MathType {
|
||||
* @param text analyzed text
|
||||
* @param i index which points to start of substring
|
||||
* @param hexMode true if current mode if HEX
|
||||
* @param engine math engine
|
||||
* @return math entity type of substring starting from ith index of specified text
|
||||
*/
|
||||
@Nonnull
|
||||
public static Result getType(@Nonnull String text, int i, boolean hexMode) {
|
||||
return getType(text, i, hexMode, new Result());
|
||||
public static Result getType(@Nonnull String text, int i, boolean hexMode, @Nonnull Engine engine) {
|
||||
return getType(text, i, hexMode, new Result(), engine);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static Result getType(@Nonnull String text, int i, boolean hexMode, @Nonnull Result result) {
|
||||
public static Result getType(@Nonnull String text, int i, boolean hexMode, @Nonnull Result result, @Nonnull Engine engine) {
|
||||
if (i < 0) {
|
||||
throw new IllegalArgumentException("I must be more or equals to 0.");
|
||||
} else if (i >= text.length() && i != 0) {
|
||||
@@ -274,7 +282,7 @@ public enum MathType {
|
||||
final List<MathType> mathTypes = getMathTypesByPriority();
|
||||
for (int j = 0; j < mathTypes.size(); j++) {
|
||||
final MathType mathType = mathTypes.get(j);
|
||||
final String s = App.find(mathType.getTokens(), text, i);
|
||||
final String s = App.find(mathType.getTokens(engine), text, i);
|
||||
if (s == null) {
|
||||
continue;
|
||||
}
|
||||
@@ -304,7 +312,7 @@ public enum MathType {
|
||||
}
|
||||
|
||||
if (mathType == MathType.grouping_separator) {
|
||||
if (i + 1 < text.length() && getType(text, i + 1, hexMode, result).type == MathType.digit) {
|
||||
if (i + 1 < text.length() && getType(text, i + 1, hexMode, result, engine).type == MathType.digit) {
|
||||
return result.set(mathType, s);
|
||||
}
|
||||
continue;
|
||||
@@ -339,6 +347,11 @@ public enum MathType {
|
||||
return groupType;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public List<String> getTokens(@Nonnull Engine engine) {
|
||||
return getTokens();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public List<String> getTokens() {
|
||||
return tokens;
|
||||
@@ -400,7 +413,8 @@ public enum MathType {
|
||||
public String match;
|
||||
|
||||
public Result(@Nonnull MathType type, @Nonnull String match) {
|
||||
set(type, match);
|
||||
this.type = type;
|
||||
this.match = match;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@@ -411,6 +425,7 @@ public enum MathType {
|
||||
}
|
||||
|
||||
public Result() {
|
||||
this(MathType.text, "");
|
||||
}
|
||||
|
||||
public int processToJscl(@Nonnull StringBuilder result, int i) throws ParseException {
|
||||
|
@@ -23,7 +23,7 @@
|
||||
package org.solovyev.android.calculator.text;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import org.solovyev.android.calculator.Locator;
|
||||
import org.solovyev.android.calculator.Engine;
|
||||
import org.solovyev.android.calculator.math.MathType;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
@@ -33,10 +33,13 @@ import java.util.List;
|
||||
|
||||
public class FromJsclSimplifyTextProcessor implements TextProcessor<String, Generic> {
|
||||
|
||||
public static final FromJsclSimplifyTextProcessor instance = new FromJsclSimplifyTextProcessor();
|
||||
private final List<MathType> mathTypes = Arrays.asList(MathType.function, MathType.constant);
|
||||
|
||||
public FromJsclSimplifyTextProcessor() {
|
||||
@Nonnull
|
||||
private final Engine engine;
|
||||
|
||||
public FromJsclSimplifyTextProcessor(@Nonnull Engine engine) {
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@@ -63,7 +66,7 @@ public class FromJsclSimplifyTextProcessor implements TextProcessor<String, Gene
|
||||
mathTypeBefore = mathType;
|
||||
|
||||
if (mathTypeAfter == null) {
|
||||
mathType = MathType.getType(s, i, false, results.obtain());
|
||||
mathType = MathType.getType(s, i, false, results.obtain(), engine);
|
||||
} else {
|
||||
mathType = mathTypeAfter;
|
||||
}
|
||||
@@ -71,13 +74,13 @@ public class FromJsclSimplifyTextProcessor implements TextProcessor<String, Gene
|
||||
char ch = s.charAt(i);
|
||||
if (ch == '*') {
|
||||
if (i + 1 < s.length()) {
|
||||
mathTypeAfter = MathType.getType(s, i + 1, false, results.obtain());
|
||||
mathTypeAfter = MathType.getType(s, i + 1, false, results.obtain(), engine);
|
||||
} else {
|
||||
mathTypeAfter = null;
|
||||
}
|
||||
|
||||
if (needMultiplicationSign(mathTypeBefore == null ? null : mathTypeBefore.type, mathTypeAfter == null ? null : mathTypeAfter.type)) {
|
||||
sb.append(Locator.getInstance().getEngine().getMultiplicationSign());
|
||||
sb.append(engine.getMultiplicationSign());
|
||||
}
|
||||
|
||||
} else {
|
||||
|
@@ -99,6 +99,8 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF
|
||||
VariablesRegistry variablesRegistry;
|
||||
@Inject
|
||||
Lazy<ToJsclTextProcessor> toJsclTextProcessor;
|
||||
@Inject
|
||||
Engine engine;
|
||||
@Nullable
|
||||
private CppVariable variable;
|
||||
|
||||
@@ -275,7 +277,7 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF
|
||||
}
|
||||
}
|
||||
|
||||
final MathType.Result type = MathType.getType(name, 0, false);
|
||||
final MathType.Result type = MathType.getType(name, 0, false, engine);
|
||||
if (type.type != MathType.text && type.type != MathType.constant) {
|
||||
setError(nameLabel, getString(R.string.c_var_name_clashes));
|
||||
return false;
|
||||
|
@@ -22,9 +22,12 @@ public final class EditorTextProcessor implements TextProcessor<TextProcessorEdi
|
||||
@Nullable
|
||||
private TextHighlighter textHighlighter;
|
||||
@Nonnull
|
||||
private final SharedPreferences preferences;
|
||||
@Nonnull
|
||||
private final Engine engine;
|
||||
|
||||
public EditorTextProcessor(@Nonnull SharedPreferences preferences, @Nonnull Engine engine) {
|
||||
this.preferences = preferences;
|
||||
this.engine = engine;
|
||||
preferences.registerOnSharedPreferenceChangeListener(this);
|
||||
onSharedPreferenceChanged(preferences, colorDisplay.getKey());
|
||||
@@ -43,7 +46,7 @@ public final class EditorTextProcessor implements TextProcessor<TextProcessorEdi
|
||||
@Nonnull
|
||||
private TextHighlighter getTextHighlighter() {
|
||||
if (textHighlighter == null) {
|
||||
onSharedPreferenceChanged(App.getPreferences(), theme.getKey());
|
||||
onSharedPreferenceChanged(preferences, theme.getKey());
|
||||
}
|
||||
return textHighlighter;
|
||||
}
|
||||
|
@@ -89,7 +89,7 @@ public class TextHighlighter implements TextProcessor<TextProcessorEditorResult,
|
||||
int openGroupsCount = 0;
|
||||
|
||||
for (int i = 0; i < text.length(); i++) {
|
||||
MathType.getType(text, i, nb.isHexMode(), result);
|
||||
MathType.getType(text, i, nb.isHexMode(), result, engine);
|
||||
|
||||
offset += nb.process(sb, result);
|
||||
|
||||
|
Reference in New Issue
Block a user