Chiao Cheng | 3298464 | 2012-10-24 15:19:31 -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 | |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame^] | 17 | package com.android.contacts; |
Chiao Cheng | 3298464 | 2012-10-24 15:19:31 -0700 | [diff] [blame] | 18 | |
Paul Soulos | 9e795d3 | 2014-06-30 14:32:53 +0000 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | |
Chiao Cheng | 3298464 | 2012-10-24 15:19:31 -0700 | [diff] [blame] | 21 | import java.util.Iterator; |
| 22 | import java.util.List; |
| 23 | |
| 24 | /** |
| 25 | * Class used for collapsing data items into groups of similar items. The data items that should be |
| 26 | * collapsible should implement the Collapsible interface. The class also contains a utility |
| 27 | * function that takes an ArrayList of items and returns a list of the same items collapsed into |
| 28 | * groups. |
| 29 | */ |
| 30 | public final class Collapser { |
| 31 | |
| 32 | /* |
| 33 | * This utility class cannot be instantiated. |
| 34 | */ |
| 35 | private Collapser() {} |
| 36 | |
| 37 | /* |
Jay Shrauner | bbf0260 | 2013-05-16 10:59:24 -0700 | [diff] [blame] | 38 | * The Collapser uses an n^2 algorithm so we don't want it to run on |
| 39 | * lists beyond a certain size. This specifies the maximum size to collapse. |
| 40 | */ |
| 41 | private static final int MAX_LISTSIZE_TO_COLLAPSE = 20; |
| 42 | |
| 43 | /* |
Chiao Cheng | 3298464 | 2012-10-24 15:19:31 -0700 | [diff] [blame] | 44 | * Interface implemented by data types that can be collapsed into groups of similar data. This |
| 45 | * can be used for example to collapse similar contact data items into a single item. |
| 46 | */ |
| 47 | public interface Collapsible<T> { |
Jay Shrauner | 34ad2aa | 2013-01-08 11:41:55 -0800 | [diff] [blame] | 48 | public void collapseWith(T t); |
Paul Soulos | 9e795d3 | 2014-06-30 14:32:53 +0000 | [diff] [blame] | 49 | public boolean shouldCollapseWith(T t, Context context); |
| 50 | |
Chiao Cheng | 3298464 | 2012-10-24 15:19:31 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Collapses a list of Collapsible items into a list of collapsed items. Items are collapsed |
| 55 | * if {@link Collapsible#shouldCollapseWith(Object)} returns true, and are collapsed |
| 56 | * through the {@Link Collapsible#collapseWith(Object)} function implemented by the data item. |
| 57 | * |
| 58 | * @param list List of Objects of type <T extends Collapsible<T>> to be collapsed. |
| 59 | */ |
Paul Soulos | ef57670 | 2014-06-30 17:57:13 -0400 | [diff] [blame] | 60 | public static <T extends Collapsible<T>> void collapseList(List<T> list, Context context) { |
Chiao Cheng | 3298464 | 2012-10-24 15:19:31 -0700 | [diff] [blame] | 61 | |
| 62 | int listSize = list.size(); |
Jay Shrauner | bbf0260 | 2013-05-16 10:59:24 -0700 | [diff] [blame] | 63 | // The algorithm below is n^2 so don't run on long lists |
| 64 | if (listSize > MAX_LISTSIZE_TO_COLLAPSE) { |
| 65 | return; |
| 66 | } |
Chiao Cheng | 3298464 | 2012-10-24 15:19:31 -0700 | [diff] [blame] | 67 | |
| 68 | for (int i = 0; i < listSize; i++) { |
| 69 | T iItem = list.get(i); |
| 70 | if (iItem != null) { |
| 71 | for (int j = i + 1; j < listSize; j++) { |
| 72 | T jItem = list.get(j); |
| 73 | if (jItem != null) { |
Paul Soulos | ef57670 | 2014-06-30 17:57:13 -0400 | [diff] [blame] | 74 | if (iItem.shouldCollapseWith(jItem, context)) { |
Chiao Cheng | 3298464 | 2012-10-24 15:19:31 -0700 | [diff] [blame] | 75 | iItem.collapseWith(jItem); |
| 76 | list.set(j, null); |
Paul Soulos | ef57670 | 2014-06-30 17:57:13 -0400 | [diff] [blame] | 77 | } else if (jItem.shouldCollapseWith(iItem, context)) { |
Jay Shrauner | 34ad2aa | 2013-01-08 11:41:55 -0800 | [diff] [blame] | 78 | jItem.collapseWith(iItem); |
| 79 | list.set(i, null); |
| 80 | break; |
Chiao Cheng | 3298464 | 2012-10-24 15:19:31 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // Remove the null items |
| 88 | Iterator<T> itr = list.iterator(); |
| 89 | while (itr.hasNext()) { |
| 90 | if (itr.next() == null) { |
| 91 | itr.remove(); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | } |
Chiao Cheng | 3298464 | 2012-10-24 15:19:31 -0700 | [diff] [blame] | 96 | } |