PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/talkingdialer/src/com/google/marvin/talkingdialer/FilterableContactsList.java

http://eyes-free.googlecode.com/
Java | 68 lines | 58 code | 10 blank | 0 comment | 11 complexity | d66cb538f3af5c1277f5f8d8151b4d2d MD5 | raw file
 1
 2package com.google.marvin.talkingdialer;
 3
 4import java.util.ArrayList;
 5import java.util.ListIterator;
 6
 7public class FilterableContactsList {
 8    private ArrayList<ContactEntry> fullList;
 9
10    private ArrayList<ContactEntry> filteredList;
11
12    private ListIterator<ContactEntry> filteredListIter;
13
14    @SuppressWarnings("unchecked")
15    public FilterableContactsList(ArrayList<String> names) {
16        fullList = new ArrayList<ContactEntry>();
17        for (int i = 0; i < names.size(); i++) {
18            fullList.add(new ContactEntry(names.get(i), i));
19        }
20        filteredList = (ArrayList<ContactEntry>) fullList.clone();
21        filteredListIter = filteredList.listIterator();
22    }
23
24    public ContactEntry next() {
25        if (filteredList.size() < 1) {
26            return null;
27        }
28        if (!filteredListIter.hasNext()) {
29            filteredListIter = filteredList.listIterator();
30        }
31        return filteredListIter.next();
32    }
33
34    public ContactEntry previous() {
35        if (filteredList.size() < 1) {
36            return null;
37        }
38        if (!filteredListIter.hasPrevious()) {
39            filteredListIter = filteredList.listIterator(filteredList.size());
40        }
41        return filteredListIter.previous();
42    }
43
44    @SuppressWarnings("unchecked")
45    public boolean filter(String partialName) {
46        filteredList = (ArrayList<ContactEntry>) fullList.clone();
47        filteredListIter = filteredList.listIterator();
48        if (partialName.length() > 0) {
49            String lcPN = partialName.toLowerCase();
50            filteredList = new ArrayList<ContactEntry>();
51            for (int i = 0; i < fullList.size(); i++) {
52                ContactEntry entry = fullList.get(i);
53                if (entry.name != null) {
54                    String lcName = entry.name.toLowerCase();
55                    if (lcName.startsWith(lcPN)) {
56                        filteredList.add(entry);
57                    }
58                }
59            }
60            filteredListIter = filteredList.listIterator();
61        }
62        if (filteredList.size() > 0) {
63            return true;
64        }
65        return false;
66    }
67
68}