Optimize work with lists

This commit is contained in:
serso 2016-01-24 22:52:55 +01:00
parent 88c1253609
commit c000a525db
12 changed files with 97 additions and 104 deletions

View File

@ -39,9 +39,7 @@ import android.text.Spannable;
import android.text.SpannableString; import android.text.SpannableString;
import android.text.style.ForegroundColorSpan; import android.text.style.ForegroundColorSpan;
import android.view.View; import android.view.View;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
import org.solovyev.android.Check; import org.solovyev.android.Check;
import org.solovyev.android.Views; import org.solovyev.android.Views;
import org.solovyev.android.calculator.ga.Ga; 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.onscreen.CalculatorOnscreenService;
import org.solovyev.android.calculator.view.ScreenMetrics; import org.solovyev.android.calculator.view.ScreenMetrics;
import org.solovyev.android.calculator.wizard.CalculatorWizards; import org.solovyev.android.calculator.wizard.CalculatorWizards;
import org.solovyev.android.checkout.Billing; import org.solovyev.android.checkout.*;
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.wizard.Wizards; 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.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger; 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. * 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). <br/> * 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). <br/>
@ -333,4 +327,29 @@ public final class App {
} }
} }
} }
public static <T> T find(@Nullable List<T> list, @Nonnull JPredicate<T> 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> T find(@Nullable Collection<T> collection, @Nonnull JPredicate<T> finder) {
if (collection == null || collection.isEmpty()) {
return null;
}
for (T t : collection) {
if (finder.apply(t)) {
return t;
}
}
return null;
}
} }

View File

