From 5efa9cd3a080ae3c1d90a877b14e9ead9d179450 Mon Sep 17 00:00:00 2001 From: serso Date: Thu, 14 Jan 2016 10:32:04 +0100 Subject: [PATCH] Take into account grouping separator --- .../android/calculator/history/History.java | 58 ++++++++++++++----- .../calculator/history/HistoryTest.java | 44 ++++++++++++-- 2 files changed, 83 insertions(+), 19 deletions(-) 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 f75bf5f4..dd754822 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 @@ -26,6 +26,7 @@ import android.app.Application; import android.content.SharedPreferences; import android.os.Handler; import android.support.annotation.NonNull; +import android.text.TextUtils; import com.google.common.base.Strings; import com.squareup.otto.Bus; @@ -41,6 +42,7 @@ import org.solovyev.android.calculator.DisplayState; import org.solovyev.android.calculator.Editor; import org.solovyev.android.calculator.EditorState; import org.solovyev.android.calculator.Locator; +import org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences; import org.solovyev.android.io.FileLoader; import org.solovyev.android.io.FileSaver; @@ -114,16 +116,6 @@ public class History { return states; } - @NonNull - private File getSavedHistoryFile() { - return new File(application.getFilesDir(), "history-saved.json"); - } - - @NonNull - private File getRecentHistoryFile() { - return new File(application.getFilesDir(), "history-recent.json"); - } - @Nonnull private static List loadStates(@Nonnull File file) { if (!file.exists()) { @@ -142,13 +134,16 @@ public class History { } private static boolean isIntermediate(@Nonnull String olderText, - @Nonnull String newerText) { - if (isEmpty(olderText)) { + @Nonnull String newerText, + @NonNull String groupingSeparator) { + if (TextUtils.isEmpty(olderText)) { return true; } - if (isEmpty(newerText)) { + if (TextUtils.isEmpty(newerText)) { return false; } + olderText = trimGroupingSeparators(olderText, groupingSeparator); + newerText = trimGroupingSeparators(newerText, groupingSeparator); final int diff = newerText.length() - olderText.length(); if (diff >= 1) { @@ -162,6 +157,39 @@ public class History { return false; } + @NonNull + static String trimGroupingSeparators(@NonNull String text, @NonNull String groupingSeparator) { + if (TextUtils.isEmpty(groupingSeparator)) { + return text; + } + Check.isTrue(groupingSeparator.length() == 1); + final StringBuilder sb = new StringBuilder(text.length()); + for (int i = 0; i < text.length(); i++) { + if (i == 0 || i == text.length() - 1) { + // grouping separator can't be the first and the last character + sb.append(text.charAt(i)); + continue; + } + if (Character.isDigit(text.charAt(i - 1)) && text.charAt(i) == groupingSeparator.charAt(0) && Character.isDigit(text.charAt(i + 1))) { + // grouping separator => skip + continue; + } + sb.append(text.charAt(i)); + + } + return sb.toString(); + } + + @NonNull + private File getSavedHistoryFile() { + return new File(application.getFilesDir(), "history-saved.json"); + } + + @NonNull + private File getRecentHistoryFile() { + return new File(application.getFilesDir(), "history-recent.json"); + } + private void migrateOldHistory() { try { final String xml = preferences.getString("org.solovyev.android.calculator.CalculatorModel_history", null); @@ -226,6 +254,8 @@ public class History { final List result = new LinkedList<>(); + final String groupingSeparator = Preferences.groupingSeparator.getPreference(preferences); + final List states = recent.asList(); final int statesCount = states.size(); for (int i = 1; i < statesCount; i++) { @@ -233,7 +263,7 @@ public class History { final HistoryState newerState = states.get(i); final String olderText = olderState.editor.getTextString(); final String newerText = newerState.editor.getTextString(); - if (!isIntermediate(olderText, newerText)) { + if (!isIntermediate(olderText, newerText, groupingSeparator)) { result.add(0, olderState); } } diff --git a/app/src/test/java/org/solovyev/android/calculator/history/HistoryTest.java b/app/src/test/java/org/solovyev/android/calculator/history/HistoryTest.java index a7d31fff..98d57769 100644 --- a/app/src/test/java/org/solovyev/android/calculator/history/HistoryTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/history/HistoryTest.java @@ -30,7 +30,6 @@ import com.squareup.otto.Bus; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Mockito; import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; @@ -48,6 +47,11 @@ import javax.annotation.Nonnull; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.groupingSeparator; @Config(constants = BuildConfig.class, sdk = CalculatorTestRunner.SUPPORTED_SDK) @RunWith(RobolectricGradleTestRunner.class) @@ -57,10 +61,10 @@ public class HistoryTest { @Before public void setUp() throws Exception { - history = new History(Mockito.mock(Bus.class), Mockito.mock(Executor.class)); - history.handler = Mockito.mock(Handler.class); - history.preferences = Mockito.mock(SharedPreferences.class); - history.editor = Mockito.mock(Editor.class); + history = new History(mock(Bus.class), mock(Executor.class)); + history.handler = mock(Handler.class); + history.preferences = mock(SharedPreferences.class); + history.editor = mock(Editor.class); history.application = RuntimeEnvironment.application; } @@ -86,6 +90,36 @@ public class HistoryTest { assertEquals("123+3", states.get(1).editor.getTextString()); } + @Test + public void testRecentHistoryShouldTakeIntoAccountGroupingSeparator() throws Exception { + when(history.preferences.contains(eq(groupingSeparator.getKey()))).thenReturn(true); + when(history.preferences.getString(eq(groupingSeparator.getKey()), anyString())).thenReturn(" "); + addState("1"); + addState("12"); + addState("123"); + addState("1 234"); + addState("12 345"); + + List states = history.getRecent(); + assertEquals(1, states.size()); + assertEquals("12 345", states.get(0).editor.getTextString()); + history.clearRecent(); + + when(history.preferences.getString(eq(groupingSeparator.getKey()), anyString())).thenReturn("'"); + addState("1"); + addState("12"); + addState("123"); + addState("1'234"); + addState("12'345"); + addState("12 345"); + + states = history.getRecent(); + assertEquals(2, states.size()); + assertEquals("12 345", states.get(0).editor.getTextString()); + assertEquals("12'345", states.get(1).editor.getTextString()); + } + + @Test public void testRecentHistoryShouldNotContainEmptyStates() throws Exception { addState("");