Search in sources :

Example 61 with Comparator

use of java.util.Comparator in project DataX by alibaba.

the class RangeSplit method splitIntegerRange.

/**
     * 切分数值类型 注意: 当begin和end相等时,函数将返回空的List
     * 
     * @param begin
     * @param end
     * @param count
     * @return
     */
public static List<Long> splitIntegerRange(long begin, long end, int count) {
    if (count <= 1) {
        throw new IllegalArgumentException("Input count <= 1 .");
    }
    List<Long> is = new ArrayList<Long>();
    BigInteger bigBegin = BigInteger.valueOf(begin);
    BigInteger bigEnd = BigInteger.valueOf(end);
    BigInteger bigCount = BigInteger.valueOf(count);
    BigInteger abs = (bigEnd.subtract(bigBegin)).abs();
    if (abs.compareTo(BigInteger.ZERO) == 0) {
        // partition key 相等的情况
        return is;
    }
    if (bigCount.compareTo(abs) > 0) {
        bigCount = abs;
    }
    if (bigEnd.subtract(bigBegin).compareTo(BigInteger.ZERO) > 0) {
        // 正向
        return splitIntegerRange(bigBegin, bigEnd, bigCount);
    } else {
        // 逆向
        List<Long> tmp = splitIntegerRange(bigEnd, bigBegin, bigCount);
        Comparator<Long> comparator = new Comparator<Long>() {

            public int compare(Long arg0, Long arg1) {
                return arg0.compareTo(arg1);
            }
        };
        Collections.sort(tmp, Collections.reverseOrder(comparator));
        return tmp;
    }
}
Also used : ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) Comparator(java.util.Comparator)

Example 62 with Comparator

use of java.util.Comparator in project Reader by TheKeeperOfPie.

the class ControllerSearch method setSort.

public Observable<Listing> setSort(Sort sort) {
    Listing listingSort = getCurrentPage() == PAGE_SUBREDDITS_RECOMMENDED ? subredditsRecommended : subreddits;
    if (Sort.ALPHABETICAL.equals(sort)) {
        if (Sort.ALPHABETICAL.equals(sortSubreddits)) {
            Collections.sort(listingSort.getChildren(), new Comparator<Thing>() {

                @Override
                public int compare(Thing lhs, Thing rhs) {
                    return ((Subreddit) rhs).getDisplayName().compareToIgnoreCase(((Subreddit) lhs).getDisplayName());
                }
            });
            sortSubreddits = null;
        } else {
            Collections.sort(listingSort.getChildren(), new Comparator<Thing>() {

                @Override
                public int compare(Thing lhs, Thing rhs) {
                    return ((Subreddit) lhs).getDisplayName().compareToIgnoreCase(((Subreddit) rhs).getDisplayName());
                }
            });
            sortSubreddits = Sort.ALPHABETICAL;
        }
        if (listingSort == subredditsSubscribed) {
            saveSubscriptions();
        } else if (listingSort == subredditsRecommended) {
            for (Listener listener : listeners) {
                listener.getAdapterSearchSubredditsRecommended().notifyDataSetChanged();
            }
        } else {
            for (Listener listener : listeners) {
                listener.getAdapterSearchSubreddits().notifyDataSetChanged();
            }
        }
    } else if (Sort.SUBSCRIBERS.equals(sort)) {
        if (Sort.SUBSCRIBERS.equals(sortSubreddits)) {
            Collections.sort(listingSort.getChildren(), new Comparator<Thing>() {

                @Override
                public int compare(Thing lhs, Thing rhs) {
                    long subscribersFirst = ((Subreddit) lhs).getSubscribers();
                    long subscribersSecond = ((Subreddit) rhs).getSubscribers();
                    return subscribersFirst < subscribersSecond ? -1 : (subscribersFirst == subscribersSecond ? 0 : 1);
                }
            });
            sortSubreddits = null;
        } else {
            Collections.sort(listingSort.getChildren(), new Comparator<Thing>() {

                @Override
                public int compare(Thing lhs, Thing rhs) {
                    long subscribersFirst = ((Subreddit) lhs).getSubscribers();
                    long subscribersSecond = ((Subreddit) rhs).getSubscribers();
                    return subscribersSecond < subscribersFirst ? -1 : (subscribersFirst == subscribersSecond ? 0 : 1);
                }
            });
            sortSubreddits = Sort.SUBSCRIBERS;
        }
        if (listingSort == subredditsSubscribed) {
            saveSubscriptions();
        } else if (listingSort == subredditsRecommended) {
            for (Listener listener : listeners) {
                listener.getAdapterSearchSubredditsRecommended().notifyDataSetChanged();
            }
        } else {
            for (Listener listener : listeners) {
                listener.getAdapterSearchSubreddits().notifyDataSetChanged();
            }
        }
    } else if (this.sort != sort) {
        this.sort = sort;
        eventHolder.getSort().call(sort);
        return reloadCurrentPage();
    }
    return Observable.empty();
}
Also used : Listing(com.winsonchiu.reader.data.reddit.Listing) Subreddit(com.winsonchiu.reader.data.reddit.Subreddit) Thing(com.winsonchiu.reader.data.reddit.Thing) Comparator(java.util.Comparator)

