Optimize work with lists
This commit is contained in:
parent
88c1253609
commit
c000a525db
@ -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). <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;
|
||||
}
|
||||
}
|
@ -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<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
|
||||
|
@ -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<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>() {
|
||||
@Override
|
||||
|
@ -84,7 +84,7 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
|
||||
|
||||
if (mathTypeBefore != null &&
|
||||
(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 Function function = Locator.getInstance().getEngine().getFunctionsRegistry().get(functionName);
|
||||
if (function == null || function.getMinParameters() > 0) {
|
||||
@ -112,11 +112,11 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
|
||||
startsWithFinder.setI(i);
|
||||
|
||||
int offset = 0;
|
||||
String functionName = Collections.find(MathType.function.getTokens(), startsWithFinder);
|
||||
String functionName = App.find(MathType.function.getTokens(), startsWithFinder);
|
||||
if (functionName == null) {
|
||||
String operatorName = Collections.find(MathType.operator.getTokens(), startsWithFinder);
|
||||
String operatorName = App.find(MathType.operator.getTokens(), startsWithFinder);
|
||||
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) {
|
||||
final IConstant var = Locator.getInstance().getEngine().getVarsRegistry().get(varName);
|
||||
if (var != null) {
|
||||
|
@ -24,9 +24,9 @@ package org.solovyev.android.calculator;
|
||||
|
||||
import android.support.annotation.StringRes;
|
||||
import jscl.math.function.IConstant;
|
||||
import org.solovyev.common.collections.Collections;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
@ -57,7 +57,7 @@ public enum VarCategory implements Category {
|
||||
|
||||
@Nonnull
|
||||
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>() {
|
||||
@Override
|
||||
|
@ -478,7 +478,7 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC
|
||||
|
||||
@Nonnull
|
||||
private List<String> getNamesSorted(@NonNull MathRegistry<?> registry) {
|
||||
final List<String> names = registry.getNames();
|
||||
final List<String> names = new ArrayList<>(registry.getNames());
|
||||
Collections.sort(names);
|
||||
return names;
|
||||
}
|
||||
|
@ -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<MathType> getMathTypesByPriority() {
|
||||
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>() {
|
||||
@Override
|
||||
|
@ -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<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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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<T extends MathEntity> implements Math
|
||||
protected final SortedList<T> entities = SortedList.newInstance(new ArrayList<T>(30), MATH_ENTITY_COMPARATOR);
|
||||
@GuardedBy("this")
|
||||
@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 AbstractMathRegistry() {
|
||||
@ -91,6 +90,7 @@ public abstract class AbstractMathRegistry<T extends MathEntity> implements Math
|
||||
|
||||
if (!contains(entity.getName(), this.entities)) {
|
||||
addEntity(entity, this.entities);
|
||||
this.entityNames.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -118,13 +118,14 @@ public abstract class AbstractMathRegistry<T extends MathEntity> 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<T extends MathEntity> implements Math
|
||||
public void remove(@Nonnull T entity) {
|
||||
synchronized (this) {
|
||||
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
|
||||
public List<String> getNames() {
|
||||
final List<String> result = new ArrayList<String>(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<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) {
|
||||
synchronized (this) {
|
||||
return find(entities, new JPredicate<T>() {
|
||||
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<T extends MathEntity> implements Math
|
||||
}
|
||||
}
|
||||
|
||||
private boolean contains(final String name, @Nonnull Collection<T> entities) {
|
||||
synchronized (this) {
|
||||
return find(entities, new MathEntity.Finder<T>(name)) != null;
|
||||
}
|
||||
private boolean contains(final String name, @Nonnull List<T> entities) {
|
||||
return get(name, entities) != null;
|
||||
}
|
||||
|
||||
static class MathEntityComparator<T extends MathEntity> implements Comparator<T> {
|
||||
|
Loading…
Reference in New Issue
Block a user