@ -22,15 +22,13 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import org.solovyev.common.collections.Collections; import jscl.AngleUnit;
import jscl.text.msg.Messages;
import java.util.List;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Arrays;
import jscl.AngleUnit; import java.util.List;
import jscl.text.msg.Messages;
/** /**
* User: serso * User: serso
@ -64,7 +62,7 @@ public enum CalculatorFixableError implements FixableError {
private final List<String> messageCodes; private final List<String> messageCodes;
CalculatorFixableError(@Nullable String... messageCodes) { CalculatorFixableError(@Nullable String... messageCodes) {
this.messageCodes = Collections.asList(messageCodes); this.messageCodes = messageCodes == null || messageCodes.length == 0 ? java.util.Collections.<String>emptyList() : Arrays.asList(messageCodes);
} }
@Nullable @Nullable

View File

@ -24,9 +24,9 @@ package org.solovyev.android.calculator;
import android.support.annotation.StringRes; import android.support.annotation.StringRes;
import jscl.math.operator.*; import jscl.math.operator.*;
import org.solovyev.common.collections.Collections;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
@ -79,7 +79,7 @@ public enum OperatorCategory implements Category {
@Nonnull @Nonnull
public static List<OperatorCategory> getCategoriesByTabOrder() { public static List<OperatorCategory> getCategoriesByTabOrder() {
final List<OperatorCategory> result = Collections.asList(OperatorCategory.values()); final List<OperatorCategory> result = Arrays.asList(OperatorCategory.values());
java.util.Collections.sort(result, new Comparator<OperatorCategory>() { java.util.Collections.sort(result, new Comparator<OperatorCategory>() {
@Override @Override

View File

@ -84,7 +84,7 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
if (mathTypeBefore != null && if (mathTypeBefore != null &&
(mathTypeBefore.type == MathType.function || mathTypeBefore.type == MathType.operator) && (mathTypeBefore.type == MathType.function || mathTypeBefore.type == MathType.operator) &&
Collections.find(MathType.groupSymbols, startsWithFinder) != null) { App.find(MathType.groupSymbols, startsWithFinder) != null) {
final String functionName = mathTypeBefore.match; final String functionName = mathTypeBefore.match;
final Function function = Locator.getInstance().getEngine().getFunctionsRegistry().get(functionName); final Function function = Locator.getInstance().getEngine().getFunctionsRegistry().get(functionName);
if (function == null || function.getMinParameters() > 0) { if (function == null || function.getMinParameters() > 0) {
@ -112,11 +112,11 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
startsWithFinder.setI(i); startsWithFinder.setI(i);
int offset = 0; int offset = 0;
String functionName = Collections.find(MathType.function.getTokens(), startsWithFinder); String functionName = App.find(MathType.function.getTokens(), startsWithFinder);
if (functionName == null) { if (functionName == null) {
String operatorName = Collections.find(MathType.operator.getTokens(), startsWithFinder); String operatorName = App.find(MathType.operator.getTokens(), startsWithFinder);
if (operatorName == null) { if (operatorName == null) {
String varName = Collections.find(Locator.getInstance().getEngine().getVarsRegistry().getNames(), startsWithFinder); String varName = App.find(Locator.getInstance().getEngine().getVarsRegistry().getNames(), startsWithFinder);
if (varName != null) { if (varName != null) {
final IConstant var = Locator.getInstance().getEngine().getVarsRegistry().get(varName); final IConstant var = Locator.getInstance().getEngine().getVarsRegistry().get(varName);
if (var != null) { if (var != null) {

View File

@ -24,9 +24,9 @@ package org.solovyev.android.calculator;
import android.support.annotation.StringRes; import android.support.annotation.StringRes;
import jscl.math.function.IConstant; import jscl.math.function.IConstant;
import org.solovyev.common.collections.Collections;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
@ -57,7 +57,7 @@ public enum VarCategory implements Category {
@Nonnull @Nonnull
public static List<VarCategory> getCategoriesByTabOrder() { public static List<VarCategory> getCategoriesByTabOrder() {
final List<VarCategory> result = Collections.asList(VarCategory.values()); final List<VarCategory> result = Arrays.asList(VarCategory.values());
java.util.Collections.sort(result, new Comparator<VarCategory>() { java.util.Collections.sort(result, new Comparator<VarCategory>() {
@Override @Override

View File

@ -478,7 +478,7 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC
@Nonnull @Nonnull
private List<String> getNamesSorted(@NonNull MathRegistry<?> registry) { private List<String> getNamesSorted(@NonNull MathRegistry<?> registry) {
final List<String> names = registry.getNames(); final List<String> names = new ArrayList<>(registry.getNames());
Collections.sort(names); Collections.sort(names);
return names; return names;
} }

View File

@ -25,10 +25,9 @@ package org.solovyev.android.calculator.math;
import jscl.JsclMathEngine; import jscl.JsclMathEngine;
import jscl.NumeralBase; import jscl.NumeralBase;
import jscl.math.function.Constants; import jscl.math.function.Constants;
import org.solovyev.android.calculator.ParseException;
import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.Locator;
import org.solovyev.android.calculator.ParseException;
import org.solovyev.common.JPredicate; import org.solovyev.common.JPredicate;
import org.solovyev.common.collections.Collections;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -235,7 +234,7 @@ public enum MathType {
boolean needMultiplicationSignAfter, boolean needMultiplicationSignAfter,
@Nonnull MathGroupType groupType, @Nonnull MathGroupType groupType,
@Nonnull String... tokens) { @Nonnull String... tokens) {
this(priority, needMultiplicationSignBefore, needMultiplicationSignAfter, groupType, Collections.asList(tokens)); this(priority, needMultiplicationSignBefore, needMultiplicationSignAfter, groupType, Arrays.asList(tokens));
} }
MathType(@Nonnull Integer priority, MathType(@Nonnull Integer priority,
@ -314,7 +313,7 @@ public enum MathType {
@Nonnull @Nonnull
private static List<MathType> getMathTypesByPriority() { private static List<MathType> getMathTypesByPriority() {
if (mathTypesByPriority == null) { if (mathTypesByPriority == null) {
final List<MathType> result = Collections.asList(MathType.values()); final List<MathType> result = Arrays.asList(MathType.values());
java.util.Collections.sort(result, new Comparator<MathType>() { java.util.Collections.sort(result, new Comparator<MathType>() {
@Override @Override

View File

@ -27,11 +27,6 @@ import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction; 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.Nonnull;
import javax.annotation.Nullable; 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<String> 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<String> 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();
}
}
}
} }

View File

@ -1,7 +1,9 @@
package jscl.text; package jscl.text;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -15,6 +17,11 @@ public class ExceptionsPool {
return obtain(position, expression, messageCode, Collections.emptyList()); 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 @Nonnull
public ParseException obtain(int position, @Nonnull String expression, @Nonnull String messageCode, @Nonnull List<?> messagesArgs) { 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(); final ParseException exception = !list.isEmpty() ? list.remove(list.size() - 1) : new ParseException();

View File

@ -1,12 +1,12 @@
package jscl.text; package jscl.text;
import org.solovyev.common.collections.Collections;
import org.solovyev.common.msg.Message; import org.solovyev.common.msg.Message;
import org.solovyev.common.msg.MessageLevel; import org.solovyev.common.msg.MessageLevel;
import org.solovyev.common.msg.MessageType; import org.solovyev.common.msg.MessageType;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.ResourceBundle; 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) { 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) { void set(int position, @Nonnull String expression, @Nonnull String messageCode, @Nonnull List<?> parameters) {

View File

@ -2,7 +2,6 @@ package jscl.text;
import jscl.math.Generic; import jscl.math.Generic;
import jscl.text.msg.Messages; import jscl.text.msg.Messages;
import org.solovyev.common.collections.Collections;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -68,7 +67,7 @@ public class ParserUtils {
@Nonnull String messageId, @Nonnull String messageId,
Object... parameters) throws ParseException { Object... parameters) throws ParseException {
final MutableInt position = p.position; 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); position.setValue(pos0);
throw parseException; throw parseException;
} }

View File

@ -23,20 +23,16 @@
package org.solovyev.common.math; package org.solovyev.common.math;
import org.solovyev.common.JBuilder; import org.solovyev.common.JBuilder;
import org.solovyev.common.JPredicate; import org.solovyev.common.collections.Collections;
import org.solovyev.common.collections.SortedList; import org.solovyev.common.collections.SortedList;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy; import javax.annotation.concurrent.GuardedBy;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import static org.solovyev.common.collections.Collections.find;
import static org.solovyev.common.collections.Collections.removeFirst;
/** /**
* User: serso * User: serso
* Date: 9/29/11 * Date: 9/29/11
@ -53,6 +49,9 @@ public abstract class AbstractMathRegistry<T extends MathEntity> implements Math
protected final SortedList<T> entities = SortedList.newInstance(new ArrayList<T>(30), MATH_ENTITY_COMPARATOR); protected final SortedList<T> entities = SortedList.newInstance(new ArrayList<T>(30), MATH_ENTITY_COMPARATOR);
@GuardedBy("this") @GuardedBy("this")
@Nonnull @Nonnull
protected final List<String> entityNames = new ArrayList<>();
@GuardedBy("this")
@Nonnull
protected final SortedList<T> systemEntities = SortedList.newInstance(new ArrayList<T>(30), MATH_ENTITY_COMPARATOR); protected final SortedList<T> systemEntities = SortedList.newInstance(new ArrayList<T>(30), MATH_ENTITY_COMPARATOR);
protected AbstractMathRegistry() { protected AbstractMathRegistry() {
@ -91,6 +90,7 @@ public abstract class AbstractMathRegistry<T extends MathEntity> implements Math
if (!contains(entity.getName(), this.entities)) { if (!contains(entity.getName(), this.entities)) {
addEntity(entity, this.entities); addEntity(entity, this.entities);
this.entityNames.clear();
} }
} }
} }
@ -118,13 +118,14 @@ public abstract class AbstractMathRegistry<T extends MathEntity> implements Math
varFromRegister = entity; varFromRegister = entity;
addEntity(entity, this.entities); addEntity(entity, this.entities);
this.entityNames.clear();
if (entity.isSystem()) { if (entity.isSystem()) {
this.systemEntities.add(entity); this.systemEntities.add(entity);
} }
} else { } else {
varFromRegister.copy(entity); varFromRegister.copy(entity);
this.entities.sort(); this.entities.sort();
this.entityNames.clear();
this.systemEntities.sort(); this.systemEntities.sort();
} }
@ -135,38 +136,56 @@ public abstract class AbstractMathRegistry<T extends MathEntity> implements Math
public void remove(@Nonnull T entity) { public void remove(@Nonnull T entity) {
synchronized (this) { synchronized (this) {
if (!entity.isSystem()) { if (!entity.isSystem()) {
removeFirst(this.entities, new MathEntity.Finder<T>(entity.getName())); final T removed = Collections.removeFirst(this.entities, new MathEntity.Finder<T>(entity.getName()));
if(removed != null) {
this.entityNames.clear();
}
} }
} }
} }
@Nonnull @Nonnull
public List<String> getNames() { public List<String> getNames() {
final List<String> result = new ArrayList<String>(entities.size());
synchronized (this) { synchronized (this) {
if (entityNames.isEmpty()) {
for (T entity : entities) { for (T entity : entities) {
result.add(entity.getName()); entityNames.add(entity.getName());
} }
} }
return entityNames;
return result; }
} }
@Nullable @Nullable
public T get(@Nonnull final String name) { public T get(@Nonnull final String name) {
synchronized (this) { synchronized (this) {
return find(entities, new MathEntity.Finder<T>(name)); return get(name, entities);
} }
} }
@Nullable
private T get(@Nonnull String name, @Nonnull List<T> 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) { public T getById(@Nonnull final Integer id) {
synchronized (this) { synchronized (this) {
return find(entities, new JPredicate<T>() { for (T entity : entities) {
public boolean apply(@Nullable T t) { if (areEqual(entity.getId(), id)) {
return t != null && t.getId().equals(id); return entity;
} }
}); }
return null;
} }
} }
@ -176,10 +195,8 @@ public abstract class AbstractMathRegistry<T extends MathEntity> implements Math
} }
} }
private boolean contains(final String name, @Nonnull Collection<T> entities) { private boolean contains(final String name, @Nonnull List<T> entities) {
synchronized (this) { return get(name, entities) != null;
return find(entities, new MathEntity.Finder<T>(name)) != null;
}
} }
static class MathEntityComparator<T extends MathEntity> implements Comparator<T> { static class MathEntityComparator<T extends MathEntity> implements Comparator<T> {