From bd91238b1407be9271624ffd552a83d40bfc30dc Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 24 Jan 2016 23:40:56 +0100 Subject: [PATCH] Memory improvements --- .../org/solovyev/android/calculator/App.java | 11 +++ .../android/calculator/FunctionsRegistry.java | 2 +- .../solovyev/android/calculator/Keyboard.java | 5 +- .../android/calculator/OperatorCategory.java | 58 ++++--------- .../calculator/ToJsclTextProcessor.java | 12 ++- .../android/calculator/VarCategory.java | 37 ++------- .../android/calculator/history/History.java | 2 +- .../android/calculator/math/MathType.java | 82 +++++++++---------- .../math/edit/CalculatorVarsActivity.java | 2 +- .../math/edit/OperatorsActivity.java | 2 +- .../text/FromJsclSimplifyTextProcessor.java | 16 ++-- .../calculator/view/TextHighlighter.java | 3 +- 12 files changed, 102 insertions(+), 130 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/App.java b/app/src/main/java/org/solovyev/android/calculator/App.java index 692f7e31..3ab3a4f2 100644 --- a/app/src/main/java/org/solovyev/android/calculator/App.java +++ b/app/src/main/java/org/solovyev/android/calculator/App.java @@ -352,4 +352,15 @@ public final class App { } return null; } + + @Nullable + public static String find(@Nonnull List tokens, @Nonnull String text, int position) { + for (int i = 0; i < tokens.size(); i++) { + final String token = tokens.get(i); + if (text.startsWith(token, position)) { + return token; + } + } + return null; + } } \ No newline at end of file diff --git a/app/src/main/java/org/solovyev/android/calculator/FunctionsRegistry.java b/app/src/main/java/org/solovyev/android/calculator/FunctionsRegistry.java index db5482ac..e01be6f8 100644 --- a/app/src/main/java/org/solovyev/android/calculator/FunctionsRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/FunctionsRegistry.java @@ -176,7 +176,7 @@ public class FunctionsRegistry extends BaseEntitiesRegistry getCategoriesByTabOrder() { - final List result = Arrays.asList(OperatorCategory.values()); - - java.util.Collections.sort(result, new Comparator() { - @Override - public int compare(OperatorCategory category, OperatorCategory category1) { - return category.tabOrder - category1.tabOrder; - } - }); - - // todo serso: current solution (as creating operators is not implemented yet) - result.remove(my); - - return result; - } - - - public abstract boolean isInCategory(@Nonnull Operator operator); @Override diff --git a/app/src/main/java/org/solovyev/android/calculator/ToJsclTextProcessor.java b/app/src/main/java/org/solovyev/android/calculator/ToJsclTextProcessor.java index f41b72ae..efd2b277 100644 --- a/app/src/main/java/org/solovyev/android/calculator/ToJsclTextProcessor.java +++ b/app/src/main/java/org/solovyev/android/calculator/ToJsclTextProcessor.java @@ -26,7 +26,6 @@ import jscl.math.function.Function; import jscl.math.function.IConstant; import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.text.TextProcessor; -import org.solovyev.common.collections.Collections; import org.solovyev.common.msg.MessageType; import org.solovyev.common.search.StartsWithFinder; @@ -56,20 +55,19 @@ public class ToJsclTextProcessor implements TextProcessor 0) { diff --git a/app/src/main/java/org/solovyev/android/calculator/VarCategory.java b/app/src/main/java/org/solovyev/android/calculator/VarCategory.java index 1c7d4229..2fe7bc3f 100644 --- a/app/src/main/java/org/solovyev/android/calculator/VarCategory.java +++ b/app/src/main/java/org/solovyev/android/calculator/VarCategory.java @@ -26,49 +26,30 @@ import android.support.annotation.StringRes; import jscl.math.function.IConstant; import javax.annotation.Nonnull; -import java.util.Arrays; -import java.util.Comparator; -import java.util.List; public enum VarCategory implements Category { - system(100, R.string.c_var_system) { - @Override - public boolean isInCategory(@Nonnull IConstant var) { - return var.isSystem(); - } - }, - - my(0, R.string.c_var_my) { + my(R.string.c_var_my) { @Override public boolean isInCategory(@Nonnull IConstant var) { return !var.isSystem(); } + }, + + system(R.string.c_var_system) { + @Override + public boolean isInCategory(@Nonnull IConstant var) { + return var.isSystem(); + } }; - private final int tabOrder; @StringRes private final int title; - VarCategory(int tabOrder, @StringRes int title) { - this.tabOrder = tabOrder; + VarCategory(@StringRes int title) { this.title = title; } - @Nonnull - public static List getCategoriesByTabOrder() { - final List result = Arrays.asList(VarCategory.values()); - - java.util.Collections.sort(result, new Comparator() { - @Override - public int compare(VarCategory category, VarCategory category1) { - return category.tabOrder - category1.tabOrder; - } - }); - - return result; - } - public abstract boolean isInCategory(@Nonnull IConstant var); 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 8306bc18..bd3acb4e 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 @@ -242,7 +242,7 @@ public class History { private void onRecentChanged(@Nonnull Object event) { handler.removeCallbacks(writeRecent); - handler.postDelayed(writeRecent, 500); + handler.postDelayed(writeRecent, 5000); bus.post(event); } diff --git a/app/src/main/java/org/solovyev/android/calculator/math/MathType.java b/app/src/main/java/org/solovyev/android/calculator/math/MathType.java index ebbf5d19..d54dd243 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/MathType.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/MathType.java @@ -25,9 +25,9 @@ package org.solovyev.android.calculator.math; import jscl.JsclMathEngine; import jscl.NumeralBase; import jscl.math.function.Constants; +import org.solovyev.android.calculator.App; import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.ParseException; -import org.solovyev.common.JPredicate; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -259,55 +259,48 @@ public enum MathType { */ @Nonnull public static Result getType(@Nonnull String text, int i, boolean hexMode) { + return getType(text, i, hexMode, new Result()); + } + + @Nonnull + public static Result getType(@Nonnull String text, int i, boolean hexMode, @Nonnull Result result) { if (i < 0) { throw new IllegalArgumentException("I must be more or equals to 0."); } else if (i >= text.length() && i != 0) { throw new IllegalArgumentException("I must be less than size of text."); } else if (i == 0 && text.length() == 0) { - return new Result(MathType.text, text); + return result.set(MathType.text, text); } - - final StartsWithFinder startsWithFinder = new StartsWithFinder(text, i); - - for (MathType mathType : getMathTypesByPriority()) { - final String s = find(mathType.getTokens(), startsWithFinder); + final List mathTypes = getMathTypesByPriority(); + for (int j = 0; j < mathTypes.size(); j++) { + final MathType mathType = mathTypes.get(j); + final String s = App.find(mathType.getTokens(), text, i); if (s == null) { continue; } if (s.length() > 1) { - return new Result(mathType, s); + return result.set(mathType, s); } if (hexMode || JsclMathEngine.getInstance().getNumeralBase() == NumeralBase.hex) { final Character ch = s.charAt(0); if (NumeralBase.hex.getAcceptableCharacters().contains(ch)) { - return new Result(MathType.digit, s); + return result.set(MathType.digit, s); } } if (mathType == MathType.grouping_separator) { - if (i + 1 < text.length() && getType(text, i + 1, hexMode).type == MathType.digit) { - return new Result(mathType, s); + if (i + 1 < text.length() && getType(text, i + 1, hexMode, result).type == MathType.digit) { + return result.set(mathType, s); } continue; } - return new Result(mathType, s); + return result.set(mathType, s); } - return new Result(MathType.text, text.substring(i)); - } - - @Nullable - private static String find(@Nonnull List list, @Nonnull JPredicate predicate) { - for (int i = 0; i < list.size(); i++) { - final String token = list.get(i); - if(predicate.apply(token)) { - return token; - } - } - return null; + return result.set(MathType.text, text.substring(i)); } @Nonnull @@ -388,15 +381,23 @@ public enum MathType { public static class Result { @Nonnull - public final MathType type; + public MathType type; @Nonnull - public final String match; + public String match; public Result(@Nonnull MathType type, @Nonnull String match) { - this.type = type; + set(type, match); + } + @Nonnull + Result set(@Nonnull MathType type, @Nonnull String match) { + this.type = type; this.match = match; + return this; + } + + public Result() { } public int processToJscl(@Nonnull StringBuilder result, int i) throws ParseException { @@ -404,24 +405,23 @@ public enum MathType { } } - private static class StartsWithFinder implements JPredicate { + public static class Results { + @Nonnull + private final List list = new ArrayList<>(); @Nonnull - private final String targetString; - private int i; - - public StartsWithFinder(@Nonnull String targetString, int i) { - this.targetString = targetString; - this.i = i; + public Result obtain() { + if (list.isEmpty()) { + return new Result(); + } + return list.remove(list.size() - 1); } - @Override - public boolean apply(@Nullable String s) { - return s != null && targetString.startsWith(s, i); - } - - public void setI(int i) { - this.i = i; + public void release(@Nullable Result result) { + if (result == null) { + return; + } + list.add(result); } } } diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java index 71c2a39c..abbb097b 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java @@ -57,7 +57,7 @@ public class CalculatorVarsActivity extends BaseActivity implements CalculatorEv final CalculatorFragmentType fragmentType = CalculatorFragmentType.variables; - for (VarCategory category : VarCategory.getCategoriesByTabOrder()) { + for (VarCategory category : VarCategory.values()) { final Bundle fragmentParameters; diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsActivity.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsActivity.java index 655a467a..5e3a1181 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsActivity.java @@ -47,7 +47,7 @@ public class OperatorsActivity extends BaseActivity implements CalculatorEventLi final CalculatorFragmentType fragmentType = CalculatorFragmentType.operators; - for (OperatorCategory category : OperatorCategory.getCategoriesByTabOrder()) { + for (OperatorCategory category : OperatorCategory.values()) { ui.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), BaseEntitiesFragment.createBundleFor(category.name()), category.title(), R.id.main_layout); } } diff --git a/app/src/main/java/org/solovyev/android/calculator/text/FromJsclSimplifyTextProcessor.java b/app/src/main/java/org/solovyev/android/calculator/text/FromJsclSimplifyTextProcessor.java index 874e706b..72b11f48 100644 --- a/app/src/main/java/org/solovyev/android/calculator/text/FromJsclSimplifyTextProcessor.java +++ b/app/src/main/java/org/solovyev/android/calculator/text/FromJsclSimplifyTextProcessor.java @@ -42,25 +42,28 @@ public class FromJsclSimplifyTextProcessor implements TextProcessor