Example 63 with Comparator

use of java.util.Comparator in project CoCoin by Nightonke.

the class CoCoinUtil method SortTreeMapByValues.

public static <K, V extends Comparable<V>> Map<K, V> SortTreeMapByValues(final Map<K, V> map) {
    Comparator<K> valueComparator = new Comparator<K>() {

        public int compare(K k1, K k2) {
            int compare = map.get(k1).compareTo(map.get(k2));
            if (compare == 0)
                return 1;
            else
                return compare;
        }
    };
    TreeMap<K, V> sortedByValues = new TreeMap<K, V>(valueComparator);
    sortedByValues.putAll(map);
    return sortedByValues;
}
Also used : TreeMap(java.util.TreeMap) Point(android.graphics.Point) Comparator(java.util.Comparator)

Example 64 with Comparator

use of java.util.Comparator in project onebusaway-gtfs-modules by OneBusAway.

the class GtfsEntitySchemaFactory method getEntityComparators.

public static Map<Class<?>, Comparator<?>> getEntityComparators() {
    Map<Class<?>, Comparator<?>> comparators = new HashMap<Class<?>, Comparator<?>>();
    comparators.put(Agency.class, getComparatorForIdentityBeanType(Agency.class));
    comparators.put(Block.class, getComparatorForIdentityBeanType(Block.class));
    comparators.put(Route.class, getComparatorForIdentityBeanType(Route.class));
    comparators.put(Stop.class, getComparatorForIdentityBeanType(Stop.class));
    comparators.put(Trip.class, getComparatorForIdentityBeanType(Trip.class));
    comparators.put(StopTime.class, new StopTimeComparator());
    comparators.put(ShapePoint.class, new ShapePointComparator());
    comparators.put(ServiceCalendar.class, new ServiceCalendarComparator());
    comparators.put(ServiceCalendarDate.class, new ServiceCalendarDateComparator());
    return comparators;
}
Also used : Trip(org.onebusaway.gtfs.model.Trip) Agency(org.onebusaway.gtfs.model.Agency) ServiceCalendarComparator(org.onebusaway.gtfs.serialization.comparators.ServiceCalendarComparator) HashMap(java.util.HashMap) Stop(org.onebusaway.gtfs.model.Stop) ServiceCalendarDateComparator(org.onebusaway.gtfs.serialization.comparators.ServiceCalendarDateComparator) ShapePointComparator(org.onebusaway.gtfs.serialization.comparators.ShapePointComparator) StopTimeComparator(org.onebusaway.gtfs.serialization.comparators.StopTimeComparator) ServiceCalendarComparator(org.onebusaway.gtfs.serialization.comparators.ServiceCalendarComparator) Comparator(java.util.Comparator) ServiceCalendarDateComparator(org.onebusaway.gtfs.serialization.comparators.ServiceCalendarDateComparator) StopTimeComparator(org.onebusaway.gtfs.serialization.comparators.StopTimeComparator) ShapePointComparator(org.onebusaway.gtfs.serialization.comparators.ShapePointComparator) Block(org.onebusaway.gtfs.model.Block) Route(org.onebusaway.gtfs.model.Route)

Example 65 with Comparator

use of java.util.Comparator in project android_frameworks_base by ParanoidAndroid.

the class AlarmManagerService method dump.

