Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.contacts; |
| 18 | |
| 19 | import java.util.HashMap; |
| 20 | import java.util.Iterator; |
| 21 | import java.util.ArrayList; |
| 22 | |
| 23 | /** |
| 24 | * Class used for collapsing data items into groups of similar items. The data items that should be |
| 25 | * collapsible should implement the Collapsible interface. The class also contains a utility |
| 26 | * function that takes an ArrayList of items and returns a list of the same items collapsed into |
| 27 | * groups. |
| 28 | */ |
| 29 | public final class Collapser { |
| 30 | |
| 31 | /* |
| 32 | * This utility class cannot be instantiated. |
| 33 | */ |
| 34 | private Collapser() {} |
| 35 | |
| 36 | /* |
| 37 | * Interface implemented by data types that can be collapsed into groups of similar data. This |
| 38 | * can be used for example to collapse similar contact data items into a single item. |
| 39 | */ |
| 40 | public interface Collapsible<T> { |
| 41 | public boolean collapseWith(T t); |
| 42 | public String getCollapseKey(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Collapses a list of Collapsible items into a list of collapsed items. Items are collapsed |
| 47 | * if they produce equal collapseKeys {@Link Collapsible#getCollapseKey()}, and are collapsed |
| 48 | * through the {@Link Collapsible#doCollapseWith(Object)} function implemented by the data item. |
| 49 | * |
| 50 | * @param list ArrayList of Objects of type <T extends Collapsible<T>> to be collapsed. |
| 51 | */ |
| 52 | public static <T extends Collapsible<T>> void collapseList(ArrayList<T> list) { |
| 53 | HashMap<String, T> collapseMap = new HashMap<String, T>(); |
| 54 | ArrayList<String> collapseKeys = new ArrayList<String>(); |
| 55 | |
| 56 | int listSize = list.size(); |
| 57 | for (int j = 0; j < listSize; j++) { |
| 58 | T entry = list.get(j); |
| 59 | String collapseKey = entry.getCollapseKey(); |
| 60 | if (!collapseMap.containsKey(collapseKey)) { |
| 61 | collapseMap.put(collapseKey, entry); |
| 62 | collapseKeys.add(collapseKey); |
| 63 | } else { |
| 64 | collapseMap.get(collapseKey).collapseWith(entry); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if (collapseKeys.size() < listSize) { |
| 69 | list.clear(); |
| 70 | Iterator<String> itr = collapseKeys.iterator(); |
| 71 | while (itr.hasNext()) { |
| 72 | list.add(collapseMap.get(itr.next())); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |