ArrayObjectAdapter.java revision eb66dab544c4c1eabe4d469b7cea348d4b01e664
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14package android.support.v17.leanback.widget;
15
16import java.util.ArrayList;
17import java.util.Collection;
18
19/**
20 * An ObjectAdapter implemented with an {@link ArrayList}.
21 */
22public class ArrayObjectAdapter extends ObjectAdapter {
23
24    private ArrayList<Object> mItems = new ArrayList<Object>();
25
26    /**
27     * Construct an adapter with the given {@link PresenterSelector}.
28     */
29    public ArrayObjectAdapter(PresenterSelector presenterSelector) {
30        super(presenterSelector);
31    }
32
33    /**
34     * Construct an adapter that uses the given {@link Presenter} for all items.
35     */
36    public ArrayObjectAdapter(Presenter presenter) {
37        super(presenter);
38    }
39
40    /**
41     * Construct an adapter.
42     */
43    public ArrayObjectAdapter() {
44        super();
45    }
46
47    @Override
48    public int size() {
49        return mItems.size();
50    }
51
52    @Override
53    public Object get(int index) {
54        return mItems.get(index);
55    }
56
57    /**
58     * Returns the index for the first occurrence of item in the adapter, or -1 if
59     * not found.
60     *
61     * @param item  The item to find in the list.
62     * @return Index of the first occurrence of the item in the adapter, or -1
63     *         if not found.
64     */
65    public int indexOf(Object item) {
66        return mItems.indexOf(item);
67    }
68
69    /**
70     * Notify that the content of a range of items changed. Note that this is
71     * not same as items being added or removed.
72     *
73     * @param positionStart The position of first item that has changed.
74     * @param itemCount The count of how many items have changed.
75     */
76    public void notifyArrayItemRangeChanged(int positionStart, int itemCount) {
77        notifyItemRangeChanged(positionStart, itemCount);
78    }
79
80    /**
81     * Adds an item to the end of the adapter.
82     *
83     * @param item The item to add to the end of the adapter.
84     */
85    public void add(Object item) {
86        add(mItems.size(), item);
87    }
88
89    /**
90     * Inserts an item into this adapter at the specified index.
91     * If the index is >= {@link #size} an exception will be thrown.
92     *
93     * @param index The index at which the item should be inserted.
94     * @param item The item to insert into the adapter.
95     */
96    public void add(int index, Object item) {
97        mItems.add(index, item);
98        notifyItemRangeInserted(index, 1);
99    }
100
101    /**
102     * Adds the objects in the given collection to the adapter, starting at the
103     * given index.  If the index is >= {@link #size} an exception will be thrown.
104     *
105     * @param index The index at which the items should be inserted.
106     * @param items A {@link Collection} of items to insert.
107     */
108    public void addAll(int index, Collection items) {
109        int itemsCount = items.size();
110        if (itemsCount == 0) {
111            return;
112        }
113        mItems.addAll(index, items);
114        notifyItemRangeInserted(index, itemsCount);
115    }
116
117    /**
118     * Removes the first occurrence of the given item from the adapter.
119     *
120     * @param item The item to remove from the adapter.
121     * @return True if the item was found and thus removed from the adapter.
122     */
123    public boolean remove(Object item) {
124        int index = mItems.indexOf(item);
125        if (index >= 0) {
126            mItems.remove(index);
127            notifyItemRangeRemoved(index, 1);
128        }
129        return index >= 0;
130    }
131
132    /**
133     * Replaces item at position with a new item and calls notifyItemRangeChanged()
134     * at the given position.  Note that this method does not compare new item to
135     * existing item.
136     * @param position  The index of item to replace.
137     * @param item      The new item to be placed at given position.
138     */
139    public void replace(int position, Object item) {
140        mItems.set(position, item);
141        notifyItemRangeChanged(position, 1);
142    }
143
144    /**
145     * Removes a range of items from the adapter. The range is specified by giving
146     * the starting position and the number of elements to remove.
147     *
148     * @param position The index of the first item to remove.
149     * @param count The number of items to remove.
150     * @return The number of items removed.
151     */
152    public int removeItems(int position, int count) {
153        int itemsToRemove = Math.min(count, mItems.size() - position);
154        if (itemsToRemove <= 0) {
155            return 0;
156        }
157
158        for (int i = 0; i < itemsToRemove; i++) {
159            mItems.remove(position);
160        }
161        notifyItemRangeRemoved(position, itemsToRemove);
162        return itemsToRemove;
163    }
164
165    /**
166     * Removes all items from this adapter, leaving it empty.
167     */
168    public void clear() {
169        int itemCount = mItems.size();
170        if (itemCount == 0) {
171            return;
172        }
173        mItems.clear();
174        notifyItemRangeRemoved(0, itemCount);
175    }
176}
177