ListSuggestionCursor.java revision 120040ef31d79c1d69138b13ca7f256841f3298e
13e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert/*
23e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * Copyright (C) 2009 The Android Open Source Project
33e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert *
43e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License");
53e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * you may not use this file except in compliance with the License.
63e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * You may obtain a copy of the License at
73e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert *
83e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert *      http://www.apache.org/licenses/LICENSE-2.0
93e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert *
103e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * Unless required by applicable law or agreed to in writing, software
113e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS,
123e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * See the License for the specific language governing permissions and
143e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * limitations under the License.
153e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert */
163e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
173e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringertpackage com.android.quicksearchbox;
183e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
19120040ef31d79c1d69138b13ca7f256841f3298eMathew Inwoodimport com.google.common.annotations.VisibleForTesting;
20120040ef31d79c1d69138b13ca7f256841f3298eMathew Inwood
21fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringertimport android.database.DataSetObservable;
22fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringertimport android.database.DataSetObserver;
233e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
243e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringertimport java.util.ArrayList;
253e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
263e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert/**
2793bd2e70b8b08da1ec37fd0e990dac05551d2e90Bjorn Bringert * A SuggestionCursor that is backed by a list of Suggestions.
283e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert */
2904a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringertpublic class ListSuggestionCursor extends AbstractSuggestionCursorWrapper {
303e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
3108ff0a7cb6b99db79508fa3124730eb81411bc56Bjorn Bringert    private static final int DEFAULT_CAPACITY = 16;
3208ff0a7cb6b99db79508fa3124730eb81411bc56Bjorn Bringert
33fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert    private final DataSetObservable mDataSetObservable = new DataSetObservable();
34fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert
3593bd2e70b8b08da1ec37fd0e990dac05551d2e90Bjorn Bringert    private final ArrayList<Suggestion> mSuggestions;
363e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
3793bd2e70b8b08da1ec37fd0e990dac05551d2e90Bjorn Bringert    private int mPos = 0;
383e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
393e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    public ListSuggestionCursor(String userQuery) {
4008ff0a7cb6b99db79508fa3124730eb81411bc56Bjorn Bringert        this(userQuery, DEFAULT_CAPACITY);
4193bd2e70b8b08da1ec37fd0e990dac05551d2e90Bjorn Bringert    }
4293bd2e70b8b08da1ec37fd0e990dac05551d2e90Bjorn Bringert
43120040ef31d79c1d69138b13ca7f256841f3298eMathew Inwood    @VisibleForTesting
4493bd2e70b8b08da1ec37fd0e990dac05551d2e90Bjorn Bringert    public ListSuggestionCursor(String userQuery, Suggestion...suggestions) {
4508ff0a7cb6b99db79508fa3124730eb81411bc56Bjorn Bringert        this(userQuery, suggestions.length);
4693bd2e70b8b08da1ec37fd0e990dac05551d2e90Bjorn Bringert        for (Suggestion suggestion : suggestions) {
4793bd2e70b8b08da1ec37fd0e990dac05551d2e90Bjorn Bringert            mSuggestions.add(suggestion);
4893bd2e70b8b08da1ec37fd0e990dac05551d2e90Bjorn Bringert        }
493e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    }
503e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
5108ff0a7cb6b99db79508fa3124730eb81411bc56Bjorn Bringert    public ListSuggestionCursor(String userQuery, int capacity) {
5208ff0a7cb6b99db79508fa3124730eb81411bc56Bjorn Bringert        super(userQuery);
5308ff0a7cb6b99db79508fa3124730eb81411bc56Bjorn Bringert        mSuggestions = new ArrayList<Suggestion>(capacity);
5408ff0a7cb6b99db79508fa3124730eb81411bc56Bjorn Bringert    }
5508ff0a7cb6b99db79508fa3124730eb81411bc56Bjorn Bringert
563e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    /**
573e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert     * Adds a suggestion from another suggestion cursor.
583e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert     *
593e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert     * @return {@code true} if the suggestion was added.
603e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert     */
6193bd2e70b8b08da1ec37fd0e990dac05551d2e90Bjorn Bringert    public boolean add(Suggestion suggestion) {
6293bd2e70b8b08da1ec37fd0e990dac05551d2e90Bjorn Bringert        mSuggestions.add(suggestion);
633e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert        return true;
643e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    }
653e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
663e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    public void close() {
673e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert        mSuggestions.clear();
683e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    }
693e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
703e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    public int getPosition() {
713e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert        return mPos;
723e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    }
733e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
743e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    public void moveTo(int pos) {
753e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert        mPos = pos;
763e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    }
773e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
7887e947cbd9f279a83337900ff8bbd5ab0a8dc455Bjorn Bringert    public boolean moveToNext() {
7987e947cbd9f279a83337900ff8bbd5ab0a8dc455Bjorn Bringert        int size = mSuggestions.size();
8087e947cbd9f279a83337900ff8bbd5ab0a8dc455Bjorn Bringert        if (mPos >= size) {
8187e947cbd9f279a83337900ff8bbd5ab0a8dc455Bjorn Bringert            // Already past the end
8287e947cbd9f279a83337900ff8bbd5ab0a8dc455Bjorn Bringert            return false;
8387e947cbd9f279a83337900ff8bbd5ab0a8dc455Bjorn Bringert        }
8487e947cbd9f279a83337900ff8bbd5ab0a8dc455Bjorn Bringert        mPos++;
8587e947cbd9f279a83337900ff8bbd5ab0a8dc455Bjorn Bringert        return mPos < size;
8687e947cbd9f279a83337900ff8bbd5ab0a8dc455Bjorn Bringert    }
8787e947cbd9f279a83337900ff8bbd5ab0a8dc455Bjorn Bringert
8894e8a2be78530170f50e7895a558bf8011bbf8e8Bryan Mawhinney    public void removeRow() {
8994e8a2be78530170f50e7895a558bf8011bbf8e8Bryan Mawhinney        mSuggestions.remove(mPos);
9094e8a2be78530170f50e7895a558bf8011bbf8e8Bryan Mawhinney    }
9194e8a2be78530170f50e7895a558bf8011bbf8e8Bryan Mawhinney
9293bd2e70b8b08da1ec37fd0e990dac05551d2e90Bjorn Bringert    public void replaceRow(Suggestion suggestion) {
9393bd2e70b8b08da1ec37fd0e990dac05551d2e90Bjorn Bringert        mSuggestions.set(mPos, suggestion);
9494e8a2be78530170f50e7895a558bf8011bbf8e8Bryan Mawhinney    }
9594e8a2be78530170f50e7895a558bf8011bbf8e8Bryan Mawhinney
963e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    public int getCount() {
973e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert        return mSuggestions.size();
983e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    }
993e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
10004a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert    @Override
10193bd2e70b8b08da1ec37fd0e990dac05551d2e90Bjorn Bringert    protected Suggestion current() {
10293bd2e70b8b08da1ec37fd0e990dac05551d2e90Bjorn Bringert        return mSuggestions.get(mPos);
1033e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    }
1043e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
105bf61e445cbe423cc2554b722b6dd38675015c36dBjorn Bringert    @Override
106bf61e445cbe423cc2554b722b6dd38675015c36dBjorn Bringert    public String toString() {
1079038d65a5a8ebcfada1ec3067f81a26f05622088Mathew Inwood        return getClass().getSimpleName() + "{[" + getUserQuery() + "] " + mSuggestions + "}";
108bf61e445cbe423cc2554b722b6dd38675015c36dBjorn Bringert    }
109bf61e445cbe423cc2554b722b6dd38675015c36dBjorn Bringert
110fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert    /**
111fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert     * Register an observer that is called when changes happen to this data set.
112fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert     *
113fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert     * @param observer gets notified when the data set changes.
114fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert     */
115fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert    public void registerDataSetObserver(DataSetObserver observer) {
116fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert        mDataSetObservable.registerObserver(observer);
117ca78085bb2127559e6f55276a307bfa857018ecaBjorn Bringert    }
118ca78085bb2127559e6f55276a307bfa857018ecaBjorn Bringert
119fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert    /**
120fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert     * Unregister an observer that has previously been registered with
121fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert     * {@link #registerDataSetObserver(DataSetObserver)}
122fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert     *
123fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert     * @param observer the observer to unregister.
124fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert     */
125fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert    public void unregisterDataSetObserver(DataSetObserver observer) {
126fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert        mDataSetObservable.unregisterObserver(observer);
1273e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    }
1283e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
129fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert    protected void notifyDataSetChanged() {
130fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert        mDataSetObservable.notifyChanged();
1313e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    }
1323e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert}
133