blob: a2010e19b17698e4e95eaf3a568fbcd6af4d0ac9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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;
}
}
|