From e3c3abf82893155e5b8ef4ec0d0f971ccb14e3be Mon Sep 17 00:00:00 2001 From: serso Date: Fri, 8 Jan 2016 10:09:24 +0100 Subject: [PATCH] Refactor --- .../android/calculator/AndroidCalculator.java | 6 +- .../AndroidCalculatorDisplayView.java | 6 +- .../solovyev/android/calculator/BaseUi.java | 5 +- .../android/calculator/Calculator.java | 4 +- .../CalculatorActivityLauncher.java | 6 +- .../CalculatorConversionEventData.java | 2 +- .../CalculatorConversionEventDataImpl.java | 6 +- .../android/calculator/CalculatorDisplay.java | 4 +- .../CalculatorDisplayChangeEventData.java | 2 +- .../CalculatorDisplayChangeEventDataImpl.java | 10 +- .../calculator/CalculatorDisplayImpl.java | 33 +- .../calculator/CalculatorDisplayMenuItem.java | 16 +- .../CalculatorDisplayOnClickListener.java | 2 +- .../calculator/CalculatorDisplayView.java | 4 +- .../CalculatorDisplayViewState.java | 59 --- .../android/calculator/CalculatorEditor.java | 117 ------ .../calculator/CalculatorEditorImpl.java | 356 ------------------ .../android/calculator/CalculatorImpl.java | 24 +- .../calculator/CalculatorKeyboardImpl.java | 8 +- .../android/calculator/CalculatorLocator.java | 2 +- .../calculator/CalculatorSpecialButton.java | 2 +- .../calculator/ConversionMenuItem.java | 4 +- ...ayViewStateImpl.java => DisplayState.java} | 62 +-- .../solovyev/android/calculator/Editor.java | 265 +++++++++++++ .../android/calculator/EditorView.java | 22 +- .../solovyev/android/calculator/Locator.java | 8 +- .../function/FunctionEditDialogFragment.java | 4 +- .../history/AndroidCalculatorHistory.java | 20 +- .../history/BaseHistoryFragment.java | 44 +-- ...istoryState.java => BaseHistoryState.java} | 16 +- .../calculator/history/CalculatorHistory.java | 12 +- .../history/CalculatorHistoryImpl.java | 40 +- ...oryState.java => DisplayHistoryState.java} | 23 +- .../history/EditorHistoryState.java | 14 +- .../android/calculator/history/History.java | 6 +- .../history/HistoryArrayAdapter.java | 6 +- .../calculator/history/HistoryFragment.java | 6 +- .../history/HistoryItemMenuData.java | 10 +- .../history/HistoryItemMenuItem.java | 18 +- ...torHistoryState.java => HistoryState.java} | 65 ++-- .../calculator/history/HistoryUtils.java | 8 +- .../history/SavedHistoryFragment.java | 4 +- .../onscreen/CalculatorOnscreenService.java | 2 +- .../onscreen/CalculatorOnscreenView.java | 2 +- .../calculator/view/LongClickEraser.java | 4 +- .../calculator/widget/CalculatorWidget.java | 6 +- ...ewTest.java => AndroidEditorViewTest.java} | 2 +- .../CalculatorDisplayViewStateImplTest.java | 10 +- .../calculator/CalculatorTestUtils.java | 9 +- ...torEditorImplTest.java => EditorTest.java} | 140 +++---- .../history/CalculatorHistoryImplTest.java | 6 +- .../calculator/history/HistoryUtilsTest.java | 33 +- 52 files changed, 604 insertions(+), 941 deletions(-) delete mode 100644 app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayViewState.java delete mode 100644 app/src/main/java/org/solovyev/android/calculator/CalculatorEditor.java delete mode 100644 app/src/main/java/org/solovyev/android/calculator/CalculatorEditorImpl.java rename app/src/main/java/org/solovyev/android/calculator/{CalculatorDisplayViewStateImpl.java => DisplayState.java} (58%) create mode 100644 app/src/main/java/org/solovyev/android/calculator/Editor.java rename app/src/main/java/org/solovyev/android/calculator/history/{AbstractHistoryState.java => BaseHistoryState.java} (87%) rename app/src/main/java/org/solovyev/android/calculator/history/{CalculatorDisplayHistoryState.java => DisplayHistoryState.java} (75%) rename app/src/main/java/org/solovyev/android/calculator/history/{CalculatorHistoryState.java => HistoryState.java} (52%) rename app/src/test/java/org/solovyev/android/calculator/{AndroidCalculatorEditorViewTest.java => AndroidEditorViewTest.java} (98%) rename app/src/test/java/org/solovyev/android/calculator/{CalculatorEditorImplTest.java => EditorTest.java} (52%) diff --git a/app/src/main/java/org/solovyev/android/calculator/AndroidCalculator.java b/app/src/main/java/org/solovyev/android/calculator/AndroidCalculator.java index bb5bee8d..583d0ee1 100644 --- a/app/src/main/java/org/solovyev/android/calculator/AndroidCalculator.java +++ b/app/src/main/java/org/solovyev/android/calculator/AndroidCalculator.java @@ -27,7 +27,7 @@ import android.content.SharedPreferences; import android.preference.PreferenceManager; import jscl.NumeralBase; import jscl.math.Generic; -import org.solovyev.android.calculator.history.CalculatorHistoryState; +import org.solovyev.android.calculator.history.HistoryState; import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.common.history.HistoryAction; import org.solovyev.common.msg.Message; @@ -156,12 +156,12 @@ public class AndroidCalculator implements Calculator, CalculatorEventListener, S @Override @Nonnull - public CalculatorHistoryState getCurrentHistoryState() { + public HistoryState getCurrentHistoryState() { return calculator.getCurrentHistoryState(); } @Override - public void setCurrentHistoryState(@Nonnull CalculatorHistoryState editorHistoryState) { + public void setCurrentHistoryState(@Nonnull HistoryState editorHistoryState) { calculator.setCurrentHistoryState(editorHistoryState); } diff --git a/app/src/main/java/org/solovyev/android/calculator/AndroidCalculatorDisplayView.java b/app/src/main/java/org/solovyev/android/calculator/AndroidCalculatorDisplayView.java index 473634e7..63716c1a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/AndroidCalculatorDisplayView.java +++ b/app/src/main/java/org/solovyev/android/calculator/AndroidCalculatorDisplayView.java @@ -68,7 +68,7 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements @Nonnull private final Handler uiHandler = new Handler(); @Nonnull - private volatile CalculatorDisplayViewState state = CalculatorDisplayViewStateImpl.newDefaultInstance(); + private volatile DisplayState state = DisplayState.empty(); private volatile boolean initialized = false; /* @@ -109,14 +109,14 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements @Nonnull @Override - public CalculatorDisplayViewState getState() { + public DisplayState getState() { synchronized (lock) { return this.state; } } @Override - public void setState(@Nonnull final CalculatorDisplayViewState state) { + public void setState(@Nonnull final DisplayState state) { uiHandler.post(new Runnable() { @Override diff --git a/app/src/main/java/org/solovyev/android/calculator/BaseUi.java b/app/src/main/java/org/solovyev/android/calculator/BaseUi.java index 91c6b42f..a3e929a2 100644 --- a/app/src/main/java/org/solovyev/android/calculator/BaseUi.java +++ b/app/src/main/java/org/solovyev/android/calculator/BaseUi.java @@ -40,13 +40,12 @@ import org.solovyev.android.views.dragbutton.DragButton; import org.solovyev.android.views.dragbutton.DragDirection; import org.solovyev.android.views.dragbutton.DragListener; import org.solovyev.android.views.dragbutton.SimpleDragListener; -import org.solovyev.android.calculator.history.CalculatorHistoryState; +import org.solovyev.android.calculator.history.HistoryState; import org.solovyev.android.calculator.history.HistoryDragProcessor; import org.solovyev.android.calculator.view.AngleUnitsButton; import org.solovyev.android.calculator.view.LongClickEraser; import org.solovyev.android.calculator.view.NumeralBasesButton; import org.solovyev.android.calculator.view.ViewsCache; -import org.solovyev.common.math.Point2d; import java.util.ArrayList; import java.util.List; @@ -166,7 +165,7 @@ public abstract class BaseUi implements SharedPreferences.OnSharedPreferenceChan final ViewsCache views = ViewsCache.forView(root); setOnDragListeners(views, activity); - HistoryDragProcessor historyDragProcessor = new HistoryDragProcessor<>(getCalculator()); + HistoryDragProcessor historyDragProcessor = new HistoryDragProcessor<>(getCalculator()); final DragListener historyDragListener = newDragListener(historyDragProcessor, activity); final DragButton historyButton = getButton(views, R.id.cpp_button_history); if (historyButton != null) { diff --git a/app/src/main/java/org/solovyev/android/calculator/Calculator.java b/app/src/main/java/org/solovyev/android/calculator/Calculator.java index 05e58fe9..d2ea0311 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Calculator.java +++ b/app/src/main/java/org/solovyev/android/calculator/Calculator.java @@ -22,7 +22,7 @@ package org.solovyev.android.calculator; -import org.solovyev.android.calculator.history.CalculatorHistoryState; +import org.solovyev.android.calculator.history.HistoryState; import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.common.history.HistoryControl; @@ -37,7 +37,7 @@ import jscl.math.Generic; * Date: 20.09.12 * Time: 16:38 */ -public interface Calculator extends CalculatorEventContainer, HistoryControl { +public interface Calculator extends CalculatorEventContainer, HistoryControl { void init(); diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java index 877e45b3..5706c510 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java @@ -130,7 +130,7 @@ public final class CalculatorActivityLauncher implements CalculatorEventListener public static void tryCreateVar(@Nonnull final Context context) { final CalculatorDisplay display = Locator.getInstance().getDisplay(); - final CalculatorDisplayViewState viewState = display.getViewState(); + final DisplayState viewState = display.getViewState(); if (viewState.isValid()) { final String varValue = viewState.getText(); if (!Strings.isEmpty(varValue)) { @@ -156,7 +156,7 @@ public final class CalculatorActivityLauncher implements CalculatorEventListener public static void tryCreateFunction(@Nonnull final Context context) { final CalculatorDisplay display = Locator.getInstance().getDisplay(); - final CalculatorDisplayViewState viewState = display.getViewState(); + final DisplayState viewState = display.getViewState(); if (viewState.isValid()) { final String functionValue = viewState.getText(); @@ -180,7 +180,7 @@ public final class CalculatorActivityLauncher implements CalculatorEventListener public static void tryPlot() { final CalculatorPlotter plotter = Locator.getInstance().getPlotter(); final CalculatorDisplay display = Locator.getInstance().getDisplay(); - final CalculatorDisplayViewState viewState = display.getViewState(); + final DisplayState viewState = display.getViewState(); if (viewState.isValid()) { final String functionValue = viewState.getText(); diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorConversionEventData.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorConversionEventData.java index 81c7505c..2a8bd5b3 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorConversionEventData.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorConversionEventData.java @@ -36,7 +36,7 @@ public interface CalculatorConversionEventData extends CalculatorEventData { // display state on the moment of conversion @Nonnull - CalculatorDisplayViewState getDisplayState(); + DisplayState getDisplayState(); @Nonnull NumeralBase getFromNumeralBase(); diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorConversionEventDataImpl.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorConversionEventDataImpl.java index 85dac2bb..6e8c1bb0 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorConversionEventDataImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorConversionEventDataImpl.java @@ -47,7 +47,7 @@ public class CalculatorConversionEventDataImpl implements CalculatorConversionEv private Generic value; @Nonnull - private CalculatorDisplayViewState displayState; + private DisplayState displayState; private CalculatorConversionEventDataImpl() { } @@ -57,7 +57,7 @@ public class CalculatorConversionEventDataImpl implements CalculatorConversionEv @Nonnull Generic value, @Nonnull NumeralBase from, @Nonnull NumeralBase to, - @Nonnull CalculatorDisplayViewState displayViewState) { + @Nonnull DisplayState displayViewState) { final CalculatorConversionEventDataImpl result = new CalculatorConversionEventDataImpl(); result.calculatorEventData = calculatorEventData; @@ -102,7 +102,7 @@ public class CalculatorConversionEventDataImpl implements CalculatorConversionEv @Nonnull @Override - public CalculatorDisplayViewState getDisplayState() { + public DisplayState getDisplayState() { return this.displayState; } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplay.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplay.java index 3a7d597f..1a053f3b 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplay.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplay.java @@ -40,9 +40,9 @@ public interface CalculatorDisplay extends CalculatorEventListener { void setView(@Nonnull CalculatorDisplayView view); @Nonnull - CalculatorDisplayViewState getViewState(); + DisplayState getViewState(); - void setViewState(@Nonnull CalculatorDisplayViewState viewState); + void setViewState(@Nonnull DisplayState viewState); @Nonnull CalculatorEventData getLastEventData(); diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayChangeEventData.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayChangeEventData.java index 8b8f2091..8b803fb7 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayChangeEventData.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayChangeEventData.java @@ -27,5 +27,5 @@ package org.solovyev.android.calculator; * Date: 9/21/12 * Time: 9:49 PM */ -public interface CalculatorDisplayChangeEventData extends Change { +public interface CalculatorDisplayChangeEventData extends Change { } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayChangeEventDataImpl.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayChangeEventDataImpl.java index f00bf4a6..a47b2aac 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayChangeEventDataImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayChangeEventDataImpl.java @@ -32,25 +32,25 @@ import javax.annotation.Nonnull; public class CalculatorDisplayChangeEventDataImpl implements CalculatorDisplayChangeEventData { @Nonnull - private final CalculatorDisplayViewState oldState; + private final DisplayState oldState; @Nonnull - private final CalculatorDisplayViewState newState; + private final DisplayState newState; - public CalculatorDisplayChangeEventDataImpl(@Nonnull CalculatorDisplayViewState oldState, @Nonnull CalculatorDisplayViewState newState) { + public CalculatorDisplayChangeEventDataImpl(@Nonnull DisplayState oldState, @Nonnull DisplayState newState) { this.oldState = oldState; this.newState = newState; } @Nonnull @Override - public CalculatorDisplayViewState getOldValue() { + public DisplayState getOldValue() { return this.oldState; } @Nonnull @Override - public CalculatorDisplayViewState getNewValue() { + public DisplayState getNewValue() { return this.newState; } } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayImpl.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayImpl.java index 19a7b952..31279925 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayImpl.java @@ -32,11 +32,6 @@ import static org.solovyev.android.calculator.CalculatorEventType.conversion_fai import static org.solovyev.android.calculator.CalculatorEventType.conversion_result; import static org.solovyev.android.calculator.CalculatorEventType.display_state_changed; -/** - * User: serso - * Date: 9/20/12 - * Time: 8:24 PM - */ public class CalculatorDisplayImpl implements CalculatorDisplay { @Nonnull @@ -48,7 +43,7 @@ public class CalculatorDisplayImpl implements CalculatorDisplay { @Nullable private CalculatorDisplayView view; @Nonnull - private CalculatorDisplayViewState viewState = CalculatorDisplayViewStateImpl.newDefaultInstance(); + private DisplayState viewState = DisplayState.empty(); public CalculatorDisplayImpl(@Nonnull Calculator calculator) { this.calculator = calculator; @@ -81,22 +76,22 @@ public class CalculatorDisplayImpl implements CalculatorDisplay { @Nonnull @Override - public CalculatorDisplayViewState getViewState() { + public DisplayState getViewState() { return this.viewState; } @Override - public void setViewState(@Nonnull CalculatorDisplayViewState newViewState) { + public void setViewState(@Nonnull DisplayState newViewState) { synchronized (viewLock) { - final CalculatorDisplayViewState oldViewState = setViewState0(newViewState); + final DisplayState oldViewState = setViewState0(newViewState); this.calculator.fireCalculatorEvent(display_state_changed, new CalculatorDisplayChangeEventDataImpl(oldViewState, newViewState)); } } - private void setViewStateForSequence(@Nonnull CalculatorDisplayViewState newViewState, @Nonnull Long sequenceId) { + private void setViewStateForSequence(@Nonnull DisplayState newViewState, @Nonnull Long sequenceId) { synchronized (viewLock) { - final CalculatorDisplayViewState oldViewState = setViewState0(newViewState); + final DisplayState oldViewState = setViewState0(newViewState); this.calculator.fireCalculatorEvent(display_state_changed, new CalculatorDisplayChangeEventDataImpl(oldViewState, newViewState), sequenceId); } @@ -104,8 +99,8 @@ public class CalculatorDisplayImpl implements CalculatorDisplay { // must be synchronized with viewLock @Nonnull - private CalculatorDisplayViewState setViewState0(@Nonnull CalculatorDisplayViewState newViewState) { - final CalculatorDisplayViewState oldViewState = this.viewState; + private DisplayState setViewState0(@Nonnull DisplayState newViewState) { + final DisplayState oldViewState = this.viewState; this.viewState = newViewState; if (this.view != null) { @@ -152,7 +147,7 @@ public class CalculatorDisplayImpl implements CalculatorDisplay { private void processConversationFailed(@Nonnull CalculatorConversionEventData calculatorEventData, @Nonnull ConversionFailure data) { - this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newErrorState(calculatorEventData.getDisplayState().getOperation(), CalculatorMessages.getBundle().getString(CalculatorMessages.syntax_error)), calculatorEventData.getSequenceId()); + this.setViewStateForSequence(DisplayState.createError(calculatorEventData.getDisplayState().getOperation(), CalculatorMessages.getBundle().getString(CalculatorMessages.syntax_error)), calculatorEventData.getSequenceId()); } @@ -172,18 +167,18 @@ public class CalculatorDisplayImpl implements CalculatorDisplay { } } - this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newErrorState(calculatorEventData.getOperation(), errorMessage), calculatorEventData.getSequenceId()); + this.setViewStateForSequence(DisplayState.createError(calculatorEventData.getOperation(), errorMessage), calculatorEventData.getSequenceId()); } private void processCalculationCancelled(@Nonnull CalculatorEvaluationEventData calculatorEventData) { final String errorMessage = CalculatorMessages.getBundle().getString(CalculatorMessages.syntax_error); - this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newErrorState(calculatorEventData.getOperation(), errorMessage), calculatorEventData.getSequenceId()); + this.setViewStateForSequence(DisplayState.createError(calculatorEventData.getOperation(), errorMessage), calculatorEventData.getSequenceId()); } private void processCalculationResult(@Nonnull CalculatorEvaluationEventData calculatorEventData, @Nonnull CalculatorOutput data) { final String stringResult = data.getStringResult(); - this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newValidState(calculatorEventData.getOperation(), data.getResult(), stringResult, 0), calculatorEventData.getSequenceId()); + this.setViewStateForSequence(DisplayState.createValid(calculatorEventData.getOperation(), data.getResult(), stringResult, 0), calculatorEventData.getSequenceId()); } private void processConversationResult(@Nonnull CalculatorConversionEventData calculatorEventData, @Nonnull String result) { @@ -192,7 +187,7 @@ public class CalculatorDisplayImpl implements CalculatorDisplay { result = calculatorEventData.getToNumeralBase().getJsclPrefix() + result; } - final CalculatorDisplayViewState displayState = calculatorEventData.getDisplayState(); - this.setViewStateForSequence(CalculatorDisplayViewStateImpl.newValidState(displayState.getOperation(), displayState.getResult(), result, 0), calculatorEventData.getSequenceId()); + final DisplayState displayState = calculatorEventData.getDisplayState(); + this.setViewStateForSequence(DisplayState.createValid(displayState.getOperation(), displayState.getResult(), result, 0), calculatorEventData.getSequenceId()); } } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayMenuItem.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayMenuItem.java index 24ac3253..1b9e6dc6 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayMenuItem.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayMenuItem.java @@ -38,18 +38,18 @@ import jscl.math.Generic; * Date: 21.09.12 * Time: 10:55 */ -public enum CalculatorDisplayMenuItem implements LabeledMenuItem { +public enum CalculatorDisplayMenuItem implements LabeledMenuItem { copy(R.string.c_copy) { @Override - public void onClick(@Nonnull CalculatorDisplayViewState data, @Nonnull Context context) { + public void onClick(@Nonnull DisplayState data, @Nonnull Context context) { Locator.getInstance().getKeyboard().copyButtonPressed(); } }, convert_to_bin(R.string.convert_to_bin) { @Override - public void onClick(@Nonnull CalculatorDisplayViewState data, @Nonnull Context context) { + public void onClick(@Nonnull DisplayState data, @Nonnull Context context) { ConversionMenuItem.convert_to_bin.onClick(data, context); } @@ -61,7 +61,7 @@ public enum CalculatorDisplayMenuItem implements LabeledMenuItem filteredMenuItems = new ArrayList(CalculatorDisplayMenuItem.values().length); diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayView.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayView.java index be61f822..87229421 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayView.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayView.java @@ -32,7 +32,7 @@ import javax.annotation.Nonnull; public interface CalculatorDisplayView { @Nonnull - CalculatorDisplayViewState getState(); + DisplayState getState(); - void setState(@Nonnull CalculatorDisplayViewState state); + void setState(@Nonnull DisplayState state); } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayViewState.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayViewState.java deleted file mode 100644 index 0ea5b78f..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayViewState.java +++ /dev/null @@ -1,59 +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.android.calculator.jscl.JsclOperation; - -import java.io.Serializable; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import jscl.math.Generic; - -/** - * User: serso - * Date: 9/20/12 - * Time: 9:50 PM - */ -public interface CalculatorDisplayViewState extends Serializable { - - @Nonnull - String getText(); - - int getSelection(); - - @Nullable - Generic getResult(); - - boolean isValid(); - - @Nullable - String getErrorMessage(); - - @Nonnull - JsclOperation getOperation(); - - @Nullable - String getStringResult(); -} diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorEditor.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorEditor.java deleted file mode 100644 index 04f34c11..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorEditor.java +++ /dev/null @@ -1,117 +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.gui.CursorControl; - -import javax.annotation.Nonnull; - -/** - * User: Solovyev_S - * Date: 21.09.12 - * Time: 11:47 - */ -public interface CalculatorEditor extends CalculatorEventListener { - - @Nonnull - String TAG = CalculatorEditor.class.getSimpleName(); - - void setView(@Nonnull EditorView view); - - void clearView(@Nonnull EditorView view); - - @Nonnull - EditorState getViewState(); - - void setViewState(@Nonnull EditorState viewState); - - // updates state of view (view.setState()) - void updateViewState(); - - /* - ********************************************************************** - * - * CURSOR CONTROL - * - ********************************************************************** - */ - - /** - * Method sets the cursor to the beginning - */ - @Nonnull - EditorState setCursorOnStart(); - - /** - * Method sets the cursor to the end - */ - @Nonnull - EditorState setCursorOnEnd(); - - /** - * Method moves cursor to the left of current position - */ - @Nonnull - EditorState moveCursorLeft(); - - /** - * Method moves cursor to the right of current position - */ - @Nonnull - EditorState moveCursorRight(); - - @Nonnull - CursorControl asCursorControl(); - - - /* - ********************************************************************** - * - * EDITOR OPERATIONS - * - ********************************************************************** - */ - @Nonnull - EditorState erase(); - - @Nonnull - EditorState clear(); - - @Nonnull - EditorState setText(@Nonnull String text); - - @Nonnull - EditorState setText(@Nonnull String text, int selection); - - @Nonnull - EditorState insert(@Nonnull String text); - - @Nonnull - EditorState insert(@Nonnull String text, int selectionOffset); - - @Nonnull - EditorState moveSelection(int offset); - - @Nonnull - EditorState setSelection(int selection); -} diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorImpl.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorImpl.java deleted file mode 100644 index 03eaf171..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorImpl.java +++ /dev/null @@ -1,356 +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.android.calculator.history.CalculatorHistoryState; -import org.solovyev.android.calculator.history.EditorHistoryState; -import org.solovyev.android.calculator.text.TextProcessor; -import org.solovyev.android.calculator.text.TextProcessorEditorResult; -import org.solovyev.common.gui.CursorControl; -import org.solovyev.common.text.Strings; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import static java.lang.Math.min; -import static org.solovyev.android.calculator.CalculatorEditorChangeEventData.newChangeEventData; -import static org.solovyev.android.calculator.CalculatorEventType.editor_state_changed; -import static org.solovyev.android.calculator.CalculatorEventType.editor_state_changed_light; - -/** - * User: Solovyev_S - * Date: 21.09.12 - * Time: 11:53 - */ -public class CalculatorEditorImpl implements CalculatorEditor { - - @Nonnull - private final Object viewLock = new Object(); - @Nonnull - private final Calculator calculator; - @Nonnull - private final CalculatorEventHolder lastEventHolder; - @Nonnull - private final CursorControlAdapter cursorControlAdapter = new CursorControlAdapter(this); - @Nullable - private final TextProcessor textProcessor; - @Nullable - private EditorView view; - @Nonnull - private EditorState lastViewState = EditorState.empty(); - - public CalculatorEditorImpl(@Nonnull Calculator calculator, @Nullable TextProcessor textProcessor) { - this.calculator = calculator; - this.textProcessor = textProcessor; - this.calculator.addCalculatorEventListener(this); - this.lastEventHolder = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId()); - } - - public static int clamp(int selection, @Nonnull CharSequence text) { - return clamp(selection, text.length()); - } - - public static int clamp(int selection, int max) { - return min(Math.max(selection, 0), max); - } - - @Override - public void setView(@Nonnull EditorView view) { - synchronized (viewLock) { - this.view = view; - this.view.setState(lastViewState); - } - } - - @Override - public void clearView(@Nonnull EditorView view) { - synchronized (viewLock) { - if (this.view == view) { - this.view = null; - } - } - } - - @Nonnull - @Override - public EditorState getViewState() { - return lastViewState; - } - - @Override - public void setViewState(@Nonnull EditorState newViewState) { - setViewState(newViewState, true); - } - - @Override - public void updateViewState() { - setViewState(this.lastViewState, false); - } - - private void setViewState(@Nonnull EditorState newViewState, boolean majorChanges) { - if (textProcessor != null) { - try { - final TextProcessorEditorResult result = textProcessor.process(newViewState.getText()); - newViewState = EditorState.create(result.getCharSequence(), newViewState.getSelection() + result.getOffset()); - } catch (CalculatorParseException e) { - Locator.getInstance().getLogger().error(TAG, e.getMessage(), e); - } - } - synchronized (viewLock) { - final EditorState oldViewState = this.lastViewState; - - this.lastViewState = newViewState; - if (this.view != null) { - this.view.setState(newViewState); - } - - fireStateChangedEvent(majorChanges, oldViewState, newViewState); - } - } - - /* - ********************************************************************** - * - * SELECTION - * - ********************************************************************** - */ - - private void fireStateChangedEvent(boolean majorChanges, @Nonnull EditorState oldViewState, @Nonnull EditorState newViewState) { - if (!Thread.holdsLock(viewLock)) throw new AssertionError(); - - if (majorChanges) { - calculator.fireCalculatorEvent(editor_state_changed, newChangeEventData(oldViewState, newViewState)); - } else { - calculator.fireCalculatorEvent(editor_state_changed_light, newChangeEventData(oldViewState, newViewState)); - } - } - - @Override - public void onCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData, - @Nonnull CalculatorEventType calculatorEventType, - @Nullable Object data) { - final CalculatorEventHolder.Result result = lastEventHolder.apply(calculatorEventData); - - if (result.isNewAfter()) { - switch (calculatorEventType) { - case use_history_state: - final CalculatorHistoryState calculatorHistoryState = (CalculatorHistoryState) data; - final EditorHistoryState editorState = calculatorHistoryState.getEditorState(); - this.setText(Strings.getNotEmpty(editorState.getText(), ""), editorState.getCursorPosition()); - break; - } - } - } - - @Nonnull - private EditorState newSelectionViewState(int newSelection) { - if (this.lastViewState.getSelection() != newSelection) { - final EditorState result = EditorState.newSelection(this.lastViewState, newSelection); - setViewState(result, false); - return result; - } else { - return this.lastViewState; - } - } - - @Nonnull - public EditorState setCursorOnStart() { - synchronized (viewLock) { - return newSelectionViewState(0); - } - } - - @Nonnull - public EditorState setCursorOnEnd() { - synchronized (viewLock) { - return newSelectionViewState(this.lastViewState.getText().length()); - } - } - - @Nonnull - public EditorState moveCursorLeft() { - synchronized (viewLock) { - if (this.lastViewState.getSelection() > 0) { - return newSelectionViewState(this.lastViewState.getSelection() - 1); - } else { - return this.lastViewState; - } - } - } - - /* - ********************************************************************** - * - * EDITOR ACTIONS - * - ********************************************************************** - */ - - @Nonnull - public EditorState moveCursorRight() { - synchronized (viewLock) { - if (this.lastViewState.getSelection() < this.lastViewState.getText().length()) { - return newSelectionViewState(this.lastViewState.getSelection() + 1); - } else { - return this.lastViewState; - } - } - } - - @Nonnull - @Override - public CursorControl asCursorControl() { - return cursorControlAdapter; - } - - @Nonnull - @Override - public EditorState erase() { - synchronized (viewLock) { - int selection = this.lastViewState.getSelection(); - final String text = this.lastViewState.getText(); - if (selection > 0 && text.length() > 0 && selection <= text.length()) { - final StringBuilder newText = new StringBuilder(text.length() - 1); - newText.append(text.substring(0, selection - 1)).append(text.substring(selection, text.length())); - - final EditorState result = EditorState.create(newText.toString(), selection - 1); - setViewState(result); - return result; - } else { - return this.lastViewState; - } - } - } - - @Nonnull - @Override - public EditorState clear() { - synchronized (viewLock) { - return setText(""); - } - } - - @Nonnull - @Override - public EditorState setText(@Nonnull String text) { - synchronized (viewLock) { - final EditorState result = EditorState.create(text, text.length()); - setViewState(result); - return result; - } - } - - @Nonnull - @Override - public EditorState setText(@Nonnull String text, int selection) { - synchronized (viewLock) { - selection = clamp(selection, text); - - final EditorState result = EditorState.create(text, selection); - setViewState(result); - return result; - } - } - - @Nonnull - @Override - public EditorState insert(@Nonnull String text) { - synchronized (viewLock) { - return insert(text, 0); - } - } - - @Nonnull - @Override - public EditorState insert(@Nonnull String text, int selectionOffset) { - synchronized (viewLock) { - final String oldText = lastViewState.getText(); - final int selection = clamp(lastViewState.getSelection(), oldText); - - int newTextLength = text.length() + oldText.length(); - final StringBuilder newText = new StringBuilder(newTextLength); - - newText.append(oldText.substring(0, selection)); - newText.append(text); - newText.append(oldText.substring(selection)); - - int newSelection = clamp(text.length() + selection + selectionOffset, newTextLength); - final EditorState result = EditorState.create(newText.toString(), newSelection); - setViewState(result); - return result; - } - } - - @Nonnull - @Override - public EditorState moveSelection(int offset) { - synchronized (viewLock) { - int selection = this.lastViewState.getSelection() + offset; - - return setSelection(selection); - } - } - - @Nonnull - @Override - public EditorState setSelection(int selection) { - synchronized (viewLock) { - selection = clamp(selection, this.lastViewState.getText()); - - final EditorState result = EditorState.newSelection(this.lastViewState, selection); - setViewState(result, false); - return result; - } - } - - private static final class CursorControlAdapter implements CursorControl { - - @Nonnull - private final CalculatorEditor calculatorEditor; - - private CursorControlAdapter(@Nonnull CalculatorEditor calculatorEditor) { - this.calculatorEditor = calculatorEditor; - } - - @Override - public void setCursorOnStart() { - this.calculatorEditor.setCursorOnStart(); - } - - @Override - public void setCursorOnEnd() { - this.calculatorEditor.setCursorOnEnd(); - } - - @Override - public void moveCursorLeft() { - this.calculatorEditor.moveCursorLeft(); - } - - @Override - public void moveCursorRight() { - this.calculatorEditor.moveCursorRight(); - } - } -} diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java index 2cee7b35..def5f43a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java @@ -31,7 +31,7 @@ import jscl.math.function.IConstant; import jscl.math.operator.Operator; import jscl.text.ParseInterruptedException; import org.solovyev.android.calculator.history.CalculatorHistory; -import org.solovyev.android.calculator.history.CalculatorHistoryState; +import org.solovyev.android.calculator.history.HistoryState; import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.model.Var; import org.solovyev.android.calculator.text.TextProcessor; @@ -173,21 +173,21 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { @Override public void evaluate() { - final EditorState viewState = getEditor().getViewState(); + final EditorState viewState = getEditor().getState(); final CalculatorEventData eventData = fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState); this.evaluate(JsclOperation.numeric, viewState.getText(), eventData.getSequenceId()); } @Override public void evaluate(@Nonnull Long sequenceId) { - final EditorState viewState = getEditor().getViewState(); + final EditorState viewState = getEditor().getState(); fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState, sequenceId); this.evaluate(JsclOperation.numeric, viewState.getText(), sequenceId); } @Override public void simplify() { - final EditorState viewState = getEditor().getViewState(); + final EditorState viewState = getEditor().getState(); final CalculatorEventData eventData = fireCalculatorEvent(CalculatorEventType.manual_calculation_requested, viewState); this.evaluate(JsclOperation.simplify, viewState.getText(), eventData.getSequenceId()); } @@ -250,7 +250,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { @Nonnull Generic value, @Nonnull NumeralBase from, @Nonnull NumeralBase to, - @Nonnull CalculatorDisplayViewState displayViewState) { + @Nonnull DisplayState displayViewState) { return CalculatorConversionEventDataImpl.newInstance(nextEventData(sequenceId), value, from, to, displayViewState); } @@ -393,7 +393,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { @Nonnull final NumeralBase to) { final CalculatorEventData eventDataId = nextEventData(); - final CalculatorDisplayViewState displayViewState = Locator.getInstance().getDisplay().getViewState(); + final DisplayState displayViewState = Locator.getInstance().getDisplay().getViewState(); final NumeralBase from = Locator.getInstance().getEngine().getNumeralBase(); calculationsExecutor.execute(new Runnable() { @@ -562,7 +562,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { } private void onDisplayStateChanged(@Nonnull CalculatorDisplayChangeEventData displayChangeEventData) { - final CalculatorDisplayViewState newState = displayChangeEventData.getNewValue(); + final DisplayState newState = displayChangeEventData.getNewValue(); if (newState.isValid()) { final String result = newState.getStringResult(); if (!Strings.isEmpty(result)) { @@ -597,7 +597,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { public void doHistoryAction(@Nonnull HistoryAction historyAction) { final CalculatorHistory history = Locator.getInstance().getHistory(); if (history.isActionAvailable(historyAction)) { - final CalculatorHistoryState newState = history.doAction(historyAction, getCurrentHistoryState()); + final HistoryState newState = history.doAction(historyAction, getCurrentHistoryState()); if (newState != null) { setCurrentHistoryState(newState); } @@ -606,12 +606,12 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { @Nonnull @Override - public CalculatorHistoryState getCurrentHistoryState() { - return CalculatorHistoryState.newInstance(getEditor(), getDisplay()); + public HistoryState getCurrentHistoryState() { + return HistoryState.create(getEditor(), getDisplay()); } @Override - public void setCurrentHistoryState(@Nonnull CalculatorHistoryState editorHistoryState) { + public void setCurrentHistoryState(@Nonnull HistoryState editorHistoryState) { editorHistoryState.setValuesFromHistory(getEditor(), getDisplay()); } @@ -624,7 +624,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { */ @Nonnull - private CalculatorEditor getEditor() { + private Editor getEditor() { return Locator.getInstance().getEditor(); } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorKeyboardImpl.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorKeyboardImpl.java index eef5ec8a..43b6ff59 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorKeyboardImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorKeyboardImpl.java @@ -83,7 +83,7 @@ public class CalculatorKeyboardImpl implements CalculatorKeyboard { } } - final CalculatorEditor editor = Locator.getInstance().getEditor(); + final Editor editor = Locator.getInstance().getEditor(); editor.insert(textToBeInserted.toString(), cursorPositionOffset); } @@ -110,8 +110,8 @@ public class CalculatorKeyboardImpl implements CalculatorKeyboard { @Override public void roundBracketsButtonPressed() { - final CalculatorEditor editor = Locator.getInstance().getEditor(); - EditorState viewState = editor.getViewState(); + final Editor editor = Locator.getInstance().getEditor(); + EditorState viewState = editor.getState(); final int cursorPosition = viewState.getSelection(); final String oldText = viewState.getText(); @@ -139,7 +139,7 @@ public class CalculatorKeyboardImpl implements CalculatorKeyboard { @Override public void copyButtonPressed() { - final CalculatorDisplayViewState displayViewState = Locator.getInstance().getDisplay().getViewState(); + final DisplayState displayViewState = Locator.getInstance().getDisplay().getViewState(); if (displayViewState.isValid()) { final CharSequence text = displayViewState.getText(); if (!Strings.isEmpty(text)) { diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java index 85e25ddf..7aa58fd7 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java @@ -58,7 +58,7 @@ public interface CalculatorLocator { CalculatorDisplay getDisplay(); @Nonnull - CalculatorEditor getEditor(); + Editor getEditor(); @Nonnull CalculatorKeyboard getKeyboard(); diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorSpecialButton.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorSpecialButton.java index bfde70d7..a3cc08c3 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorSpecialButton.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorSpecialButton.java @@ -115,7 +115,7 @@ public enum CalculatorSpecialButton { return; } - final CalculatorDisplayViewState displayViewState = Locator.getInstance().getDisplay().getViewState(); + final DisplayState displayViewState = Locator.getInstance().getDisplay().getViewState(); if (displayViewState.isValid()) { final CharSequence text = displayViewState.getText(); if (!Strings.isEmpty(text)) { diff --git a/app/src/main/java/org/solovyev/android/calculator/ConversionMenuItem.java b/app/src/main/java/org/solovyev/android/calculator/ConversionMenuItem.java index f528f99c..a5a10e98 100644 --- a/app/src/main/java/org/solovyev/android/calculator/ConversionMenuItem.java +++ b/app/src/main/java/org/solovyev/android/calculator/ConversionMenuItem.java @@ -37,7 +37,7 @@ import jscl.math.Generic; * Date: 9/21/12 * Time: 12:11 AM */ -enum ConversionMenuItem implements AMenuItem { +enum ConversionMenuItem implements AMenuItem { convert_to_bin(NumeralBase.bin), convert_to_dec(NumeralBase.dec), @@ -67,7 +67,7 @@ enum ConversionMenuItem implements AMenuItem { } @Override - public void onClick(@Nonnull CalculatorDisplayViewState data, @Nonnull Context context) { + public void onClick(@Nonnull DisplayState data, @Nonnull Context context) { final Generic result = data.getResult(); if (result != null) { diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayViewStateImpl.java b/app/src/main/java/org/solovyev/android/calculator/DisplayState.java similarity index 58% rename from app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayViewStateImpl.java rename to app/src/main/java/org/solovyev/android/calculator/DisplayState.java index 2f6b1903..54575985 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorDisplayViewStateImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/DisplayState.java @@ -25,25 +25,14 @@ package org.solovyev.android.calculator; import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.common.text.Strings; +import java.io.Serializable; + import javax.annotation.Nonnull; import javax.annotation.Nullable; import jscl.math.Generic; -/** - * User: serso - * Date: 9/20/12 - * Time: 9:50 PM - */ -public class CalculatorDisplayViewStateImpl implements CalculatorDisplayViewState { - - /* - ********************************************************************** - * - * FIELDS - * - ********************************************************************** - */ +public class DisplayState implements Serializable { @Nonnull private JsclOperation operation = JsclOperation.numeric; @@ -61,26 +50,18 @@ public class CalculatorDisplayViewStateImpl implements CalculatorDisplayViewStat private int selection = 0; - /* - ********************************************************************** - * - * CONSTRUCTORS - * - ********************************************************************** - */ - - private CalculatorDisplayViewStateImpl() { + private DisplayState() { } @Nonnull - public static CalculatorDisplayViewState newDefaultInstance() { - return new CalculatorDisplayViewStateImpl(); + public static DisplayState empty() { + return new DisplayState(); } @Nonnull - public static CalculatorDisplayViewState newErrorState(@Nonnull JsclOperation operation, - @Nonnull String errorMessage) { - final CalculatorDisplayViewStateImpl calculatorDisplayState = new CalculatorDisplayViewStateImpl(); + public static DisplayState createError(@Nonnull JsclOperation operation, + @Nonnull String errorMessage) { + final DisplayState calculatorDisplayState = new DisplayState(); calculatorDisplayState.valid = false; calculatorDisplayState.errorMessage = errorMessage; calculatorDisplayState.operation = operation; @@ -88,11 +69,11 @@ public class CalculatorDisplayViewStateImpl implements CalculatorDisplayViewStat } @Nonnull - public static CalculatorDisplayViewState newValidState(@Nonnull JsclOperation operation, - @Nullable Generic result, - @Nonnull String stringResult, - int selection) { - final CalculatorDisplayViewStateImpl calculatorDisplayState = new CalculatorDisplayViewStateImpl(); + public static DisplayState createValid(@Nonnull JsclOperation operation, + @Nullable Generic result, + @Nonnull String stringResult, + int selection) { + final DisplayState calculatorDisplayState = new DisplayState(); calculatorDisplayState.valid = true; calculatorDisplayState.result = result; calculatorDisplayState.stringResult = stringResult; @@ -102,50 +83,35 @@ public class CalculatorDisplayViewStateImpl implements CalculatorDisplayViewStat return calculatorDisplayState; } - /* - ********************************************************************** - * - * METHODS - * - ********************************************************************** - */ - @Nonnull - @Override public String getText() { return Strings.getNotEmpty(isValid() ? stringResult : errorMessage, ""); } - @Override public int getSelection() { return selection; } @Nullable - @Override public Generic getResult() { return this.result; } - @Override public boolean isValid() { return this.valid; } @Nullable - @Override public String getErrorMessage() { return this.errorMessage; } - @Override @Nullable public String getStringResult() { return stringResult; } @Nonnull - @Override public JsclOperation getOperation() { return this.operation; } diff --git a/app/src/main/java/org/solovyev/android/calculator/Editor.java b/app/src/main/java/org/solovyev/android/calculator/Editor.java new file mode 100644 index 00000000..d1cb1cb5 --- /dev/null +++ b/app/src/main/java/org/solovyev/android/calculator/Editor.java @@ -0,0 +1,265 @@ +/* + * 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.android.Check; +import org.solovyev.android.calculator.history.HistoryState; +import org.solovyev.android.calculator.history.EditorHistoryState; +import org.solovyev.android.calculator.text.TextProcessor; +import org.solovyev.android.calculator.text.TextProcessorEditorResult; +import org.solovyev.common.text.Strings; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import static java.lang.Math.min; +import static org.solovyev.android.calculator.CalculatorEditorChangeEventData.newChangeEventData; +import static org.solovyev.android.calculator.CalculatorEventType.editor_state_changed; +import static org.solovyev.android.calculator.CalculatorEventType.editor_state_changed_light; + +public class Editor implements CalculatorEventListener { + + private static final String TAG = App.subTag("Editor"); + + @Nonnull + private final Calculator calculator; + @Nonnull + private final CalculatorEventHolder lastEventHolder; + @Nullable + private final TextProcessor textProcessor; + @Nullable + private EditorView view; + @Nonnull + private EditorState state = EditorState.empty(); + + public Editor(@Nonnull Calculator calculator, @Nullable TextProcessor textProcessor) { + this.calculator = calculator; + this.textProcessor = textProcessor; + this.calculator.addCalculatorEventListener(this); + this.lastEventHolder = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId()); + } + + public static int clamp(int selection, @Nonnull CharSequence text) { + return clamp(selection, text.length()); + } + + public static int clamp(int selection, int max) { + return min(Math.max(selection, 0), max); + } + + public void setView(@Nonnull EditorView view) { + Check.isMainThread(); + this.view = view; + this.view.setState(state); + } + + public void clearView(@Nonnull EditorView view) { + Check.isMainThread(); + if (this.view == view) { + this.view = null; + } + } + + @Nonnull + public EditorState getState() { + return state; + } + + public void setState(@Nonnull EditorState state) { + setState(state, true); + } + + private void setState(@Nonnull EditorState newState, boolean majorChanges) { + Check.isMainThread(); + if (textProcessor != null) { + try { + final TextProcessorEditorResult result = textProcessor.process(newState.getText()); + newState = EditorState.create(result.getCharSequence(), newState.getSelection() + result.getOffset()); + } catch (CalculatorParseException e) { + Locator.getInstance().getLogger().error(TAG, e.getMessage(), e); + } + } + final EditorState oldState = state; + state = newState; + if (view != null) { + view.setState(newState); + } + fireStateChangedEvent(majorChanges, oldState, newState); + } + + private void fireStateChangedEvent(boolean majorChanges, @Nonnull EditorState oldViewState, @Nonnull EditorState newViewState) { + Check.isMainThread(); + + if (majorChanges) { + calculator.fireCalculatorEvent(editor_state_changed, newChangeEventData(oldViewState, newViewState)); + } else { + calculator.fireCalculatorEvent(editor_state_changed_light, newChangeEventData(oldViewState, newViewState)); + } + } + + @Override + public void onCalculatorEvent(@Nonnull CalculatorEventData evenData, + @Nonnull CalculatorEventType eventType, + @Nullable Object data) { + Check.isMainThread(); + final CalculatorEventHolder.Result result = lastEventHolder.apply(evenData); + + if (result.isNewAfter()) { + switch (eventType) { + case use_history_state: + final HistoryState historyState = (HistoryState) data; + final EditorHistoryState editorState = historyState.getEditorState(); + this.setText(Strings.getNotEmpty(editorState.getText(), ""), editorState.getCursorPosition()); + break; + } + } + } + + @Nonnull + private EditorState newSelectionViewState(int newSelection) { + Check.isMainThread(); + if (state.getSelection() != newSelection) { + final EditorState result = EditorState.newSelection(state, newSelection); + setState(result, false); + return result; + } else { + return state; + } + } + + @Nonnull + public EditorState setCursorOnStart() { + Check.isMainThread(); + return newSelectionViewState(0); + } + + @Nonnull + public EditorState setCursorOnEnd() { + Check.isMainThread(); + return newSelectionViewState(state.getText().length()); + } + + @Nonnull + public EditorState moveCursorLeft() { + Check.isMainThread(); + if (state.getSelection() > 0) { + return newSelectionViewState(state.getSelection() - 1); + } else { + return state; + } + } + + @Nonnull + public EditorState moveCursorRight() { + Check.isMainThread(); + if (state.getSelection() < state.getText().length()) { + return newSelectionViewState(state.getSelection() + 1); + } else { + return state; + } + } + + @Nonnull + public EditorState erase() { + Check.isMainThread(); + int selection = state.getSelection(); + final String text = state.getText(); + if (selection > 0 && text.length() > 0 && selection <= text.length()) { + final StringBuilder newText = new StringBuilder(text.length() - 1); + newText.append(text.substring(0, selection - 1)).append(text.substring(selection, text.length())); + + final EditorState result = EditorState.create(newText.toString(), selection - 1); + setState(result); + return result; + } else { + return state; + } + } + + @Nonnull + public EditorState clear() { + Check.isMainThread(); + return setText(""); + } + + @Nonnull + public EditorState setText(@Nonnull String text) { + Check.isMainThread(); + final EditorState result = EditorState.create(text, text.length()); + setState(result); + return result; + } + + @Nonnull + public EditorState setText(@Nonnull String text, int selection) { + Check.isMainThread(); + selection = clamp(selection, text); + + final EditorState result = EditorState.create(text, selection); + setState(result); + return result; + } + + @Nonnull + public EditorState insert(@Nonnull String text) { + Check.isMainThread(); + return insert(text, 0); + } + + @Nonnull + public EditorState insert(@Nonnull String text, int selectionOffset) { + Check.isMainThread(); + final String oldText = state.getText(); + final int selection = clamp(state.getSelection(), oldText); + + int newTextLength = text.length() + oldText.length(); + final StringBuilder newText = new StringBuilder(newTextLength); + + newText.append(oldText.substring(0, selection)); + newText.append(text); + newText.append(oldText.substring(selection)); + + int newSelection = clamp(text.length() + selection + selectionOffset, newTextLength); + final EditorState result = EditorState.create(newText.toString(), newSelection); + setState(result); + return result; + } + + @Nonnull + public EditorState moveSelection(int offset) { + Check.isMainThread(); + int selection = state.getSelection() + offset; + return setSelection(selection); + } + + @Nonnull + public EditorState setSelection(int selection) { + Check.isMainThread(); + selection = clamp(selection, state.getText()); + + final EditorState result = EditorState.newSelection(state, selection); + setState(result, false); + return result; + } + +} diff --git a/app/src/main/java/org/solovyev/android/calculator/EditorView.java b/app/src/main/java/org/solovyev/android/calculator/EditorView.java index 4f0e5661..6dba4dde 100644 --- a/app/src/main/java/org/solovyev/android/calculator/EditorView.java +++ b/app/src/main/java/org/solovyev/android/calculator/EditorView.java @@ -31,22 +31,23 @@ import android.util.AttributeSet; import android.util.Log; import android.view.ContextMenu; import android.widget.EditText; + import org.solovyev.android.Check; import org.solovyev.android.calculator.onscreen.CalculatorOnscreenService; +import java.lang.reflect.Method; + import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.lang.reflect.Method; public class EditorView extends EditText { @Nonnull private static final String TAG = App.subTag("EditorView"); - private boolean initialized; + private boolean reportChanges; @Nullable private Method setShowSoftInputOnFocusMethod; - private boolean externalChange; public EditorView(Context context) { super(context); @@ -70,10 +71,10 @@ public class EditorView extends EditText { } private void init() { - Check.isTrue(!initialized); addTextChangedListener(new MyTextWatcher()); setShowSoftInputOnFocusCompat(false); - initialized = true; + // changes should only be reported after the view has been set up completely, i.e. now + reportChanges = true; } @Override @@ -85,24 +86,25 @@ public class EditorView extends EditText { public void setState(@Nonnull final EditorState state) { Check.isMainThread(); try { - externalChange = true; + // we don't want to be notified about changes we make ourselves + reportChanges = false; if (App.getTheme().light && getContext() instanceof CalculatorOnscreenService) { // don't need formatting setText(state.getText()); } else { setText(state.getTextAsCharSequence(), BufferType.EDITABLE); } - final int selection = CalculatorEditorImpl.clamp(state.getSelection(), length()); + final int selection = Editor.clamp(state.getSelection(), length()); setSelection(selection); } finally { - externalChange = false; + reportChanges = true; } } @Override protected void onSelectionChanged(int start, int end) { Check.isMainThread(); - if (!initialized || externalChange) { + if (!reportChanges) { return; } // external text change => need to notify editor @@ -147,7 +149,7 @@ public class EditorView extends EditText { @Override public void afterTextChanged(Editable s) { - if (!initialized || externalChange) { + if (!reportChanges) { return; } // external text change => need to notify editor diff --git a/app/src/main/java/org/solovyev/android/calculator/Locator.java b/app/src/main/java/org/solovyev/android/calculator/Locator.java index 343307a7..889eed53 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Locator.java +++ b/app/src/main/java/org/solovyev/android/calculator/Locator.java @@ -44,7 +44,7 @@ public class Locator implements CalculatorLocator { @Nonnull private Calculator calculator; @Nonnull - private CalculatorEditor calculatorEditor; + private Editor editor; @Nonnull private CalculatorDisplay calculatorDisplay; @Nonnull @@ -92,7 +92,7 @@ public class Locator implements CalculatorLocator { this.calculatorPreferenceService = preferenceService; this.calculatorPlotter = plotter; - calculatorEditor = new CalculatorEditorImpl(this.calculator, editorTextProcessor); + editor = new Editor(this.calculator, editorTextProcessor); calculatorDisplay = new CalculatorDisplayImpl(this.calculator); calculatorKeyboard = keyboard; } @@ -117,8 +117,8 @@ public class Locator implements CalculatorLocator { @Nonnull @Override - public CalculatorEditor getEditor() { - return calculatorEditor; + public Editor getEditor() { + return editor; } @Override diff --git a/app/src/main/java/org/solovyev/android/calculator/function/FunctionEditDialogFragment.java b/app/src/main/java/org/solovyev/android/calculator/function/FunctionEditDialogFragment.java index bb82be28..fe95a1ee 100644 --- a/app/src/main/java/org/solovyev/android/calculator/function/FunctionEditDialogFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/function/FunctionEditDialogFragment.java @@ -36,11 +36,11 @@ import android.view.ViewGroup; import android.widget.EditText; import org.solovyev.android.calculator.App; -import org.solovyev.android.calculator.CalculatorDisplayViewState; import org.solovyev.android.calculator.CalculatorEventData; import org.solovyev.android.calculator.CalculatorEventListener; import org.solovyev.android.calculator.CalculatorEventType; import org.solovyev.android.calculator.CalculatorUtils; +import org.solovyev.android.calculator.DisplayState; import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.math.edit.CalculatorFunctionsActivity; @@ -308,7 +308,7 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul } @Nonnull - public static Input newFromDisplay(@Nonnull CalculatorDisplayViewState viewState) { + public static Input newFromDisplay(@Nonnull DisplayState viewState) { final Input result = new Input(); result.content = viewState.getText(); diff --git a/app/src/main/java/org/solovyev/android/calculator/history/AndroidCalculatorHistory.java b/app/src/main/java/org/solovyev/android/calculator/history/AndroidCalculatorHistory.java index 695f3cc5..6df3882f 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/AndroidCalculatorHistory.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/AndroidCalculatorHistory.java @@ -80,7 +80,7 @@ public class AndroidCalculatorHistory implements CalculatorHistory { save(); } - public void removeSavedHistory(@Nonnull CalculatorHistoryState historyState) { + public void removeSavedHistory(@Nonnull HistoryState historyState) { historyState.setSaved(false); calculatorHistory.removeSavedHistory(historyState); save(); @@ -92,7 +92,7 @@ public class AndroidCalculatorHistory implements CalculatorHistory { } @Override - public CalculatorHistoryState getLastHistoryState() { + public HistoryState getLastHistoryState() { return calculatorHistory.getLastHistoryState(); } @@ -102,7 +102,7 @@ public class AndroidCalculatorHistory implements CalculatorHistory { } @Override - public CalculatorHistoryState undo(@Nullable CalculatorHistoryState currentState) { + public HistoryState undo(@Nullable HistoryState currentState) { return calculatorHistory.undo(currentState); } @@ -112,7 +112,7 @@ public class AndroidCalculatorHistory implements CalculatorHistory { } @Override - public CalculatorHistoryState redo(@Nullable CalculatorHistoryState currentState) { + public HistoryState redo(@Nullable HistoryState currentState) { return calculatorHistory.redo(currentState); } @@ -122,24 +122,24 @@ public class AndroidCalculatorHistory implements CalculatorHistory { } @Override - public CalculatorHistoryState doAction(@Nonnull HistoryAction historyAction, @Nullable CalculatorHistoryState currentState) { + public HistoryState doAction(@Nonnull HistoryAction historyAction, @Nullable HistoryState currentState) { return calculatorHistory.doAction(historyAction, currentState); } @Override - public void addState(@Nullable CalculatorHistoryState currentState) { + public void addState(@Nullable HistoryState currentState) { calculatorHistory.addState(currentState); } @Nonnull @Override - public List getStates() { + public List getStates() { return calculatorHistory.getStates(); } @Nonnull @Override - public List getStates(boolean includeIntermediateStates) { + public List getStates(boolean includeIntermediateStates) { return calculatorHistory.getStates(includeIntermediateStates); } @@ -149,12 +149,12 @@ public class AndroidCalculatorHistory implements CalculatorHistory { } @Nonnull - public List getSavedHistory() { + public List getSavedHistory() { return calculatorHistory.getSavedHistory(); } @Nonnull - public CalculatorHistoryState addSavedState(@Nonnull CalculatorHistoryState historyState) { + public HistoryState addSavedState(@Nonnull HistoryState historyState) { return calculatorHistory.addSavedState(historyState); } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryFragment.java b/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryFragment.java index a208a5c0..d1513d99 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryFragment.java @@ -88,9 +88,9 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul ********************************************************************** */ - public static final Comparator COMPARATOR = new Comparator() { + public static final Comparator COMPARATOR = new Comparator() { @Override - public int compare(CalculatorHistoryState state1, CalculatorHistoryState state2) { + public int compare(HistoryState state1, HistoryState state2) { if (state1.isSaved() == state2.isSaved()) { long l = state2.getTime() - state1.getTime(); return l > 0l ? 1 : (l < 0l ? -1 : 0); @@ -137,15 +137,15 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul ui = CalculatorApplication.getInstance().createFragmentHelper(fragmentType.getDefaultLayoutId(), fragmentType.getDefaultTitleResId(), false); } - public static boolean isAlreadySaved(@Nonnull CalculatorHistoryState historyState) { + public static boolean isAlreadySaved(@Nonnull HistoryState historyState) { if (historyState.isSaved()) throw new AssertionError(); boolean result = false; try { historyState.setSaved(true); - if (Collections.contains(historyState, Locator.getInstance().getHistory().getSavedHistory(), new Equalizer() { + if (Collections.contains(historyState, Locator.getInstance().getHistory().getSavedHistory(), new Equalizer() { @Override - public boolean areEqual(@Nullable CalculatorHistoryState first, @Nullable CalculatorHistoryState second) { + public boolean areEqual(@Nullable HistoryState first, @Nullable HistoryState second) { return first != null && second != null && first.getTime() == second.getTime() && first.getDisplayState().equals(second.getDisplayState()) && @@ -160,13 +160,13 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul return result; } - public static void useHistoryItem(@Nonnull final CalculatorHistoryState historyState) { + public static void useHistoryItem(@Nonnull final HistoryState historyState) { App.getVibrator().vibrate(); Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_history_state, historyState); } @Nonnull - public static String getHistoryText(@Nonnull CalculatorHistoryState state) { + public static String getHistoryText(@Nonnull HistoryState state) { final StringBuilder result = new StringBuilder(); result.append(state.getEditorState().getText()); result.append(getIdentitySign(state.getDisplayState().getJsclOperation())); @@ -211,7 +211,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul ui.onViewCreated(this, root); - adapter = new HistoryArrayAdapter(this.getActivity(), getItemLayoutId(), org.solovyev.android.calculator.R.id.history_item, new ArrayList(), showDatetime); + adapter = new HistoryArrayAdapter(this.getActivity(), getItemLayoutId(), org.solovyev.android.calculator.R.id.history_item, new ArrayList(), showDatetime); setListAdapter(adapter); final ListView lv = getListView(); @@ -232,14 +232,14 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul final int position, final long id) { - useHistoryItem((CalculatorHistoryState) parent.getItemAtPosition(position)); + useHistoryItem((HistoryState) parent.getItemAtPosition(position)); } }); lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) { - final CalculatorHistoryState historyState = (CalculatorHistoryState) parent.getItemAtPosition(position); + final HistoryState historyState = (HistoryState) parent.getItemAtPosition(position); final FragmentActivity activity = getActivity(); @@ -309,13 +309,13 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul protected abstract int getItemLayoutId(); private void updateAdapter() { - final List historyList = getHistoryList(); + final List historyList = getHistoryList(); - final ArrayAdapter adapter = getAdapter(); + final ArrayAdapter adapter = getAdapter(); try { adapter.setNotifyOnChange(false); adapter.clear(); - for (CalculatorHistoryState historyState : historyList) { + for (HistoryState historyState : historyList) { adapter.add(historyState); } } finally { @@ -326,26 +326,26 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul } @Nonnull - private List getHistoryList() { - final List calculatorHistoryStates = getHistoryItems(); + private List getHistoryList() { + final List historyStates = getHistoryItems(); - java.util.Collections.sort(calculatorHistoryStates, COMPARATOR); + java.util.Collections.sort(historyStates, COMPARATOR); - final FilterRulesChain filterRulesChain = new FilterRulesChain<>(); - filterRulesChain.addFilterRule(new JPredicate() { + final FilterRulesChain filterRulesChain = new FilterRulesChain<>(); + filterRulesChain.addFilterRule(new JPredicate() { @Override - public boolean apply(CalculatorHistoryState object) { + public boolean apply(HistoryState object) { return object == null || Strings.isEmpty(object.getEditorState().getText()); } }); - new Filter<>(filterRulesChain).filter(calculatorHistoryStates.iterator()); + new Filter<>(filterRulesChain).filter(historyStates.iterator()); - return calculatorHistoryStates; + return historyStates; } @Nonnull - protected abstract List getHistoryItems(); + protected abstract List getHistoryItems(); protected abstract void clearHistory(); diff --git a/app/src/main/java/org/solovyev/android/calculator/history/AbstractHistoryState.java b/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryState.java similarity index 87% rename from app/src/main/java/org/solovyev/android/calculator/history/AbstractHistoryState.java rename to app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryState.java index dc40d5c6..4d487cee 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/AbstractHistoryState.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryState.java @@ -29,12 +29,7 @@ import java.util.Date; import javax.annotation.Nullable; -/** - * User: serso - * Date: 10/15/11 - * Time: 1:45 PM - */ -public class AbstractHistoryState implements Cloneable { +public class BaseHistoryState implements Cloneable { @Element private long time = new Date().getTime(); @@ -82,16 +77,13 @@ public class AbstractHistoryState implements Cloneable { this.saved = saved; } + @SuppressWarnings("CloneDoesntDeclareCloneNotSupportedException") @Override - protected AbstractHistoryState clone() { - AbstractHistoryState clone; - + protected BaseHistoryState clone() { try { - clone = (AbstractHistoryState) super.clone(); + return (BaseHistoryState) super.clone(); } catch (CloneNotSupportedException e) { throw new UnsupportedOperationException(e); } - - return clone; } } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistory.java b/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistory.java index 0ccb6a41..48d9d1c1 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistory.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistory.java @@ -34,7 +34,7 @@ import javax.annotation.Nonnull; * Date: 20.09.12 * Time: 16:11 */ -public interface CalculatorHistory extends HistoryHelper, CalculatorEventListener { +public interface CalculatorHistory extends HistoryHelper, CalculatorEventListener { void load(); @@ -46,18 +46,18 @@ public interface CalculatorHistory extends HistoryHelper void clearSavedHistory(); - void removeSavedHistory(@Nonnull CalculatorHistoryState historyState); + void removeSavedHistory(@Nonnull HistoryState historyState); @Nonnull - List getSavedHistory(); + List getSavedHistory(); @Nonnull - CalculatorHistoryState addSavedState(@Nonnull CalculatorHistoryState historyState); + HistoryState addSavedState(@Nonnull HistoryState historyState); @Nonnull - List getStates(); + List getStates(); @Nonnull - List getStates(boolean includeIntermediateStates); + List getStates(boolean includeIntermediateStates); } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryImpl.java b/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryImpl.java index ac2f057d..9049e5c3 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryImpl.java @@ -47,10 +47,10 @@ public class CalculatorHistoryImpl implements CalculatorHistory { private final AtomicInteger counter = new AtomicInteger(0); @Nonnull - private final HistoryHelper history = SimpleHistoryHelper.newInstance(); + private final HistoryHelper history = SimpleHistoryHelper.newInstance(); @Nonnull - private final List savedHistory = new ArrayList(); + private final List savedHistory = new ArrayList(); @Nonnull private final CalculatorEventHolder lastEventData = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId()); @@ -70,7 +70,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory { } @Override - public CalculatorHistoryState getLastHistoryState() { + public HistoryState getLastHistoryState() { synchronized (history) { return this.history.getLastHistoryState(); } @@ -84,7 +84,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory { } @Override - public CalculatorHistoryState undo(@Nullable CalculatorHistoryState currentState) { + public HistoryState undo(@Nullable HistoryState currentState) { synchronized (history) { return history.undo(currentState); } @@ -96,7 +96,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory { } @Override - public CalculatorHistoryState redo(@Nullable CalculatorHistoryState currentState) { + public HistoryState redo(@Nullable HistoryState currentState) { synchronized (history) { return history.redo(currentState); } @@ -110,14 +110,14 @@ public class CalculatorHistoryImpl implements CalculatorHistory { } @Override - public CalculatorHistoryState doAction(@Nonnull HistoryAction historyAction, @Nullable CalculatorHistoryState currentState) { + public HistoryState doAction(@Nonnull HistoryAction historyAction, @Nullable HistoryState currentState) { synchronized (history) { return history.doAction(historyAction, currentState); } } @Override - public void addState(@Nullable CalculatorHistoryState currentState) { + public void addState(@Nullable HistoryState currentState) { synchronized (history) { history.addState(currentState); Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.history_state_added, currentState); @@ -126,7 +126,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory { @Nonnull @Override - public List getStates() { + public List getStates() { synchronized (history) { return history.getStates(); } @@ -134,17 +134,17 @@ public class CalculatorHistoryImpl implements CalculatorHistory { @Nonnull @Override - public List getStates(boolean includeIntermediateStates) { + public List getStates(boolean includeIntermediateStates) { synchronized (history) { if (includeIntermediateStates) { return getStates(); } else { - final List states = getStates(); + final List states = getStates(); - final List result = new LinkedList(); + final List result = new LinkedList(); - CalculatorHistoryState laterState = null; - for (CalculatorHistoryState state : org.solovyev.common.collections.Collections.reversed(states)) { + HistoryState laterState = null; + for (HistoryState state : org.solovyev.common.collections.Collections.reversed(states)) { if (laterState != null) { final String laterEditorText = laterState.getEditorState().getText(); final String editorText = state.getEditorState().getText(); @@ -187,17 +187,17 @@ public class CalculatorHistoryImpl implements CalculatorHistory { @Override @Nonnull - public List getSavedHistory() { + public List getSavedHistory() { return Collections.unmodifiableList(savedHistory); } @Override @Nonnull - public CalculatorHistoryState addSavedState(@Nonnull CalculatorHistoryState historyState) { + public HistoryState addSavedState(@Nonnull HistoryState historyState) { if (historyState.isSaved()) { return historyState; } else { - final CalculatorHistoryState savedState = historyState.clone(); + final HistoryState savedState = historyState.clone(); savedState.setId(counter.incrementAndGet()); savedState.setSaved(true); @@ -223,7 +223,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory { clearSavedHistory(); HistoryUtils.fromXml(xml, this.savedHistory); - for (CalculatorHistoryState historyState : savedHistory) { + for (HistoryState historyState : savedHistory) { historyState.setSaved(true); historyState.setId(counter.incrementAndGet()); } @@ -240,7 +240,7 @@ public class CalculatorHistoryImpl implements CalculatorHistory { } @Override - public void removeSavedHistory(@Nonnull CalculatorHistoryState historyState) { + public void removeSavedHistory(@Nonnull HistoryState historyState) { this.savedHistory.remove(historyState); } @@ -266,8 +266,8 @@ public class CalculatorHistoryImpl implements CalculatorHistory { if (lastEditorViewState != null) { final EditorState editorViewState = lastEditorViewState; final CalculatorDisplayChangeEventData displayChangeData = (CalculatorDisplayChangeEventData) data; - final CalculatorDisplayViewState displayViewState = displayChangeData.getNewValue(); - addState(CalculatorHistoryState.newInstance(editorViewState, displayViewState)); + final DisplayState displayViewState = displayChangeData.getNewValue(); + addState(HistoryState.create(editorViewState, displayViewState)); } } else { lastEditorViewState = null; diff --git a/app/src/main/java/org/solovyev/android/calculator/history/CalculatorDisplayHistoryState.java b/app/src/main/java/org/solovyev/android/calculator/history/DisplayHistoryState.java similarity index 75% rename from app/src/main/java/org/solovyev/android/calculator/history/CalculatorDisplayHistoryState.java rename to app/src/main/java/org/solovyev/android/calculator/history/DisplayHistoryState.java index 6af6ab91..338e357c 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/CalculatorDisplayHistoryState.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/DisplayHistoryState.java @@ -26,8 +26,7 @@ import org.simpleframework.xml.Element; import org.simpleframework.xml.Root; import org.simpleframework.xml.Transient; import org.solovyev.android.calculator.CalculatorDisplay; -import org.solovyev.android.calculator.CalculatorDisplayViewState; -import org.solovyev.android.calculator.CalculatorDisplayViewStateImpl; +import org.solovyev.android.calculator.DisplayState; import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.common.text.Strings; @@ -43,7 +42,7 @@ import jscl.math.Generic; */ @Root -public class CalculatorDisplayHistoryState implements Cloneable { +public class DisplayHistoryState implements Cloneable { @Transient private boolean valid = true; @@ -64,15 +63,15 @@ public class CalculatorDisplayHistoryState implements Cloneable { @Nullable private Generic genericResult; - private CalculatorDisplayHistoryState() { + private DisplayHistoryState() { // for xml } @Nonnull - public static CalculatorDisplayHistoryState newInstance(@Nonnull CalculatorDisplayViewState viewState) { - final CalculatorDisplayHistoryState result = new CalculatorDisplayHistoryState(); + public static DisplayHistoryState newInstance(@Nonnull DisplayState viewState) { + final DisplayHistoryState result = new DisplayHistoryState(); - result.editorState = EditorHistoryState.newInstance(viewState); + result.editorState = EditorHistoryState.create(viewState); result.valid = viewState.isValid(); result.jsclOperation = viewState.getOperation(); @@ -84,9 +83,9 @@ public class CalculatorDisplayHistoryState implements Cloneable { public void setValuesFromHistory(@Nonnull CalculatorDisplay display) { if (this.isValid()) { - display.setViewState(CalculatorDisplayViewStateImpl.newValidState(this.getJsclOperation(), this.getGenericResult(), Strings.getNotEmpty(this.getEditorState().getText(), ""), this.getEditorState().getCursorPosition())); + display.setViewState(DisplayState.createValid(this.getJsclOperation(), this.getGenericResult(), Strings.getNotEmpty(this.getEditorState().getText(), ""), this.getEditorState().getCursorPosition())); } else { - display.setViewState(CalculatorDisplayViewStateImpl.newErrorState(this.getJsclOperation(), Strings.getNotEmpty(this.getErrorMessage(), ""))); + display.setViewState(DisplayState.createError(this.getJsclOperation(), Strings.getNotEmpty(this.getErrorMessage(), ""))); } } @@ -121,7 +120,7 @@ public class CalculatorDisplayHistoryState implements Cloneable { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - CalculatorDisplayHistoryState that = (CalculatorDisplayHistoryState) o; + DisplayHistoryState that = (DisplayHistoryState) o; if (!editorState.equals(that.editorState)) return false; if (jsclOperation != that.jsclOperation) return false; @@ -147,9 +146,9 @@ public class CalculatorDisplayHistoryState implements Cloneable { } @Override - protected CalculatorDisplayHistoryState clone() { + protected DisplayHistoryState clone() { try { - final CalculatorDisplayHistoryState clone = (CalculatorDisplayHistoryState) super.clone(); + final DisplayHistoryState clone = (DisplayHistoryState) super.clone(); clone.editorState = this.editorState.clone(); diff --git a/app/src/main/java/org/solovyev/android/calculator/history/EditorHistoryState.java b/app/src/main/java/org/solovyev/android/calculator/history/EditorHistoryState.java index ff847f56..6c6a6c0c 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/EditorHistoryState.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/EditorHistoryState.java @@ -24,8 +24,8 @@ package org.solovyev.android.calculator.history; import org.simpleframework.xml.Element; import org.simpleframework.xml.Root; -import org.solovyev.android.calculator.CalculatorDisplayViewState; -import org.solovyev.android.calculator.CalculatorEditor; +import org.solovyev.android.calculator.DisplayState; +import org.solovyev.android.calculator.Editor; import org.solovyev.android.calculator.EditorState; import org.solovyev.common.text.Strings; @@ -47,17 +47,17 @@ public class EditorHistoryState implements Cloneable { } @Nonnull - public static EditorHistoryState newInstance(@Nonnull EditorState viewState) { + public static EditorHistoryState create(@Nonnull EditorState state) { final EditorHistoryState result = new EditorHistoryState(); - result.text = String.valueOf(viewState.getText()); - result.cursorPosition = viewState.getSelection(); + result.text = String.valueOf(state.getText()); + result.cursorPosition = state.getSelection(); return result; } @Nonnull - public static EditorHistoryState newInstance(@Nonnull CalculatorDisplayViewState viewState) { + public static EditorHistoryState create(@Nonnull DisplayState viewState) { final EditorHistoryState result = new EditorHistoryState(); result.text = viewState.getText(); @@ -66,7 +66,7 @@ public class EditorHistoryState implements Cloneable { return result; } - public void setValuesFromHistory(@Nonnull CalculatorEditor editor) { + public void setValuesFromHistory(@Nonnull Editor editor) { editor.setText(Strings.getNotEmpty(this.getText(), "")); editor.setSelection(this.getCursorPosition()); } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/History.java b/app/src/main/java/org/solovyev/android/calculator/history/History.java index 1ac19c92..9f2857ca 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/History.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/History.java @@ -37,13 +37,13 @@ import java.util.List; @Root public class History { - @ElementList(type = CalculatorHistoryState.class) - private List historyItems = new ArrayList(); + @ElementList(type = HistoryState.class) + private List historyItems = new ArrayList(); public History() { } - public List getHistoryItems() { + public List getHistoryItems() { return historyItems; } } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/HistoryArrayAdapter.java b/app/src/main/java/org/solovyev/android/calculator/history/HistoryArrayAdapter.java index 284a4b95..9f633347 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/HistoryArrayAdapter.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/HistoryArrayAdapter.java @@ -47,12 +47,12 @@ import static org.solovyev.android.calculator.history.BaseHistoryFragment.isAlre * Date: 12/18/11 * Time: 7:39 PM */ -public class HistoryArrayAdapter extends ArrayAdapter { +public class HistoryArrayAdapter extends ArrayAdapter { private static final int DATETIME_FORMAT = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_MONTH | DateUtils.FORMAT_ABBREV_TIME; private boolean showDatetime; - HistoryArrayAdapter(Context context, int resource, int textViewResourceId, @Nonnull List historyList, boolean showDatetime) { + HistoryArrayAdapter(Context context, int resource, int textViewResourceId, @Nonnull List historyList, boolean showDatetime) { super(context, resource, textViewResourceId, historyList); this.showDatetime = showDatetime; } @@ -61,7 +61,7 @@ public class HistoryArrayAdapter extends ArrayAdapter { public View getView(int position, View convertView, ViewGroup parent) { final ViewGroup result = (ViewGroup) super.getView(position, convertView, parent); - final CalculatorHistoryState state = getItem(position); + final HistoryState state = getItem(position); final TextView time = (TextView) result.findViewById(R.id.history_time); if (showDatetime) { diff --git a/app/src/main/java/org/solovyev/android/calculator/history/HistoryFragment.java b/app/src/main/java/org/solovyev/android/calculator/history/HistoryFragment.java index b5b55496..be8b4ee9 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/HistoryFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/HistoryFragment.java @@ -48,11 +48,11 @@ public class HistoryFragment extends BaseHistoryFragment { @Nonnull @Override - protected List getHistoryItems() { + protected List getHistoryItems() { final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); final boolean showIntermediateCalculations = Preferences.History.showIntermediateCalculations.getPreference(preferences); - final List historyStates = Locator.getInstance().getHistory().getStates(showIntermediateCalculations); - return new ArrayList(historyStates); + final List historyStates = Locator.getInstance().getHistory().getStates(showIntermediateCalculations); + return new ArrayList(historyStates); } @Override diff --git a/app/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuData.java b/app/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuData.java index 2bda1acc..9f07e202 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuData.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuData.java @@ -34,23 +34,23 @@ import javax.annotation.Nonnull; public class HistoryItemMenuData { @Nonnull - private final ArrayAdapter adapter; + private final ArrayAdapter adapter; @Nonnull - private final CalculatorHistoryState historyState; + private final HistoryState historyState; - public HistoryItemMenuData(@Nonnull CalculatorHistoryState historyState, ArrayAdapter adapter) { + public HistoryItemMenuData(@Nonnull HistoryState historyState, ArrayAdapter adapter) { this.historyState = historyState; this.adapter = adapter; } @Nonnull - public CalculatorHistoryState getHistoryState() { + public HistoryState getHistoryState() { return historyState; } @Nonnull - public ArrayAdapter getAdapter() { + public ArrayAdapter getAdapter() { return adapter; } } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuItem.java b/app/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuItem.java index b708b2c1..c4368da2 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuItem.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuItem.java @@ -57,8 +57,8 @@ public enum HistoryItemMenuItem implements LabeledMenuItem copy_expression(R.string.c_copy_expression) { @Override public void onClick(@Nonnull HistoryItemMenuData data, @Nonnull Context context) { - final CalculatorHistoryState calculatorHistoryState = data.getHistoryState(); - final String text = calculatorHistoryState.getEditorState().getText(); + final HistoryState historyState = data.getHistoryState(); + final String text = historyState.getEditorState().getText(); if (!Strings.isEmpty(text)) { final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); clipboard.setText(text); @@ -70,8 +70,8 @@ public enum HistoryItemMenuItem implements LabeledMenuItem copy_result(R.string.c_copy_result) { @Override public void onClick(@Nonnull HistoryItemMenuData data, @Nonnull Context context) { - final CalculatorHistoryState calculatorHistoryState = data.getHistoryState(); - final String text = calculatorHistoryState.getDisplayState().getEditorState().getText(); + final HistoryState historyState = data.getHistoryState(); + final String text = historyState.getDisplayState().getEditorState().getText(); if (!Strings.isEmpty(text)) { final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); clipboard.setText(text); @@ -83,7 +83,7 @@ public enum HistoryItemMenuItem implements LabeledMenuItem save(R.string.c_save) { @Override public void onClick(@Nonnull final HistoryItemMenuData data, @Nonnull final Context context) { - final CalculatorHistoryState historyState = data.getHistoryState(); + final HistoryState historyState = data.getHistoryState(); if (!historyState.isSaved()) { createEditHistoryDialog(data, context, true); } else { @@ -95,7 +95,7 @@ public enum HistoryItemMenuItem implements LabeledMenuItem edit(R.string.c_edit) { @Override public void onClick(@Nonnull final HistoryItemMenuData data, @Nonnull final Context context) { - final CalculatorHistoryState historyState = data.getHistoryState(); + final HistoryState historyState = data.getHistoryState(); if (historyState.isSaved()) { createEditHistoryDialog(data, context, false); } else { @@ -107,7 +107,7 @@ public enum HistoryItemMenuItem implements LabeledMenuItem remove(R.string.c_remove) { @Override public void onClick(@Nonnull HistoryItemMenuData data, @Nonnull Context context) { - final CalculatorHistoryState historyState = data.getHistoryState(); + final HistoryState historyState = data.getHistoryState(); if (historyState.isSaved()) { data.getAdapter().remove(historyState); Locator.getInstance().getHistory().removeSavedHistory(historyState); @@ -124,7 +124,7 @@ public enum HistoryItemMenuItem implements LabeledMenuItem } private static void createEditHistoryDialog(@Nonnull final HistoryItemMenuData data, @Nonnull final Context context, final boolean save) { - final CalculatorHistoryState historyState = data.getHistoryState(); + final HistoryState historyState = data.getHistoryState(); final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); final View editView = layoutInflater.inflate(R.layout.history_edit, null); @@ -142,7 +142,7 @@ public enum HistoryItemMenuItem implements LabeledMenuItem @Override public void onClick(DialogInterface dialog, int which) { if (save) { - final CalculatorHistoryState savedHistoryItem = Locator.getInstance().getHistory().addSavedState(historyState); + final HistoryState savedHistoryItem = Locator.getInstance().getHistory().addSavedState(historyState); savedHistoryItem.setComment(comment.getText().toString()); Locator.getInstance().getHistory().save(); // we don't need to add element to the adapter as adapter of another activity must be updated and not this diff --git a/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryState.java b/app/src/main/java/org/solovyev/android/calculator/history/HistoryState.java similarity index 52% rename from app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryState.java rename to app/src/main/java/org/solovyev/android/calculator/history/HistoryState.java index 72760cd4..d2b4c137 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryState.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/HistoryState.java @@ -25,20 +25,14 @@ package org.solovyev.android.calculator.history; import org.simpleframework.xml.Element; import org.simpleframework.xml.Root; import org.solovyev.android.calculator.CalculatorDisplay; -import org.solovyev.android.calculator.CalculatorDisplayViewState; -import org.solovyev.android.calculator.CalculatorEditor; +import org.solovyev.android.calculator.DisplayState; +import org.solovyev.android.calculator.Editor; import org.solovyev.android.calculator.EditorState; import javax.annotation.Nonnull; -/** - * User: serso - * Date: 9/11/11 - * Time: 12:16 AM - */ - @Root -public class CalculatorHistoryState extends AbstractHistoryState { +public class HistoryState extends BaseHistoryState { @Element @Nonnull @@ -46,35 +40,28 @@ public class CalculatorHistoryState extends AbstractHistoryState { @Element @Nonnull - private CalculatorDisplayHistoryState displayState; + private DisplayHistoryState displayState; - private CalculatorHistoryState() { + private HistoryState() { // for xml } - private CalculatorHistoryState(@Nonnull EditorHistoryState editorState, - @Nonnull CalculatorDisplayHistoryState displayState) { + private HistoryState(@Nonnull EditorHistoryState editorState, + @Nonnull DisplayHistoryState displayState) { this.editorState = editorState; this.displayState = displayState; } @Nonnull - public static CalculatorHistoryState newInstance(@Nonnull CalculatorEditor editor, - @Nonnull CalculatorDisplay display) { - final EditorState editorViewState = editor.getViewState(); - final CalculatorDisplayViewState displayViewState = display.getViewState(); - - return newInstance(editorViewState, displayViewState); + public static HistoryState create(@Nonnull Editor editor, + @Nonnull CalculatorDisplay display) { + return create(editor.getState(), display.getViewState()); } @Nonnull - public static CalculatorHistoryState newInstance(@Nonnull EditorState editorViewState, - @Nonnull CalculatorDisplayViewState displayViewState) { - final EditorHistoryState editorHistoryState = EditorHistoryState.newInstance(editorViewState); - - final CalculatorDisplayHistoryState displayHistoryState = CalculatorDisplayHistoryState.newInstance(displayViewState); - - return new CalculatorHistoryState(editorHistoryState, displayHistoryState); + public static HistoryState create(@Nonnull EditorState editorState, + @Nonnull DisplayState displayState) { + return new HistoryState(EditorHistoryState.create(editorState), DisplayHistoryState.newInstance(displayState)); } @Nonnull @@ -82,22 +69,14 @@ public class CalculatorHistoryState extends AbstractHistoryState { return editorState; } - public void setEditorState(@Nonnull EditorHistoryState editorState) { - this.editorState = editorState; - } - @Nonnull - public CalculatorDisplayHistoryState getDisplayState() { + public DisplayHistoryState getDisplayState() { return displayState; } - public void setDisplayState(@Nonnull CalculatorDisplayHistoryState displayState) { - this.displayState = displayState; - } - @Override public String toString() { - return "CalculatorHistoryState{" + + return "HistoryState{" + "editorState=" + editorState + ", displayState=" + displayState + '}'; @@ -108,7 +87,7 @@ public class CalculatorHistoryState extends AbstractHistoryState { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - CalculatorHistoryState that = (CalculatorHistoryState) o; + HistoryState that = (HistoryState) o; if (this.isSaved() != that.isSaved()) return false; if (this.getId() != that.getId()) return false; @@ -127,18 +106,18 @@ public class CalculatorHistoryState extends AbstractHistoryState { return result; } - public void setValuesFromHistory(@Nonnull CalculatorEditor editor, @Nonnull CalculatorDisplay display) { + public void setValuesFromHistory(@Nonnull Editor editor, @Nonnull CalculatorDisplay display) { this.getEditorState().setValuesFromHistory(editor); this.getDisplayState().setValuesFromHistory(display); } @Override - protected CalculatorHistoryState clone() { - final CalculatorHistoryState clone = (CalculatorHistoryState) super.clone(); + protected HistoryState clone() { + final HistoryState that = (HistoryState) super.clone(); - clone.editorState = this.editorState.clone(); - clone.displayState = this.displayState.clone(); + that.editorState = this.editorState.clone(); + that.displayState = this.displayState.clone(); - return clone; + return that; } } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/HistoryUtils.java b/app/src/main/java/org/solovyev/android/calculator/history/HistoryUtils.java index f2f246e8..12895ad7 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/HistoryUtils.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/HistoryUtils.java @@ -43,12 +43,12 @@ class HistoryUtils { throw new AssertionError(); } - public static void fromXml(@Nullable String xml, @Nonnull List historyItems) { + public static void fromXml(@Nullable String xml, @Nonnull List historyItems) { if (xml != null) { final Serializer serializer = new Persister(); try { final History history = serializer.read(History.class, xml); - for (CalculatorHistoryState historyItem : history.getHistoryItems()) { + for (HistoryState historyItem : history.getHistoryItems()) { historyItems.add(historyItem); } } catch (Exception e) { @@ -58,9 +58,9 @@ class HistoryUtils { } @Nonnull - public static String toXml(@Nonnull List historyItems) { + public static String toXml(@Nonnull List historyItems) { final History history = new History(); - for (CalculatorHistoryState historyState : historyItems) { + for (HistoryState historyState : historyItems) { if (historyState.isSaved()) { history.getHistoryItems().add(historyState); } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/SavedHistoryFragment.java b/app/src/main/java/org/solovyev/android/calculator/history/SavedHistoryFragment.java index 73a1b7a4..7d5f56ec 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/SavedHistoryFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/SavedHistoryFragment.java @@ -44,8 +44,8 @@ public class SavedHistoryFragment extends BaseHistoryFragment { @Nonnull @Override - protected List getHistoryItems() { - return new ArrayList(Locator.getInstance().getHistory().getSavedHistory()); + protected List getHistoryItems() { + return new ArrayList(Locator.getInstance().getHistory().getSavedHistory()); } @Override diff --git a/app/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenService.java b/app/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenService.java index 45299ddf..f66a84ca 100644 --- a/app/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenService.java +++ b/app/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenService.java @@ -122,7 +122,7 @@ public class CalculatorOnscreenService extends Service implements OnscreenViewLi view.show(); startCalculatorListening(); - view.updateEditorState(Locator.getInstance().getEditor().getViewState()); + view.updateEditorState(Locator.getInstance().getEditor().getState()); view.updateDisplayState(Locator.getInstance().getDisplay().getViewState()); viewCreated = true; diff --git a/app/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenView.java b/app/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenView.java index 3dcd4c41..1ee4d1f2 100644 --- a/app/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenView.java +++ b/app/src/main/java/org/solovyev/android/calculator/onscreen/CalculatorOnscreenView.java @@ -180,7 +180,7 @@ public class CalculatorOnscreenView { } } - public void updateDisplayState(@Nonnull CalculatorDisplayViewState displayState) { + public void updateDisplayState(@Nonnull DisplayState displayState) { checkInit(); displayView.setState(displayState); } diff --git a/app/src/main/java/org/solovyev/android/calculator/view/LongClickEraser.java b/app/src/main/java/org/solovyev/android/calculator/view/LongClickEraser.java index 96d3ba8d..184919e8 100644 --- a/app/src/main/java/org/solovyev/android/calculator/view/LongClickEraser.java +++ b/app/src/main/java/org/solovyev/android/calculator/view/LongClickEraser.java @@ -5,7 +5,7 @@ import android.view.MotionEvent; import android.view.View; import org.solovyev.android.calculator.Calculator; -import org.solovyev.android.calculator.CalculatorEditor; +import org.solovyev.android.calculator.Editor; import org.solovyev.android.calculator.EditorState; import org.solovyev.android.calculator.Locator; @@ -22,7 +22,7 @@ public final class LongClickEraser implements View.OnTouchListener { private final GestureDetector gestureDetector; @Nonnull - private final CalculatorEditor editor = Locator.getInstance().getEditor(); + private final Editor editor = Locator.getInstance().getEditor(); @Nonnull private final Calculator calculator = Locator.getInstance().getCalculator(); diff --git a/app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java b/app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java index b1b23d07..fbceeccb 100644 --- a/app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java +++ b/app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java @@ -107,8 +107,8 @@ public class CalculatorWidget extends AppWidgetProvider { private void updateWidget(@Nonnull Context context, @Nonnull AppWidgetManager manager, @Nonnull int[] widgetIds) { - final EditorState editorState = Locator.getInstance().getEditor().getViewState(); - final CalculatorDisplayViewState displayState = Locator.getInstance().getDisplay().getViewState(); + final EditorState editorState = Locator.getInstance().getEditor().getState(); + final DisplayState displayState = Locator.getInstance().getDisplay().getViewState(); final Resources resources = context.getResources(); final SimpleTheme theme = App.getWidgetTheme().resolveThemeFor(App.getTheme()); @@ -195,7 +195,7 @@ public class CalculatorWidget extends AppWidgetProvider { } } - private void updateDisplayState(@Nonnull Context context, @Nonnull RemoteViews views, @Nonnull CalculatorDisplayViewState displayState, @Nonnull SimpleTheme theme) { + private void updateDisplayState(@Nonnull Context context, @Nonnull RemoteViews views, @Nonnull DisplayState displayState, @Nonnull SimpleTheme theme) { final boolean error = !displayState.isValid(); if (!error) { views.setTextViewText(R.id.calculator_display, displayState.getText()); diff --git a/app/src/test/java/org/solovyev/android/calculator/AndroidCalculatorEditorViewTest.java b/app/src/test/java/org/solovyev/android/calculator/AndroidEditorViewTest.java similarity index 98% rename from app/src/test/java/org/solovyev/android/calculator/AndroidCalculatorEditorViewTest.java rename to app/src/test/java/org/solovyev/android/calculator/AndroidEditorViewTest.java index df612475..a3b44e3b 100644 --- a/app/src/test/java/org/solovyev/android/calculator/AndroidCalculatorEditorViewTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/AndroidEditorViewTest.java @@ -47,7 +47,7 @@ import java.util.concurrent.atomic.AtomicBoolean; @Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) @RunWith(value = RobolectricGradleTestRunner.class) -public class AndroidCalculatorEditorViewTest { +public class AndroidEditorViewTest { @BeforeClass public static void staticSetUp() throws Exception { diff --git a/app/src/test/java/org/solovyev/android/calculator/CalculatorDisplayViewStateImplTest.java b/app/src/test/java/org/solovyev/android/calculator/CalculatorDisplayViewStateImplTest.java index 8a01ee0e..80903e2c 100644 --- a/app/src/test/java/org/solovyev/android/calculator/CalculatorDisplayViewStateImplTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/CalculatorDisplayViewStateImplTest.java @@ -36,11 +36,11 @@ public class CalculatorDisplayViewStateImplTest { @Test public void testSerializable() throws Exception { - CalculatorTestUtils.testSerialization(CalculatorDisplayViewStateImpl.newValidState(JsclOperation.numeric, null, "test", 3)); - CalculatorTestUtils.testSerialization(CalculatorDisplayViewStateImpl.newValidState(JsclOperation.numeric, Expression.valueOf("3"), "test", 3)); - CalculatorTestUtils.testSerialization(CalculatorDisplayViewStateImpl.newValidState(JsclOperation.simplify, Expression.valueOf("3+3"), "test", 3)); - CalculatorTestUtils.testSerialization(CalculatorDisplayViewStateImpl.newDefaultInstance()); - CalculatorTestUtils.testSerialization(CalculatorDisplayViewStateImpl.newErrorState(JsclOperation.numeric, "ertert")); + CalculatorTestUtils.testSerialization(DisplayState.createValid(JsclOperation.numeric, null, "test", 3)); + CalculatorTestUtils.testSerialization(DisplayState.createValid(JsclOperation.numeric, Expression.valueOf("3"), "test", 3)); + CalculatorTestUtils.testSerialization(DisplayState.createValid(JsclOperation.simplify, Expression.valueOf("3+3"), "test", 3)); + CalculatorTestUtils.testSerialization(DisplayState.empty()); + CalculatorTestUtils.testSerialization(DisplayState.createError(JsclOperation.numeric, "ertert")); } } diff --git a/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java b/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java index de0f0504..31a44363 100644 --- a/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java +++ b/app/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java @@ -76,7 +76,6 @@ public class CalculatorTestUtils { public static void initViews(@Nonnull Context context) { final EditorView editor = new EditorView(context); - editor.init(); Locator.getInstance().getEditor().setView(editor); final AndroidCalculatorDisplayView display = new AndroidCalculatorDisplayView(context); @@ -105,7 +104,7 @@ public class CalculatorTestUtils { public static void assertEval(@Nonnull String expected, @Nonnull String expression, @Nonnull JsclOperation operation) { final Calculator calculator = Locator.getInstance().getCalculator(); - Locator.getInstance().getDisplay().setViewState(CalculatorDisplayViewStateImpl.newDefaultInstance()); + Locator.getInstance().getDisplay().setViewState(DisplayState.empty()); final CountDownLatch latch = new CountDownLatch(1); final TestCalculatorEventListener calculatorEventListener = new TestCalculatorEventListener(latch); @@ -161,7 +160,7 @@ public class CalculatorTestUtils { public static void assertError(@Nonnull String expression, @Nonnull JsclOperation operation) { final Calculator calculator = Locator.getInstance().getCalculator(); - Locator.getInstance().getDisplay().setViewState(CalculatorDisplayViewStateImpl.newDefaultInstance()); + Locator.getInstance().getDisplay().setViewState(DisplayState.empty()); final CountDownLatch latch = new CountDownLatch(1); final TestCalculatorEventListener calculatorEventListener = new TestCalculatorEventListener(latch); @@ -190,7 +189,7 @@ public class CalculatorTestUtils { @Nullable private CalculatorEventData calculatorEventData; @Nullable - private volatile CalculatorDisplayViewState result = null; + private volatile DisplayState result = null; public TestCalculatorEventListener(@Nonnull CountDownLatch latch) { this.latch = latch; @@ -230,7 +229,7 @@ public class CalculatorTestUtils { } @Nullable - public CalculatorDisplayViewState getResult() { + public DisplayState getResult() { return result; } } diff --git a/app/src/test/java/org/solovyev/android/calculator/CalculatorEditorImplTest.java b/app/src/test/java/org/solovyev/android/calculator/EditorTest.java similarity index 52% rename from app/src/test/java/org/solovyev/android/calculator/CalculatorEditorImplTest.java rename to app/src/test/java/org/solovyev/android/calculator/EditorTest.java index 939e0a08..690dfa81 100644 --- a/app/src/test/java/org/solovyev/android/calculator/CalculatorEditorImplTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/EditorTest.java @@ -33,198 +33,198 @@ import javax.annotation.Nonnull; * Date: 21.09.12 * Time: 12:44 */ -public class CalculatorEditorImplTest extends AbstractCalculatorTest { +public class EditorTest extends AbstractCalculatorTest { @Nonnull - private CalculatorEditor calculatorEditor; + private Editor editor; @Before public void setUp() throws Exception { super.setUp(); - this.calculatorEditor = new CalculatorEditorImpl(Locator.getInstance().getCalculator(), null); + this.editor = new Editor(Locator.getInstance().getCalculator(), null); } @Test public void testInsert() throws Exception { - CalculatorEditorViewState viewState = this.calculatorEditor.getViewState(); + EditorState viewState = this.editor.getState(); Assert.assertEquals("", viewState.getText()); Assert.assertEquals(0, viewState.getSelection()); - viewState = this.calculatorEditor.insert(""); + viewState = this.editor.insert(""); Assert.assertEquals("", viewState.getText()); Assert.assertEquals(0, viewState.getSelection()); - viewState = this.calculatorEditor.insert("test"); + viewState = this.editor.insert("test"); Assert.assertEquals("test", viewState.getText()); Assert.assertEquals(4, viewState.getSelection()); - viewState = this.calculatorEditor.insert("test"); + viewState = this.editor.insert("test"); Assert.assertEquals("testtest", viewState.getText()); Assert.assertEquals(8, viewState.getSelection()); - viewState = this.calculatorEditor.insert(""); + viewState = this.editor.insert(""); Assert.assertEquals("testtest", viewState.getText()); Assert.assertEquals(8, viewState.getSelection()); - viewState = this.calculatorEditor.insert("1234567890"); + viewState = this.editor.insert("1234567890"); Assert.assertEquals("testtest1234567890", viewState.getText()); Assert.assertEquals(18, viewState.getSelection()); - viewState = this.calculatorEditor.moveCursorLeft(); - viewState = this.calculatorEditor.insert("9"); + viewState = this.editor.moveCursorLeft(); + viewState = this.editor.insert("9"); Assert.assertEquals("testtest12345678990", viewState.getText()); Assert.assertEquals(18, viewState.getSelection()); - viewState = this.calculatorEditor.setCursorOnStart(); - viewState = this.calculatorEditor.insert("9"); + viewState = this.editor.setCursorOnStart(); + viewState = this.editor.insert("9"); Assert.assertEquals("9testtest12345678990", viewState.getText()); Assert.assertEquals(1, viewState.getSelection()); - viewState = this.calculatorEditor.erase(); - viewState = this.calculatorEditor.insert("9"); + viewState = this.editor.erase(); + viewState = this.editor.insert("9"); Assert.assertEquals("9testtest12345678990", viewState.getText()); Assert.assertEquals(1, viewState.getSelection()); - viewState = this.calculatorEditor.insert("öäü"); + viewState = this.editor.insert("öäü"); Assert.assertEquals("9öäütesttest12345678990", viewState.getText()); - this.calculatorEditor.setCursorOnEnd(); - viewState = this.calculatorEditor.insert("öäü"); + this.editor.setCursorOnEnd(); + viewState = this.editor.insert("öäü"); Assert.assertEquals("9öäütesttest12345678990öäü", viewState.getText()); } @Test public void testErase() throws Exception { - this.calculatorEditor.setText(""); - this.calculatorEditor.erase(); + this.editor.setText(""); + this.editor.erase(); - Assert.assertEquals("", this.calculatorEditor.getViewState().getText()); + Assert.assertEquals("", this.editor.getState().getText()); - this.calculatorEditor.setText("test"); - this.calculatorEditor.erase(); - Assert.assertEquals("tes", this.calculatorEditor.getViewState().getText()); + this.editor.setText("test"); + this.editor.erase(); + Assert.assertEquals("tes", this.editor.getState().getText()); - this.calculatorEditor.erase(); - Assert.assertEquals("te", this.calculatorEditor.getViewState().getText()); + this.editor.erase(); + Assert.assertEquals("te", this.editor.getState().getText()); - this.calculatorEditor.erase(); - Assert.assertEquals("t", this.calculatorEditor.getViewState().getText()); + this.editor.erase(); + Assert.assertEquals("t", this.editor.getState().getText()); - this.calculatorEditor.erase(); - Assert.assertEquals("", this.calculatorEditor.getViewState().getText()); + this.editor.erase(); + Assert.assertEquals("", this.editor.getState().getText()); - this.calculatorEditor.erase(); - Assert.assertEquals("", this.calculatorEditor.getViewState().getText()); + this.editor.erase(); + Assert.assertEquals("", this.editor.getState().getText()); - this.calculatorEditor.setText("1234"); - this.calculatorEditor.moveCursorLeft(); - this.calculatorEditor.erase(); - Assert.assertEquals("124", this.calculatorEditor.getViewState().getText()); + this.editor.setText("1234"); + this.editor.moveCursorLeft(); + this.editor.erase(); + Assert.assertEquals("124", this.editor.getState().getText()); - this.calculatorEditor.erase(); - Assert.assertEquals("14", this.calculatorEditor.getViewState().getText()); + this.editor.erase(); + Assert.assertEquals("14", this.editor.getState().getText()); - this.calculatorEditor.erase(); - Assert.assertEquals("4", this.calculatorEditor.getViewState().getText()); + this.editor.erase(); + Assert.assertEquals("4", this.editor.getState().getText()); - this.calculatorEditor.setText("1"); - this.calculatorEditor.moveCursorLeft(); - this.calculatorEditor.erase(); - Assert.assertEquals("1", this.calculatorEditor.getViewState().getText()); + this.editor.setText("1"); + this.editor.moveCursorLeft(); + this.editor.erase(); + Assert.assertEquals("1", this.editor.getState().getText()); } @Test public void testMoveSelection() throws Exception { - this.calculatorEditor.setText(""); + this.editor.setText(""); - CalculatorEditorViewState viewState = this.calculatorEditor.moveSelection(0); + EditorState viewState = this.editor.moveSelection(0); Assert.assertEquals(0, viewState.getSelection()); - viewState = this.calculatorEditor.moveSelection(2); + viewState = this.editor.moveSelection(2); Assert.assertEquals(0, viewState.getSelection()); - viewState = this.calculatorEditor.moveSelection(100); + viewState = this.editor.moveSelection(100); Assert.assertEquals(0, viewState.getSelection()); - viewState = this.calculatorEditor.moveSelection(-3); + viewState = this.editor.moveSelection(-3); Assert.assertEquals(0, viewState.getSelection()); - viewState = this.calculatorEditor.moveSelection(-100); + viewState = this.editor.moveSelection(-100); Assert.assertEquals(0, viewState.getSelection()); - viewState = this.calculatorEditor.setText("0123456789"); + viewState = this.editor.setText("0123456789"); - viewState = this.calculatorEditor.moveSelection(0); + viewState = this.editor.moveSelection(0); Assert.assertEquals(10, viewState.getSelection()); - viewState = this.calculatorEditor.moveSelection(1); + viewState = this.editor.moveSelection(1); Assert.assertEquals(10, viewState.getSelection()); - viewState = this.calculatorEditor.moveSelection(-2); + viewState = this.editor.moveSelection(-2); Assert.assertEquals(8, viewState.getSelection()); - viewState = this.calculatorEditor.moveSelection(1); + viewState = this.editor.moveSelection(1); Assert.assertEquals(9, viewState.getSelection()); - viewState = this.calculatorEditor.moveSelection(-9); + viewState = this.editor.moveSelection(-9); Assert.assertEquals(0, viewState.getSelection()); - viewState = this.calculatorEditor.moveSelection(-10); + viewState = this.editor.moveSelection(-10); Assert.assertEquals(0, viewState.getSelection()); - viewState = this.calculatorEditor.moveSelection(2); + viewState = this.editor.moveSelection(2); Assert.assertEquals(2, viewState.getSelection()); - viewState = this.calculatorEditor.moveSelection(2); + viewState = this.editor.moveSelection(2); Assert.assertEquals(4, viewState.getSelection()); - viewState = this.calculatorEditor.moveSelection(-6); + viewState = this.editor.moveSelection(-6); Assert.assertEquals(0, viewState.getSelection()); } @Test public void testSetText() throws Exception { - CalculatorEditorViewState viewState = this.calculatorEditor.setText("test"); + EditorState viewState = this.editor.setText("test"); Assert.assertEquals("test", viewState.getText()); Assert.assertEquals(4, viewState.getSelection()); - viewState = this.calculatorEditor.setText("testtest"); + viewState = this.editor.setText("testtest"); Assert.assertEquals("testtest", viewState.getText()); Assert.assertEquals(8, viewState.getSelection()); - viewState = this.calculatorEditor.setText(""); + viewState = this.editor.setText(""); Assert.assertEquals("", viewState.getText()); Assert.assertEquals(0, viewState.getSelection()); - viewState = this.calculatorEditor.setText("testtest", 0); + viewState = this.editor.setText("testtest", 0); Assert.assertEquals("testtest", viewState.getText()); Assert.assertEquals(0, viewState.getSelection()); - viewState = this.calculatorEditor.setText("testtest", 2); + viewState = this.editor.setText("testtest", 2); Assert.assertEquals("testtest", viewState.getText()); Assert.assertEquals(2, viewState.getSelection()); - viewState = this.calculatorEditor.setText("", 0); + viewState = this.editor.setText("", 0); Assert.assertEquals("", viewState.getText()); Assert.assertEquals(0, viewState.getSelection()); - viewState = this.calculatorEditor.setText("", 3); + viewState = this.editor.setText("", 3); Assert.assertEquals("", viewState.getText()); Assert.assertEquals(0, viewState.getSelection()); - viewState = this.calculatorEditor.setText("", -3); + viewState = this.editor.setText("", -3); Assert.assertEquals("", viewState.getText()); Assert.assertEquals(0, viewState.getSelection()); - viewState = this.calculatorEditor.setText("test"); + viewState = this.editor.setText("test"); Assert.assertEquals("test", viewState.getText()); Assert.assertEquals(4, viewState.getSelection()); - viewState = this.calculatorEditor.setText("", 2); + viewState = this.editor.setText("", 2); Assert.assertEquals("", viewState.getText()); Assert.assertEquals(0, viewState.getSelection()); } diff --git a/app/src/test/java/org/solovyev/android/calculator/history/CalculatorHistoryImplTest.java b/app/src/test/java/org/solovyev/android/calculator/history/CalculatorHistoryImplTest.java index 88050f49..32234b9e 100644 --- a/app/src/test/java/org/solovyev/android/calculator/history/CalculatorHistoryImplTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/history/CalculatorHistoryImplTest.java @@ -25,7 +25,7 @@ package org.solovyev.android.calculator.history; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; -import org.solovyev.android.calculator.CalculatorDisplayViewStateImpl; +import org.solovyev.android.calculator.DisplayState; import org.solovyev.android.calculator.EditorState; import org.solovyev.android.calculator.CalculatorTestUtils; import org.solovyev.android.calculator.Locator; @@ -64,13 +64,13 @@ public class CalculatorHistoryImplTest { addState(calculatorHistory, "2354"); addState(calculatorHistory, "23547"); - final List states = calculatorHistory.getStates(false); + final List states = calculatorHistory.getStates(false); Assert.assertEquals(2, states.size()); Assert.assertEquals("23547", states.get(1).getEditorState().getText()); Assert.assertEquals("123+3", states.get(0).getEditorState().getText()); } private void addState(@Nonnull CalculatorHistory calculatorHistory, @Nonnull String text) { - calculatorHistory.addState(CalculatorHistoryState.newInstance(EditorState.create(text, 3), CalculatorDisplayViewStateImpl.newDefaultInstance())); + calculatorHistory.addState(HistoryState.create(EditorState.create(text, 3), DisplayState.empty())); } } diff --git a/app/src/test/java/org/solovyev/android/calculator/history/HistoryUtilsTest.java b/app/src/test/java/org/solovyev/android/calculator/history/HistoryUtilsTest.java index c680dd84..4fd1fffe 100644 --- a/app/src/test/java/org/solovyev/android/calculator/history/HistoryUtilsTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/history/HistoryUtilsTest.java @@ -24,8 +24,7 @@ package org.solovyev.android.calculator.history; import org.junit.Assert; import org.junit.Test; -import org.solovyev.android.calculator.CalculatorDisplayViewState; -import org.solovyev.android.calculator.CalculatorDisplayViewStateImpl; +import org.solovyev.android.calculator.DisplayState; import org.solovyev.android.calculator.EditorState; import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.common.Objects; @@ -137,13 +136,13 @@ public class HistoryUtilsTest { public void testToXml() throws Exception { final Date date = new Date(100000000); - HistoryHelper history = SimpleHistoryHelper.newInstance(); + HistoryHelper history = SimpleHistoryHelper.newInstance(); - CalculatorDisplayViewState calculatorDisplay = CalculatorDisplayViewStateImpl.newErrorState(JsclOperation.simplify, "Error"); + DisplayState calculatorDisplay = DisplayState.createError(JsclOperation.simplify, "Error"); CalculatorEditorViewState calculatorEditor = EditorState.create("1+1", 3); - CalculatorHistoryState state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay); + HistoryState state = HistoryState.newInstance(calculatorEditor, calculatorDisplay); state.setTime(date.getTime()); history.addState(state); @@ -154,29 +153,29 @@ public class HistoryUtilsTest { assertEquals(toXml1, HistoryUtils.toXml(history.getStates())); - calculatorDisplay = CalculatorDisplayViewStateImpl.newValidState(JsclOperation.numeric, null, "5/6", 3); + calculatorDisplay = DisplayState.createValid(JsclOperation.numeric, null, "5/6", 3); calculatorEditor = EditorState.create("5/6", 2); - state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay); + state = HistoryState.newInstance(calculatorEditor, calculatorDisplay); state.setSaved(true); state.setTime(date.getTime()); history.addState(state); - calculatorDisplay = CalculatorDisplayViewStateImpl.newErrorState(JsclOperation.elementary, "Error"); + calculatorDisplay = DisplayState.createError(JsclOperation.elementary, "Error"); calculatorEditor = EditorState.create("", 1); - state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay); + state = HistoryState.newInstance(calculatorEditor, calculatorDisplay); state.setSaved(true); state.setTime(date.getTime()); history.addState(state); - calculatorDisplay = CalculatorDisplayViewStateImpl.newValidState(JsclOperation.numeric, null, "4+5/35sin(41)+dfdsfsdfs", 1); + calculatorDisplay = DisplayState.createValid(JsclOperation.numeric, null, "4+5/35sin(41)+dfdsfsdfs", 1); calculatorEditor = EditorState.create("4+5/35sin(41)+dfdsfsdfs", 0); - state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay); + state = HistoryState.newInstance(calculatorEditor, calculatorDisplay); state.setSaved(true); state.setTime(date.getTime()); history.addState(state); @@ -184,23 +183,23 @@ public class HistoryUtilsTest { String xml = HistoryUtils.toXml(history.getStates()); assertEquals(toXml2, xml); - final List fromXml = new ArrayList(); - final HistoryHelper historyFromXml = SimpleHistoryHelper.newInstance(); + final List fromXml = new ArrayList(); + final HistoryHelper historyFromXml = SimpleHistoryHelper.newInstance(); HistoryUtils.fromXml(xml, fromXml); - for (CalculatorHistoryState historyState : fromXml) { + for (HistoryState historyState : fromXml) { historyFromXml.addState(historyState); } assertEquals(history.getStates().size(), historyFromXml.getStates().size()); - for (CalculatorHistoryState historyState : history.getStates()) { + for (HistoryState historyState : history.getStates()) { historyState.setId(0); historyState.setSaved(true); } - for (CalculatorHistoryState historyState : historyFromXml.getStates()) { + for (HistoryState historyState : historyFromXml.getStates()) { historyState.setId(0); historyState.setSaved(true); } - Assert.assertTrue(Objects.areEqual(history.getStates(), historyFromXml.getStates(), new CollectionEqualizer(null))); + Assert.assertTrue(Objects.areEqual(history.getStates(), historyFromXml.getStates(), new CollectionEqualizer(null))); } }