| 1 | /* |
| 2 | * Copyright (C) 2010 Keith Kildare |
| 3 | * |
| 4 | * This file is part of SimplyDo. |
| 5 | * |
| 6 | * SimplyDo is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * SimplyDo is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with SimplyDo. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | package kdk.android.simplydo; |
| 21 | |
| 22 | import java.util.Collections; |
| 23 | import java.util.Comparator; |
| 24 | import java.util.List; |
| 25 | |
| 26 | import android.util.Log; |
| 27 | |
| 28 | public class ListListSorter |
| 29 | { |
| 30 | public static final String PREF_NONE = "none"; |
| 31 | public static final String PREF_ALPHA = "alphabetical"; |
| 32 | |
| 33 | private static final int NONE = 0; |
| 34 | private static final int ALPHA = 1; |
| 35 | |
| 36 | private int sortingMode; |
| 37 | |
| 38 | private Comparator<ListDesc> idCompare; |
| 39 | private Comparator<ListDesc> alphaCompare; |
| 40 | |
| 41 | |
| 42 | public ListListSorter() |
| 43 | { |
| 44 | idCompare = new Comparator<ListDesc>() { |
| 45 | @Override |
| 46 | public int compare(ListDesc object1, ListDesc object2) |
| 47 | { |
| 48 | return object2.getId() - object1.getId(); |
| 49 | } |
| 50 | }; |
| 51 | alphaCompare = new Comparator<ListDesc>() { |
| 52 | @Override |
| 53 | public int compare(ListDesc object1, ListDesc object2) |
| 54 | { |
| 55 | return object1.getLabel().compareToIgnoreCase(object2.getLabel()); |
| 56 | } |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | public void setSortingMode(String mode) |
| 62 | { |
| 63 | if(PREF_NONE.equals(mode)) |
| 64 | { |
| 65 | sortingMode = NONE; |
| 66 | } |
| 67 | else if(PREF_ALPHA.equals(mode)) |
| 68 | { |
| 69 | sortingMode = ALPHA; |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | sortingMode = NONE; |
| 74 | Log.w(L.TAG, "Unknown list sorting mode " + mode); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | |
| 79 | public void sort(List<ListDesc> list) |
| 80 | { |
| 81 | switch(sortingMode) |
| 82 | { |
| 83 | default: |
| 84 | Log.w(L.TAG, "Unknown list sorting enum " + sortingMode); |
| 85 | // fall through |
| 86 | case NONE: |
| 87 | // actually sorted by db id |
| 88 | Collections.sort(list, idCompare); |
| 89 | break; |
| 90 | case ALPHA: |
| 91 | Collections.sort(list, alphaCompare); |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | } |