blob: 5b5d5a0a2869c9f3aca5e182c5478464b770652b [file] [log] [blame]
Evan Millar54a5c9f2009-06-23 17:41:09 -07001/*
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
17package com.android.contacts;
18
Evan Millar54a5c9f2009-06-23 17:41:09 -070019import java.util.Iterator;
Daniel Lehmannaf8e3862010-11-19 15:38:44 -080020import java.util.List;
Evan Millar54a5c9f2009-06-23 17:41:09 -070021
22/**
23 * Class used for collapsing data items into groups of similar items. The data items that should be
24 * collapsible should implement the Collapsible interface. The class also contains a utility
25 * function that takes an ArrayList of items and returns a list of the same items collapsed into
26 * groups.
27 */
28public final class Collapser {
29
30 /*
31 * This utility class cannot be instantiated.
32 */
33 private Collapser() {}
34
35 /*
36 * Interface implemented by data types that can be collapsed into groups of similar data. This
37 * can be used for example to collapse similar contact data items into a single item.
38 */
39 public interface Collapsible<T> {
40 public boolean collapseWith(T t);
Evan Millaradb0f8c2009-09-28 17:20:50 -070041 public boolean shouldCollapseWith(T t);
Evan Millar54a5c9f2009-06-23 17:41:09 -070042 }
43
44 /**
45 * Collapses a list of Collapsible items into a list of collapsed items. Items are collapsed
Daniel Lehmanncdef2b62010-06-06 18:25:49 -070046 * if {@link Collapsible#shouldCollapseWith(Object)} returns strue, and are collapsed
Evan Millaradb0f8c2009-09-28 17:20:50 -070047 * through the {@Link Collapsible#collapseWith(Object)} function implemented by the data item.
Evan Millar54a5c9f2009-06-23 17:41:09 -070048 *
Daniel Lehmannaf8e3862010-11-19 15:38:44 -080049 * @param list List of Objects of type <T extends Collapsible<T>> to be collapsed.
Evan Millar54a5c9f2009-06-23 17:41:09 -070050 */
Daniel Lehmannaf8e3862010-11-19 15:38:44 -080051 public static <T extends Collapsible<T>> void collapseList(List<T> list) {
Evan Millar54a5c9f2009-06-23 17:41:09 -070052
53 int listSize = list.size();
Evan Millaradb0f8c2009-09-28 17:20:50 -070054
55 for (int i = 0; i < listSize; i++) {
56 T iItem = list.get(i);
57 if (iItem != null) {
58 for (int j = i + 1; j < listSize; j++) {
59 T jItem = list.get(j);
60 if (jItem != null) {
61 if (iItem.shouldCollapseWith(jItem)) {
62 iItem.collapseWith(jItem);
63 list.set(j, null);
64 }
65 }
66 }
Evan Millar54a5c9f2009-06-23 17:41:09 -070067 }
68 }
69
Evan Millaradb0f8c2009-09-28 17:20:50 -070070 // Remove the null items
71 Iterator<T> itr = list.iterator();
72 while (itr.hasNext()) {
73 if (itr.next() == null) {
74 itr.remove();
Evan Millar54a5c9f2009-06-23 17:41:09 -070075 }
76 }
Evan Millaradb0f8c2009-09-28 17:20:50 -070077
Evan Millar54a5c9f2009-06-23 17:41:09 -070078 }
79}