From c000a525db2a4f1cecc9988730b285b285876f88 Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 24 Jan 2016 22:52:55 +0100 Subject: [PATCH] Optimize work with lists --- .../org/solovyev/android/calculator/App.java | 43 +++++++++---- .../calculator/CalculatorFixableError.java | 12 ++-- .../android/calculator/OperatorCategory.java | 4 +- .../calculator/ToJsclTextProcessor.java | 8 +-- .../android/calculator/VarCategory.java | 4 +- .../function/EditFunctionFragment.java | 2 +- .../android/calculator/math/MathType.java | 7 +-- .../android/fragments/FragmentUtils.java | 46 -------------- .../main/java/jscl/text/ExceptionsPool.java | 7 +++ .../main/java/jscl/text/ParseException.java | 4 +- jscl/src/main/java/jscl/text/ParserUtils.java | 3 +- .../common/math/AbstractMathRegistry.java | 61 ++++++++++++------- 12 files changed, 97 insertions(+), 104 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 8d566935..692f7e31 100644 --- a/app/src/main/java/org/solovyev/android/calculator/App.java +++ b/app/src/main/java/org/solovyev/android/calculator/App.java @@ -39,9 +39,7 @@ import android.text.Spannable; import android.text.SpannableString; import android.text.style.ForegroundColorSpan; import android.view.View; - import com.squareup.otto.Bus; - import org.solovyev.android.Check; import org.solovyev.android.Views; import org.solovyev.android.calculator.ga.Ga; @@ -49,24 +47,20 @@ import org.solovyev.android.calculator.language.Languages; import org.solovyev.android.calculator.onscreen.CalculatorOnscreenService; import org.solovyev.android.calculator.view.ScreenMetrics; import org.solovyev.android.calculator.wizard.CalculatorWizards; -import org.solovyev.android.checkout.Billing; -import org.solovyev.android.checkout.Checkout; -import org.solovyev.android.checkout.Inventory; -import org.solovyev.android.checkout.ProductTypes; -import org.solovyev.android.checkout.Products; -import org.solovyev.android.checkout.RobotmediaDatabase; -import org.solovyev.android.checkout.RobotmediaInventory; +import org.solovyev.android.checkout.*; import org.solovyev.android.wizard.Wizards; +import org.solovyev.common.JPredicate; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.Arrays; +import java.util.Collection; +import java.util.List; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicInteger; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - /** * This class aggregates several useful in any Android application interfaces and provides access to {@link android.app.Application} object from a static context. * NOTE: use this class only if you don't use and dependency injection library (if you use any you can directly set interfaces through it).
@@ -333,4 +327,29 @@ public final class App { } } } + + public static T find(@Nullable List list, @Nonnull JPredicate finder) { + if (list == null || list.isEmpty()) { + return null; + } + for (int i = 0; i < list.size(); i++) { + final T t = list.get(i); + if (finder.apply(t)) { + return t; + } + } + return null; + } + + public static T find(@Nullable Collection collection, @Nonnull JPredicate finder) { + if (collection == null || collection.isEmpty()) { + return null; + } + for (T t : collection) { + if (finder.apply(t)) { + return t; + } + } + return null; + } } \ No newline at end of file diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorFixableError.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorFixableError.java index d2b1372f..89dfd8ee 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorFixableError.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorFixableError.java @@ -22,15 +22,13 @@ package org.solovyev.android.calculator; -import org.solovyev.common.collections.Collections; - -import java.util.List; +import jscl.AngleUnit; +import jscl.text.msg.Messages; import javax.annotation.Nonnull; import javax.annotation.Nullable; - -import jscl.AngleUnit; -import jscl.text.msg.Messages; +import java.util.Arrays; +import java.util.List; /** * User: serso @@ -64,7 +62,7 @@ public enum CalculatorFixableError implements FixableError { private final List messageCodes; CalculatorFixableError(@Nullable String... messageCodes) { - this.messageCodes = Collections.asList(messageCodes); + this.messageCodes = messageCodes == null || messageCodes.length == 0 ? java.util.Collections.emptyList() : Arrays.asList(messageCodes); } @Nullable diff --git a/app/src/main/java/org/solovyev/android/calculator/OperatorCategory.java b/app/src/main/java/org/solovyev/android/calculator/OperatorCategory.java index 1970ff92..57649954 100644 --- a/app/src/main/java/org/solovyev/android/calculator/OperatorCategory.java +++ b/app/src/main/java/org/solovyev/android/calculator/OperatorCategory.java @@ -24,9 +24,9 @@ package org.solovyev.android.calculator; import android.support.annotation.StringRes; import jscl.math.operator.*; -import org.solovyev.common.collections.Collections; import javax.annotation.Nonnull; +import java.util.Arrays; import java.util.Comparator; import java.util.List; @@ -79,7 +79,7 @@ public enum OperatorCategory implements Category { @Nonnull public static List getCategoriesByTabOrder() { - final List result = Collections.asList(OperatorCategory.values()); + final List result = Arrays.asList(OperatorCategory.values()); java.util.Collections.sort(result, new Comparator() { @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 85318efa..f41b72ae 100644 --- a/app/src/main/java/org/solovyev/android/calculator/ToJsclTextProcessor.java +++ b/app/src/main/java/org/solovyev/android/calculator/ToJsclTextProcessor.java @@ -84,7 +84,7 @@ public class ToJsclTextProcessor implements TextProcessor 0) { @@ -112,11 +112,11 @@ public class ToJsclTextProcessor implements TextProcessor getCategoriesByTabOrder() { - final List result = Collections.asList(VarCategory.values()); + final List result = Arrays.asList(VarCategory.values()); java.util.Collections.sort(result, new Comparator() { @Override diff --git a/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java b/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java index ae84a1e8..ed7913c0 100644 --- a/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java @@ -478,7 +478,7 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC @Nonnull private List getNamesSorted(@NonNull MathRegistry registry) { - final List names = registry.getNames(); + final List names = new ArrayList<>(registry.getNames()); Collections.sort(names); return names; } 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 98177f9c..ebbf5d19 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,10 +25,9 @@ package org.solovyev.android.calculator.math; import jscl.JsclMathEngine; import jscl.NumeralBase; import jscl.math.function.Constants; -import org.solovyev.android.calculator.ParseException; import org.solovyev.android.calculator.Locator; +import org.solovyev.android.calculator.ParseException; import org.solovyev.common.JPredicate; -import org.solovyev.common.collections.Collections; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -235,7 +234,7 @@ public enum MathType { boolean needMultiplicationSignAfter, @Nonnull MathGroupType groupType, @Nonnull String... tokens) { - this(priority, needMultiplicationSignBefore, needMultiplicationSignAfter, groupType, Collections.asList(tokens)); + this(priority, needMultiplicationSignBefore, needMultiplicationSignAfter, groupType, Arrays.asList(tokens)); } MathType(@Nonnull Integer priority, @@ -314,7 +313,7 @@ public enum MathType { @Nonnull private static List getMathTypesByPriority() { if (mathTypesByPriority == null) { - final List result = Collections.asList(MathType.values()); + final List result = Arrays.asList(MathType.values()); java.util.Collections.sort(result, new Comparator() { @Override diff --git a/app/src/main/java/org/solovyev/android/fragments/FragmentUtils.java b/app/src/main/java/org/solovyev/android/fragments/FragmentUtils.java index 2045d441..ad3d2390 100644 --- a/app/src/main/java/org/solovyev/android/fragments/FragmentUtils.java +++ b/app/src/main/java/org/solovyev/android/fragments/FragmentUtils.java @@ -27,11 +27,6 @@ import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; -import android.support.v7.app.ActionBarActivity; - -import org.solovyev.common.collections.Collections; - -import java.util.List; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -74,45 +69,4 @@ public class FragmentUtils { } } - public static void removeFragments(@Nonnull ActionBarActivity activity, @Nonnull String... fragmentTags) { - removeFragments(activity, Collections.asList(fragmentTags)); - } - - public static void removeFragments(@Nonnull ActionBarActivity activity, @Nonnull List fragmentTags) { - for (String fragmentTag : fragmentTags) { - removeFragment(activity, fragmentTag); - } - } - - public static void detachFragments(@Nonnull ActionBarActivity activity, @Nonnull String... fragmentTags) { - detachFragments(activity, Collections.asList(fragmentTags)); - } - - public static void detachFragments(@Nonnull ActionBarActivity activity, @Nonnull List fragmentTags) { - for (String fragmentTag : fragmentTags) { - detachFragment(activity, fragmentTag); - } - } - - public static void detachFragment(@Nonnull ActionBarActivity activity, @Nonnull String fragmentTag) { - final Fragment fragment = activity.getSupportFragmentManager().findFragmentByTag(fragmentTag); - if (fragment != null) { - if (!fragment.isDetached()) { - FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction(); - ft.detach(fragment); - ft.commit(); - } - } - } - - public static void removeFragment(@Nonnull ActionBarActivity activity, @Nonnull String fragmentTag) { - final Fragment fragment = activity.getSupportFragmentManager().findFragmentByTag(fragmentTag); - if (fragment != null) { - if (fragment.isAdded()) { - FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction(); - ft.remove(fragment); - ft.commit(); - } - } - } } diff --git a/jscl/src/main/java/jscl/text/ExceptionsPool.java b/jscl/src/main/java/jscl/text/ExceptionsPool.java index a0089778..f3d3944b 100644 --- a/jscl/src/main/java/jscl/text/ExceptionsPool.java +++ b/jscl/src/main/java/jscl/text/ExceptionsPool.java @@ -1,7 +1,9 @@ package jscl.text; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -15,6 +17,11 @@ public class ExceptionsPool { return obtain(position, expression, messageCode, Collections.emptyList()); } + @Nonnull + public ParseException obtain(int position, @Nonnull String expression, @Nonnull String messageCode, @Nullable Object[] messageArgs) { + return obtain(position, expression, messageCode, messageArgs == null || messageArgs.length == 0 ? java.util.Collections.emptyList() : Arrays.asList(messageArgs)); + } + @Nonnull public ParseException obtain(int position, @Nonnull String expression, @Nonnull String messageCode, @Nonnull List messagesArgs) { final ParseException exception = !list.isEmpty() ? list.remove(list.size() - 1) : new ParseException(); diff --git a/jscl/src/main/java/jscl/text/ParseException.java b/jscl/src/main/java/jscl/text/ParseException.java index d99fe2d2..e4dc532a 100644 --- a/jscl/src/main/java/jscl/text/ParseException.java +++ b/jscl/src/main/java/jscl/text/ParseException.java @@ -1,12 +1,12 @@ package jscl.text; -import org.solovyev.common.collections.Collections; import org.solovyev.common.msg.Message; import org.solovyev.common.msg.MessageLevel; import org.solovyev.common.msg.MessageType; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.util.Arrays; import java.util.List; import java.util.Locale; import java.util.ResourceBundle; @@ -25,7 +25,7 @@ public class ParseException extends Exception implements Message { } public ParseException(int position, @Nonnull String expression, @Nonnull String messageCode, @Nullable Object... parameters) { - set(position, expression, messageCode, Collections.asList(parameters)); + set(position, expression, messageCode, parameters == null || parameters.length == 0 ? java.util.Collections.emptyList() : Arrays.asList(parameters)); } void set(int position, @Nonnull String expression, @Nonnull String messageCode, @Nonnull List parameters) { diff --git a/jscl/src/main/java/jscl/text/ParserUtils.java b/jscl/src/main/java/jscl/text/ParserUtils.java index 01168b43..3ac5c1ff 100644 --- a/jscl/src/main/java/jscl/text/ParserUtils.java +++ b/jscl/src/main/java/jscl/text/ParserUtils.java @@ -2,7 +2,6 @@ package jscl.text; import jscl.math.Generic; import jscl.text.msg.Messages; -import org.solovyev.common.collections.Collections; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -68,7 +67,7 @@ public class ParserUtils { @Nonnull String messageId, Object... parameters) throws ParseException { final MutableInt position = p.position; - final ParseException parseException = p.exceptionsPool.obtain(position.intValue(), p.expression, messageId, Collections.asList(parameters)); + final ParseException parseException = p.exceptionsPool.obtain(position.intValue(), p.expression, messageId, parameters); position.setValue(pos0); throw parseException; } diff --git a/jscl/src/main/java/org/solovyev/common/math/AbstractMathRegistry.java b/jscl/src/main/java/org/solovyev/common/math/AbstractMathRegistry.java index 402652a2..b5fe8bd8 100644 --- a/jscl/src/main/java/org/solovyev/common/math/AbstractMathRegistry.java +++ b/jscl/src/main/java/org/solovyev/common/math/AbstractMathRegistry.java @@ -23,20 +23,16 @@ package org.solovyev.common.math; import org.solovyev.common.JBuilder; -import org.solovyev.common.JPredicate; +import org.solovyev.common.collections.Collections; import org.solovyev.common.collections.SortedList; import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.annotation.concurrent.GuardedBy; import java.util.ArrayList; -import java.util.Collection; import java.util.Comparator; import java.util.List; -import static org.solovyev.common.collections.Collections.find; -import static org.solovyev.common.collections.Collections.removeFirst; - /** * User: serso * Date: 9/29/11 @@ -53,6 +49,9 @@ public abstract class AbstractMathRegistry implements Math protected final SortedList entities = SortedList.newInstance(new ArrayList(30), MATH_ENTITY_COMPARATOR); @GuardedBy("this") @Nonnull + protected final List entityNames = new ArrayList<>(); + @GuardedBy("this") + @Nonnull protected final SortedList systemEntities = SortedList.newInstance(new ArrayList(30), MATH_ENTITY_COMPARATOR); protected AbstractMathRegistry() { @@ -91,6 +90,7 @@ public abstract class AbstractMathRegistry implements Math if (!contains(entity.getName(), this.entities)) { addEntity(entity, this.entities); + this.entityNames.clear(); } } } @@ -118,13 +118,14 @@ public abstract class AbstractMathRegistry implements Math varFromRegister = entity; addEntity(entity, this.entities); + this.entityNames.clear(); if (entity.isSystem()) { this.systemEntities.add(entity); } - } else { varFromRegister.copy(entity); this.entities.sort(); + this.entityNames.clear(); this.systemEntities.sort(); } @@ -135,38 +136,56 @@ public abstract class AbstractMathRegistry implements Math public void remove(@Nonnull T entity) { synchronized (this) { if (!entity.isSystem()) { - removeFirst(this.entities, new MathEntity.Finder(entity.getName())); + final T removed = Collections.removeFirst(this.entities, new MathEntity.Finder(entity.getName())); + if(removed != null) { + this.entityNames.clear(); + } } } } @Nonnull public List getNames() { - final List result = new ArrayList(entities.size()); - synchronized (this) { - for (T entity : entities) { - result.add(entity.getName()); + if (entityNames.isEmpty()) { + for (T entity : entities) { + entityNames.add(entity.getName()); + } } + return entityNames; } - - return result; } @Nullable public T get(@Nonnull final String name) { synchronized (this) { - return find(entities, new MathEntity.Finder(name)); + return get(name, entities); } } + @Nullable + private T get(@Nonnull String name, @Nonnull List list) { + for (int i = 0; i < list.size(); i++) { + final T entity = list.get(i); + if (areEqual(entity.getName(), name)) { + return entity; + } + } + return null; + } + + private static boolean areEqual(@Nullable Object l, @Nullable Object r) { + return l != null ? l.equals(r) : r == null; + } + public T getById(@Nonnull final Integer id) { synchronized (this) { - return find(entities, new JPredicate() { - public boolean apply(@Nullable T t) { - return t != null && t.getId().equals(id); + for (T entity : entities) { + if (areEqual(entity.getId(), id)) { + return entity; } - }); + } + return null; } } @@ -176,10 +195,8 @@ public abstract class AbstractMathRegistry implements Math } } - private boolean contains(final String name, @Nonnull Collection entities) { - synchronized (this) { - return find(entities, new MathEntity.Finder(name)) != null; - } + private boolean contains(final String name, @Nonnull List entities) { + return get(name, entities) != null; } static class MathEntityComparator implements Comparator {