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.common;
18 
19 import android.content.Context;
20 import java.util.Iterator;
21 import java.util.List;
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    * The Collapser uses an n^2 algorithm so we don't want it to run on
33    * lists beyond a certain size. This specifies the maximum size to collapse.
34    */
35   private static final int MAX_LISTSIZE_TO_COLLAPSE = 20;
36 
37   /*
38    * This utility class cannot be instantiated.
39    */
Collapser()40   private Collapser() {}
41 
42   /**
43    * Collapses a list of Collapsible items into a list of collapsed items. Items are collapsed if
44    * {@link Collapsible#shouldCollapseWith(Object)} returns true, and are collapsed through the
45    * {@Link Collapsible#collapseWith(Object)} function implemented by the data item.
46    *
47    * @param list List of Objects of type <T extends Collapsible<T>> to be collapsed.
48    */
collapseList(List<T> list, Context context)49   public static <T extends Collapsible<T>> void collapseList(List<T> list, Context context) {
50 
51     int listSize = list.size();
52     // The algorithm below is n^2 so don't run on long lists
53     if (listSize > MAX_LISTSIZE_TO_COLLAPSE) {
54       return;
55     }
56 
57     for (int i = 0; i < listSize; i++) {
58       T iItem = list.get(i);
59       if (iItem != null) {
60         for (int j = i + 1; j < listSize; j++) {
61           T jItem = list.get(j);
62           if (jItem != null) {
63             if (iItem.shouldCollapseWith(jItem, context)) {
64               iItem.collapseWith(jItem);
65               list.set(j, null);
66             } else if (jItem.shouldCollapseWith(iItem, context)) {
67               jItem.collapseWith(iItem);
68               list.set(i, null);
69               break;
70             }
71           }
72         }
73       }
74     }
75 
76     // Remove the null items
77     Iterator<T> itr = list.iterator();
78     while (itr.hasNext()) {
79       if (itr.next() == null) {
80         itr.remove();
81       }
82     }
83   }
84 
85   /*
86    * Interface implemented by data types that can be collapsed into groups of similar data. This
87    * can be used for example to collapse similar contact data items into a single item.
88    */
89   public interface Collapsible<T> {
90 
collapseWith(T t)91     void collapseWith(T t);
92 
shouldCollapseWith(T t, Context context)93     boolean shouldCollapseWith(T t, Context context);
94   }
95 }
96