diff options
author | linus_lee <llee@cyngn.com> | 2014-11-20 16:39:38 -0800 |
---|---|---|
committer | linus_lee <llee@cyngn.com> | 2014-12-09 12:23:20 -0800 |
commit | 71810ebb2bf8fd792c92487fe87f9dbebefc8541 (patch) | |
tree | 42a4d11ba03a4c7af843edc0b45375b17c64053c /src/com/cyanogenmod/eleven/utils/SortUtils.java | |
parent | f199f983c9a5e2f4434b85273d1da0d609c33228 (diff) | |
download | android_packages_apps_Eleven-71810ebb2bf8fd792c92487fe87f9dbebefc8541.tar.gz android_packages_apps_Eleven-71810ebb2bf8fd792c92487fe87f9dbebefc8541.tar.bz2 android_packages_apps_Eleven-71810ebb2bf8fd792c92487fe87f9dbebefc8541.zip |
Update Eleven headers and namespace for open source
Change-Id: I82caf2ebf991998e67f546ff2ac7eaf2b30dc6be
Diffstat (limited to 'src/com/cyanogenmod/eleven/utils/SortUtils.java')
-rw-r--r-- | src/com/cyanogenmod/eleven/utils/SortUtils.java | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/com/cyanogenmod/eleven/utils/SortUtils.java b/src/com/cyanogenmod/eleven/utils/SortUtils.java new file mode 100644 index 0000000..a2010e1 --- /dev/null +++ b/src/com/cyanogenmod/eleven/utils/SortUtils.java @@ -0,0 +1,90 @@ +/* +* Copyright (C) 2014 The CyanogenMod Project +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package com.cyanogenmod.eleven.utils; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.TreeMap; + +/** + * Implementation of custom sorting routines of song list + */ +public class SortUtils { + + /** + * Sorts items based on the localized bucket letter they belong to and the sort order specified + * @param items the original list of items + * @param sortOrder values derived from SortOrder.class + * @return the new sorted list + */ + public static <T> ArrayList<T> localizeSortList(ArrayList<T> items, String sortOrder) { + ArrayList<T> finalList = Lists.newArrayList(); + // map of items grouped by their localized label + TreeMap<String, LinkedList<T>> mappedList = new TreeMap<String, LinkedList<T>>(); + + // list holding items that don't have a localized label + ArrayList<T> nonLocalizableItems = Lists.newArrayList(); + + for (T item : items) { + // get the bucket letter based on the attribute to sort by + String label = MusicUtils.getLocalizedBucketLetterByAttribute(item, sortOrder); + //divvy items based on their localized bucket letter + if (label != null) { + if (mappedList.get(label) == null) { + // create new label slot to assign items + mappedList.put(label, Lists.<T>newLinkedList()); + } + // add item to the label's list + mappedList.get(label).add(item); + } else { + nonLocalizableItems.add(item); + } + } + + // generate a sorted item list out of localizable items + boolean isDescendingSort = MusicUtils.isSortOrderDesending(sortOrder); + finalList.addAll(getSortedList(mappedList, isDescendingSort)); + finalList.addAll(nonLocalizableItems); + + return finalList; + } + + /** + * Traverses a tree map of a divvied up list to generate a sorted list + * @param mappedList the bucketized list of items based on the header + * @param reverseOrder dictates the order in which the TreeMap is traversed (descending order + * if true) + * @return the combined sorted list + */ + private static <T> ArrayList<T> getSortedList(TreeMap<String, LinkedList<T>> mappedList, + boolean reverseOrder) { + ArrayList<T> sortedList = Lists.newArrayList(); + + Iterator<String> iterator = mappedList.navigableKeySet().iterator(); + if (reverseOrder) { + iterator = mappedList.navigableKeySet().descendingIterator(); + } + + while (iterator.hasNext()) { + LinkedList<T> list = mappedList.get(iterator.next()); + sortedList.addAll(list); + } + + return sortedList; + } + +} |