Optimize work with lists
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user