From 3fd671877ddea4efa01bd25b442288378f912d56 Mon Sep 17 00:00:00 2001 From: serso Date: Mon, 11 Jan 2016 17:20:57 +0100 Subject: [PATCH] History refactor --- .../solovyev/android/calculator/BaseUi.java | 2 +- .../android/calculator/Calculator.java | 9 +- .../android/calculator/CalculatorImpl.java | 69 ++----- .../solovyev/android/calculator/Editor.java | 6 +- .../history/BaseHistoryFragment.java | 4 +- .../calculator/history/CalculatorHistory.java | 185 ++++++++---------- .../history/HistoryDragProcessor.java | 32 +-- .../calculator/history/HistoryFragment.java | 6 +- .../calculator/history/HistoryList.java | 79 ++++++++ .../calculator/history/HistoryState.java | 75 +++++++ ...oryState.java => OldBaseHistoryState.java} | 6 +- .../calculator/history/OldHistoryState.java | 31 +-- .../history/SavedHistoryFragment.java | 4 +- .../history/CalculatorHistoryTest.java | 4 +- 14 files changed, 280 insertions(+), 232 deletions(-) create mode 100644 app/src/main/java/org/solovyev/android/calculator/history/HistoryList.java create mode 100644 app/src/main/java/org/solovyev/android/calculator/history/HistoryState.java rename app/src/main/java/org/solovyev/android/calculator/history/{BaseHistoryState.java => OldBaseHistoryState.java} (92%) 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 98dce673..00d4fda4 100644 --- a/app/src/main/java/org/solovyev/android/calculator/BaseUi.java +++ b/app/src/main/java/org/solovyev/android/calculator/BaseUi.java @@ -165,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<>(); 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 2031e7c0..0e22c317 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Calculator.java +++ b/app/src/main/java/org/solovyev/android/calculator/Calculator.java @@ -22,21 +22,20 @@ package org.solovyev.android.calculator; -import jscl.NumeralBase; -import jscl.math.Generic; -import org.solovyev.android.calculator.history.OldHistoryState; import org.solovyev.android.calculator.jscl.JsclOperation; -import org.solovyev.common.history.HistoryControl; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import jscl.NumeralBase; +import jscl.math.Generic; + /** * User: Solovyev_S * Date: 20.09.12 * Time: 16:38 */ -public interface Calculator extends CalculatorEventContainer, HistoryControl { +public interface Calculator extends CalculatorEventContainer { void init(); 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 0c0b7ec4..e8aa9645 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java @@ -23,22 +23,14 @@ package org.solovyev.android.calculator; import android.text.TextUtils; + import com.squareup.otto.Subscribe; -import jscl.AbstractJsclArithmeticException; -import jscl.NumeralBase; -import jscl.NumeralBaseException; -import jscl.math.Generic; -import jscl.math.function.Function; -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.OldHistoryState; import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.model.Var; import org.solovyev.android.calculator.text.TextProcessor; import org.solovyev.android.calculator.units.CalculatorNumeralBase; -import org.solovyev.common.history.HistoryAction; import org.solovyev.common.msg.ListMessageRegistry; import org.solovyev.common.msg.Message; import org.solovyev.common.msg.MessageRegistry; @@ -47,14 +39,24 @@ import org.solovyev.common.text.Strings; import org.solovyev.common.units.ConversionException; import org.solovyev.common.units.Conversions; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicLong; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import jscl.AbstractJsclArithmeticException; +import jscl.NumeralBase; +import jscl.NumeralBaseException; +import jscl.math.Generic; +import jscl.math.function.Function; +import jscl.math.function.IConstant; +import jscl.math.operator.Operator; +import jscl.text.ParseInterruptedException; + /** * User: Solovyev_S * Date: 20.09.12 @@ -507,6 +509,11 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { CalculatorVarsRegistry.saveVariable(varsRegistry, builder, ansVar, this, false); } + public void onHistoryChanged(@Nonnull CalculatorHistory.ChangedEvent e) { + getEditor().setState(e.state.getEditor()); + getDisplay().setState(e.state.getDisplay()); + } + @Override public void onCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData, @Nonnull CalculatorEventType calculatorEventType, @Nullable Object data) { @@ -548,44 +555,6 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { } } - /* - ********************************************************************** - * - * HISTORY - * - ********************************************************************** - */ - - @Override - public void doHistoryAction(@Nonnull HistoryAction historyAction) { - final CalculatorHistory history = Locator.getInstance().getHistory(); - if (history.isActionAvailable(historyAction)) { - final OldHistoryState newState = history.doAction(historyAction, getCurrentHistoryState()); - if (newState != null) { - setCurrentHistoryState(newState); - } - } - } - - @Nonnull - @Override - public OldHistoryState getCurrentHistoryState() { - return OldHistoryState.create(getEditor(), getDisplay()); - } - - @Override - public void setCurrentHistoryState(@Nonnull OldHistoryState editorHistoryState) { - editorHistoryState.setValuesFromHistory(getEditor(), getDisplay()); - } - - /* - ********************************************************************** - * - * OTHER - * - ********************************************************************** - */ - @Nonnull private Editor getEditor() { return Locator.getInstance().getEditor(); diff --git a/app/src/main/java/org/solovyev/android/calculator/Editor.java b/app/src/main/java/org/solovyev/android/calculator/Editor.java index 83ce958f..6b74ce04 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Editor.java +++ b/app/src/main/java/org/solovyev/android/calculator/Editor.java @@ -23,10 +23,8 @@ package org.solovyev.android.calculator; import org.solovyev.android.Check; -import org.solovyev.android.calculator.history.OldEditorHistoryState; 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; @@ -104,9 +102,9 @@ public class Editor { } @Nonnull - public EditorState setState(@Nonnull OldEditorHistoryState state) { + public EditorState setState(@Nonnull EditorState state) { Check.isMainThread(); - return setText(Strings.getNotEmpty(state.getText(), ""), state.getCursorPosition()); + return setText(state.getTextString(), state.selection); } @Nonnull 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 9d4ab363..d2fec4db 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 @@ -109,7 +109,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul boolean result = false; try { historyState.setSaved(true); - if (Collections.contains(historyState, Locator.getInstance().getHistory().getSavedOldHistory(), new Equalizer() { + if (Collections.contains(historyState, Locator.getInstance().getHistory().getSavedHistory(), new Equalizer() { @Override public boolean areEqual(@Nullable OldHistoryState first, @Nullable OldHistoryState second) { return first != null && second != null && @@ -401,7 +401,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul } @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/CalculatorHistory.java b/app/src/main/java/org/solovyev/android/calculator/history/CalculatorHistory.java index 100f1b9b..9f72396b 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 @@ -24,30 +24,33 @@ package org.solovyev.android.calculator.history; import android.content.SharedPreferences; import android.text.TextUtils; -import com.squareup.otto.Subscribe; -import org.solovyev.android.Check; -import org.solovyev.android.calculator.*; -import org.solovyev.common.history.HistoryAction; -import org.solovyev.common.history.HistoryHelper; -import org.solovyev.common.history.SimpleHistoryHelper; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; +import com.squareup.otto.Subscribe; + +import org.solovyev.android.Check; +import org.solovyev.android.calculator.App; +import org.solovyev.android.calculator.CalculatorEventType; +import org.solovyev.android.calculator.Display; +import org.solovyev.android.calculator.Editor; +import org.solovyev.android.calculator.EditorState; +import org.solovyev.android.calculator.Locator; + +import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + public class CalculatorHistory { private final AtomicInteger counter = new AtomicInteger(0); - @Nonnull - private final HistoryHelper history = SimpleHistoryHelper.newInstance(); - + private final HistoryList current = new HistoryList(); @Nonnull - private final OldHistory savedOldHistory = new OldHistory(); - + private final List saved = new ArrayList<>(); @Nullable private EditorState lastEditorState; @@ -61,12 +64,7 @@ public class CalculatorHistory { }); } - private void init() { - Check.isNotMainThread(); - migrateOldHistory(); - } - - private void migrateOldHistory() { + private static void migrateOldHistory() { final SharedPreferences preferences = App.getPreferences(); final String xml = preferences.getString("org.solovyev.android.calculator.CalculatorModel_history", null); if (TextUtils.isEmpty(xml)) { @@ -79,77 +77,60 @@ public class CalculatorHistory { } for (OldHistoryState state : history.getItems()) { state.setSaved(true); - state.setId(counter.incrementAndGet()); - savedOldHistory.add(state); } } + private void init() { + Check.isNotMainThread(); + migrateOldHistory(); + } + public boolean isEmpty() { Check.isMainThread(); - return history.isEmpty(); + return current.isEmpty(); } - public boolean isActionAvailable(@Nonnull HistoryAction action) { + public void addCurrentState(@Nonnull HistoryState state) { Check.isMainThread(); - return history.isActionAvailable(action); + current.addState(state); + Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.history_state_added, state); + // todo serso: schedule save } - public OldHistoryState doAction(@Nonnull HistoryAction action, @Nullable OldHistoryState state) { + public void addSavedState(@Nonnull HistoryState state) { Check.isMainThread(); - return history.doAction(action, state); - } - - public void addState(@Nullable OldHistoryState currentState) { - Check.isMainThread(); - history.addState(currentState); - Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.history_state_added, currentState); + saved.add(state); + // todo serso: schedule save } @Nonnull - public List getStates() { + public List getCurrentHistory() { Check.isMainThread(); - return history.getStates(); - } - @Nonnull - public List getStates(boolean includeIntermediateStates) { - Check.isMainThread(); - if (includeIntermediateStates) { - return getStates(); - } else { - final List states = getStates(); + final List result = new LinkedList<>(); - final List result = new LinkedList<>(); - - OldHistoryState laterState = null; - for (OldHistoryState state : org.solovyev.common.collections.Collections.reversed(states)) { - if (laterState != null) { - final String laterEditorText = laterState.getEditorState().getText(); - final String editorText = state.getEditorState().getText(); - if (laterEditorText != null && editorText != null && isIntermediate(laterEditorText, editorText)) { - // intermediate result => skip from add - } else { - result.add(0, state); - } - } else { - result.add(0, state); - } - - laterState = state; - } - - return result; + final List states = current.asList(); + for (int i = 1; i < states.size(); i++) { + final HistoryState newerState = states.get(i); + final HistoryState olderState = states.get(i - 1); + final String newerText = newerState.editor.getTextString(); + final String olderText = olderState.editor.getTextString(); + if (!isIntermediate(olderText, newerText)) { + result.add(0, olderState); } } + return result; + } - private boolean isIntermediate(@Nonnull String laterEditorText, - @Nonnull String editorText) { - if (Math.abs(laterEditorText.length() - editorText.length()) <= 1) { - if (laterEditorText.length() > editorText.length()) { - return laterEditorText.startsWith(editorText); - } else { - return editorText.startsWith(laterEditorText); - } + private boolean isIntermediate(@Nonnull String newerText, + @Nonnull String olderText) { + final int diff = newerText.length() - olderText.length(); + if (diff == 1) { + return newerText.startsWith(olderText); + } else if (diff == -1) { + return olderText.startsWith(newerText); + } else if (diff == 0) { + return newerText.equals(olderText); } return false; @@ -157,53 +138,36 @@ public class CalculatorHistory { public void clear() { Check.isMainThread(); - history.clear(); + current.clear(); } - @Nonnull - public List getSavedOldHistory() { - return Collections.unmodifiableList(savedOldHistory.getItems()); - } - - @Nonnull - public OldHistoryState addSavedState(@Nonnull OldHistoryState state) { - if (state.isSaved()) { - return state; - } else { - final OldHistoryState savedState = state.clone(); - - savedState.setId(counter.incrementAndGet()); - savedState.setSaved(true); - - savedOldHistory.add(savedState); - - return savedState; + public void undo() { + final HistoryState state = current.undo(); + if (state == null) { + return; } + App.getBus().post(new ChangedEvent(state)); } - public void load() { + public void redo() { + final HistoryState state = current.redo(); + if (state == null) { + return; + } + App.getBus().post(new ChangedEvent(state)); } - public void save() { - final SharedPreferences settings = App.getPreferences(); - final SharedPreferences.Editor editor = settings.edit(); - editor.putString("org.solovyev.android.calculator.CalculatorModel_history", toXml()); - editor.apply(); - } - - public String toXml() { - return savedOldHistory.toXml(); + @Nonnull + public List getSavedHistory() { + return Collections.unmodifiableList(saved); } public void clearSavedHistory() { - savedOldHistory.clear(); - save(); + saved.clear(); } - public void removeSavedHistory(@Nonnull OldHistoryState historyState) { - historyState.setSaved(false); - this.savedOldHistory.remove(historyState); - save(); + public void removeSavedHistory(@Nonnull HistoryState state) { + saved.remove(state); } @Subscribe @@ -219,7 +183,16 @@ public class CalculatorHistory { if (lastEditorState.sequence != e.newState.getSequence()) { return; } - addState(OldHistoryState.create(lastEditorState, e.newState)); + addCurrentState(HistoryState.newBuilder(lastEditorState, e.newState).build()); lastEditorState = null; } + + public static final class ChangedEvent { + @Nonnull + public final HistoryState state; + + public ChangedEvent(@Nonnull HistoryState state) { + this.state = state; + } + } } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/HistoryDragProcessor.java b/app/src/main/java/org/solovyev/android/calculator/history/HistoryDragProcessor.java index d4f20db1..cc64aa9c 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/HistoryDragProcessor.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/HistoryDragProcessor.java @@ -25,41 +25,25 @@ package org.solovyev.android.calculator.history; import android.graphics.PointF; import android.view.MotionEvent; +import org.solovyev.android.calculator.Locator; import org.solovyev.android.views.dragbutton.DragButton; import org.solovyev.android.views.dragbutton.DragDirection; import org.solovyev.android.views.dragbutton.SimpleDragListener; -import org.solovyev.common.history.HistoryAction; -import org.solovyev.common.history.HistoryControl; import javax.annotation.Nonnull; -import javax.annotation.Nullable; public class HistoryDragProcessor implements SimpleDragListener.DragProcessor { - @Nonnull - private final HistoryControl historyControl; - - public HistoryDragProcessor(@Nonnull HistoryControl historyControl) { - this.historyControl = historyControl; - } - @Override public boolean processDragEvent(@Nonnull DragDirection direction, @Nonnull DragButton button, @Nonnull PointF startPoint, @Nonnull MotionEvent motionEvent) { - final HistoryAction action = getActionFromDirection(direction); - if (action != null) { - historyControl.doHistoryAction(action); - return true; + switch (direction) { + case up: + Locator.getInstance().getHistory().undo(); + return true; + case down: + Locator.getInstance().getHistory().redo(); + return true; } return false; } - - @Nullable - private HistoryAction getActionFromDirection(@Nonnull DragDirection direction) { - if (direction == DragDirection.up) { - return HistoryAction.undo; - } else if (direction == DragDirection.down) { - return HistoryAction.redo; - } - return null; - } } 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 8726810a..274589f0 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 @@ -27,7 +27,7 @@ import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.R; import javax.annotation.Nonnull; -import java.util.ArrayList; + import java.util.List; public class HistoryFragment extends BaseHistoryFragment { @@ -43,8 +43,8 @@ public class HistoryFragment extends BaseHistoryFragment { @Nonnull @Override - protected List getHistoryItems() { - return new ArrayList<>(Locator.getInstance().getHistory().getStates(false)); + protected List getHistoryItems() { + return Locator.getInstance().getHistory().getCurrentHistory(); } @Override diff --git a/app/src/main/java/org/solovyev/android/calculator/history/HistoryList.java b/app/src/main/java/org/solovyev/android/calculator/history/HistoryList.java new file mode 100644 index 00000000..3c4563ad --- /dev/null +++ b/app/src/main/java/org/solovyev/android/calculator/history/HistoryList.java @@ -0,0 +1,79 @@ +package org.solovyev.android.calculator.history; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +import org.solovyev.android.Check; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public class HistoryList { + + private static final int MAX_HISTORY = 20; + + @NonNull + private final List list = new LinkedList<>(); + private int current = -1; + + public void addState(@NonNull HistoryState e) { + Check.isMainThread(); + while (current != list.size() - 1) { + list.remove(list.size() - 1); + } + list.add(e); + current++; + if (list.size() > MAX_HISTORY) { + current--; + list.remove(0); + } + } + + @Nullable + public HistoryState getCurrent() { + Check.isMainThread(); + if (current == -1) { + return null; + } + return list.get(current); + } + + @Nullable + public HistoryState redo() { + Check.isMainThread(); + if (current < list.size() - 1) { + current++; + } + return getCurrent(); + } + + @Nullable + public HistoryState undo() { + Check.isMainThread(); + if (current == -1) { + return null; + } + if (current > 0) { + current--; + return getCurrent(); + } + return null; + } + + + public void clear() { + Check.isMainThread(); + list.clear(); + current = -1; + } + + public boolean isEmpty() { + return list.isEmpty(); + } + + @NonNull + public List asList() { + return Collections.unmodifiableList(list); + } +} diff --git a/app/src/main/java/org/solovyev/android/calculator/history/HistoryState.java b/app/src/main/java/org/solovyev/android/calculator/history/HistoryState.java new file mode 100644 index 00000000..0eb2b3f6 --- /dev/null +++ b/app/src/main/java/org/solovyev/android/calculator/history/HistoryState.java @@ -0,0 +1,75 @@ +package org.solovyev.android.calculator.history; + +import android.support.annotation.Nullable; + +import org.solovyev.android.Check; +import org.solovyev.android.calculator.DisplayState; +import org.solovyev.android.calculator.EditorState; + +import javax.annotation.Nonnull; + +public class HistoryState { + + @Nonnull + protected final EditorState editor; + @Nonnull + protected final DisplayState display; + protected long time; + @Nullable + protected String comment; + + private HistoryState(@Nonnull EditorState editor, @Nonnull DisplayState display) { + this.editor = editor; + this.display = display; + } + + @Nonnull + public static Builder newBuilder(@Nonnull EditorState editor, @Nonnull DisplayState display) { + return new Builder(editor, display); + } + + @Nonnull + public EditorState getEditor() { + return editor; + } + + @Nonnull + public DisplayState getDisplay() { + return display; + } + + public long getTime() { + return time; + } + + @Nullable + public String getComment() { + return comment; + } + + public static final class Builder extends HistoryState { + + private boolean built; + + private Builder(@Nonnull EditorState editor, @Nonnull DisplayState display) { + super(editor, display); + setTime(System.currentTimeMillis()); + } + + public void setTime(long time) { + Check.isTrue(!built); + this.time = time; + } + + public void setComment(@Nullable String comment) { + Check.isTrue(!built); + this.comment = comment; + } + + @Nonnull + public HistoryState build() { + built = true; + return this; + } + } +} diff --git a/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryState.java b/app/src/main/java/org/solovyev/android/calculator/history/OldBaseHistoryState.java similarity index 92% rename from app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryState.java rename to app/src/main/java/org/solovyev/android/calculator/history/OldBaseHistoryState.java index 4d487cee..52805291 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryState.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/OldBaseHistoryState.java @@ -29,7 +29,7 @@ import java.util.Date; import javax.annotation.Nullable; -public class BaseHistoryState implements Cloneable { +public class OldBaseHistoryState implements Cloneable { @Element private long time = new Date().getTime(); @@ -79,9 +79,9 @@ public class BaseHistoryState implements Cloneable { @SuppressWarnings("CloneDoesntDeclareCloneNotSupportedException") @Override - protected BaseHistoryState clone() { + protected OldBaseHistoryState clone() { try { - return (BaseHistoryState) super.clone(); + return (OldBaseHistoryState) super.clone(); } catch (CloneNotSupportedException e) { throw new UnsupportedOperationException(e); } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/OldHistoryState.java b/app/src/main/java/org/solovyev/android/calculator/history/OldHistoryState.java index 31b61434..3ea03dbb 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/OldHistoryState.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/OldHistoryState.java @@ -32,7 +32,7 @@ import org.solovyev.android.calculator.EditorState; import javax.annotation.Nonnull; @Root(name = "HistoryState") -public class OldHistoryState extends BaseHistoryState { +public class OldHistoryState extends OldBaseHistoryState { @Element @Nonnull @@ -82,35 +82,6 @@ public class OldHistoryState extends BaseHistoryState { '}'; } - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - OldHistoryState that = (OldHistoryState) o; - - if (this.isSaved() != that.isSaved()) return false; - if (this.getId() != that.getId()) return false; - if (!displayState.equals(that.displayState)) return false; - if (!editorState.equals(that.editorState)) return false; - - return true; - } - - @Override - public int hashCode() { - int result = Boolean.valueOf(isSaved()).hashCode(); - result = 31 * result + getId(); - result = 31 * result + editorState.hashCode(); - result = 31 * result + displayState.hashCode(); - return result; - } - - public void setValuesFromHistory(@Nonnull Editor editor, @Nonnull Display display) { - this.getEditorState().setValuesFromHistory(editor); - this.getDisplayState().setValuesFromHistory(display); - } - @Override protected OldHistoryState clone() { final OldHistoryState that = (OldHistoryState) super.clone(); 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 7e2119b9..96628489 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().getSavedOldHistory()); + protected List getHistoryItems() { + return new ArrayList<>(Locator.getInstance().getHistory().getSavedHistory()); } @Override diff --git a/app/src/test/java/org/solovyev/android/calculator/history/CalculatorHistoryTest.java b/app/src/test/java/org/solovyev/android/calculator/history/CalculatorHistoryTest.java index ad29a9da..7a52530e 100644 --- a/app/src/test/java/org/solovyev/android/calculator/history/CalculatorHistoryTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/history/CalculatorHistoryTest.java @@ -62,13 +62,13 @@ public class CalculatorHistoryTest { addState(calculatorHistory, "2354"); addState(calculatorHistory, "23547"); - final List states = calculatorHistory.getStates(false); + final List states = calculatorHistory.getCurrentHistory(); 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(OldHistoryState.create(EditorState.create(text, 3), DisplayState.empty())); + calculatorHistory.addCurrentState(OldHistoryState.create(EditorState.create(text, 3), DisplayState.empty())); } }