@Override
protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
    if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
        pw.println("Permission Denial: can't dump AlarmManager from from pid=" + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
        return;
    }
    synchronized (mLock) {
        pw.println("Current Alarm Manager state:");
        if (mRtcWakeupAlarms.size() > 0 || mRtcAlarms.size() > 0) {
            final long now = System.currentTimeMillis();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            pw.println(" ");
            pw.print("  Realtime wakeup (now=");
            pw.print(sdf.format(new Date(now)));
            pw.println("):");
            if (mRtcWakeupAlarms.size() > 0) {
                dumpAlarmList(pw, mRtcWakeupAlarms, "  ", "RTC_WAKEUP", now);
            }
            if (mRtcAlarms.size() > 0) {
                dumpAlarmList(pw, mRtcAlarms, "  ", "RTC", now);
            }
        }
        if (mElapsedRealtimeWakeupAlarms.size() > 0 || mElapsedRealtimeAlarms.size() > 0) {
            final long now = SystemClock.elapsedRealtime();
            pw.println(" ");
            pw.print("  Elapsed realtime wakeup (now=");
            TimeUtils.formatDuration(now, pw);
            pw.println("):");
            if (mElapsedRealtimeWakeupAlarms.size() > 0) {
                dumpAlarmList(pw, mElapsedRealtimeWakeupAlarms, "  ", "ELAPSED_WAKEUP", now);
            }
            if (mElapsedRealtimeAlarms.size() > 0) {
                dumpAlarmList(pw, mElapsedRealtimeAlarms, "  ", "ELAPSED", now);
            }
        }
        pw.println();
        pw.print("  Broadcast ref count: ");
        pw.println(mBroadcastRefCount);
        pw.println();
        if (mLog.dump(pw, "  Recent problems", "    ")) {
            pw.println();
        }
        final FilterStats[] topFilters = new FilterStats[10];
        final Comparator<FilterStats> comparator = new Comparator<FilterStats>() {

            @Override
            public int compare(FilterStats lhs, FilterStats rhs) {
                if (lhs.aggregateTime < rhs.aggregateTime) {
                    return 1;
                } else if (lhs.aggregateTime > rhs.aggregateTime) {
                    return -1;
                }
                return 0;
            }
        };
        int len = 0;
        for (Map.Entry<String, BroadcastStats> be : mBroadcastStats.entrySet()) {
            BroadcastStats bs = be.getValue();
            for (Map.Entry<Pair<String, ComponentName>, FilterStats> fe : bs.filterStats.entrySet()) {
                FilterStats fs = fe.getValue();
                int pos = len > 0 ? Arrays.binarySearch(topFilters, 0, len, fs, comparator) : 0;
                if (pos < 0) {
                    pos = -pos - 1;
                }
                if (pos < topFilters.length) {
                    int copylen = topFilters.length - pos - 1;
                    if (copylen > 0) {
                        System.arraycopy(topFilters, pos, topFilters, pos + 1, copylen);
                    }
                    topFilters[pos] = fs;
                    if (len < topFilters.length) {
                        len++;
                    }
                }
            }
        }
        if (len > 0) {
            pw.println("  Top Alarms:");
            for (int i = 0; i < len; i++) {
                FilterStats fs = topFilters[i];
                pw.print("    ");
                if (fs.nesting > 0)
                    pw.print("*ACTIVE* ");
                TimeUtils.formatDuration(fs.aggregateTime, pw);
                pw.print(" running, ");
                pw.print(fs.numWakeup);
                pw.print(" wakeups, ");
                pw.print(fs.count);
                pw.print(" alarms: ");
                pw.print(fs.mBroadcastStats.mPackageName);
                pw.println();
                pw.print("      ");
                if (fs.mTarget.first != null) {
                    pw.print(" act=");
                    pw.print(fs.mTarget.first);
                }
                if (fs.mTarget.second != null) {
                    pw.print(" cmp=");
                    pw.print(fs.mTarget.second.toShortString());
                }
                pw.println();
            }
        }
        pw.println(" ");
        pw.println("  Alarm Stats:");
        final ArrayList<FilterStats> tmpFilters = new ArrayList<FilterStats>();
        for (Map.Entry<String, BroadcastStats> be : mBroadcastStats.entrySet()) {
            BroadcastStats bs = be.getValue();
            pw.print("  ");
            if (bs.nesting > 0)
                pw.print("*ACTIVE* ");
            pw.print(be.getKey());
            pw.print(" ");
            TimeUtils.formatDuration(bs.aggregateTime, pw);
            pw.print(" running, ");
            pw.print(bs.numWakeup);
            pw.println(" wakeups:");
            tmpFilters.clear();
            for (Map.Entry<Pair<String, ComponentName>, FilterStats> fe : bs.filterStats.entrySet()) {
                tmpFilters.add(fe.getValue());
            }
            Collections.sort(tmpFilters, comparator);
            for (int i = 0; i < tmpFilters.size(); i++) {
                FilterStats fs = tmpFilters.get(i);
                pw.print("    ");
                if (fs.nesting > 0)
                    pw.print("*ACTIVE* ");
                TimeUtils.formatDuration(fs.aggregateTime, pw);
                pw.print(" ");
                pw.print(fs.numWakeup);
                pw.print(" wakes ");
                pw.print(fs.count);
                pw.print(" alarms:");
                if (fs.mTarget.first != null) {
                    pw.print(" act=");
                    pw.print(fs.mTarget.first);
                }
                if (fs.mTarget.second != null) {
                    pw.print(" cmp=");
                    pw.print(fs.mTarget.second.toShortString());
                }
                pw.println();
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Date(java.util.Date) Comparator(java.util.Comparator) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) Map(java.util.Map) Pair(android.util.Pair)

Aggregations

Comparator (java.util.Comparator)372 ArrayList (java.util.ArrayList)139 Test (org.junit.Test)66 List (java.util.List)65 HashMap (java.util.HashMap)57 Map (java.util.Map)39 IOException (java.io.IOException)37 File (java.io.File)27 HashSet (java.util.HashSet)26 TreeSet (java.util.TreeSet)23 Set (java.util.Set)21 Date (java.util.Date)17 TreeMap (java.util.TreeMap)17 Method (java.lang.reflect.Method)16 Iterator (java.util.Iterator)16 LinkedList (java.util.LinkedList)16 Collection (java.util.Collection)15 Collections (java.util.Collections)15 ArrayMap (android.util.ArrayMap)12 SimpleDateFormat (java.text.SimpleDateFormat)10