1 package android.support.v17.leanback.widget;
2 
3 import android.util.SparseArray;
4 
5 /**
6  * An {@link ObjectAdapter} implemented with a {@link android.util.SparseArray}.
7  * This class maintains an array of objects where each object is associated
8  * with an integer key which determines its order relative to other objects.
9  */
10 public class SparseArrayObjectAdapter extends ObjectAdapter {
11     private SparseArray<Object> mItems = new SparseArray<Object>();
12 
13     /**
14      * Constructs an adapter with the given {@link PresenterSelector}.
15      */
SparseArrayObjectAdapter(PresenterSelector presenterSelector)16     public SparseArrayObjectAdapter(PresenterSelector presenterSelector) {
17         super(presenterSelector);
18     }
19 
20     /**
21      * Constructs an adapter with the given {@link Presenter}.
22      */
SparseArrayObjectAdapter(Presenter presenter)23     public SparseArrayObjectAdapter(Presenter presenter) {
24         super(presenter);
25     }
26 
27     /**
28      * Constructs an adapter.
29      */
SparseArrayObjectAdapter()30     public SparseArrayObjectAdapter() {
31         super();
32     }
33 
34     @Override
size()35     public int size() {
36         return mItems.size();
37     }
38 
39     @Override
get(int position)40     public Object get(int position) {
41         return mItems.valueAt(position);
42     }
43 
44     /**
45      * Returns the index for the given item in the adapter.
46      *
47      * @param item  The item to find in the array.
48      * @return Index of the item, or a negative value if not found.
49      */
indexOf(Object item)50     public int indexOf(Object item) {
51         return mItems.indexOfValue(item);
52     }
53 
54     /**
55      * Returns the index for the given key in the adapter.
56      *
57      * @param key The key to find in the array.
58      * @return Index of the item, or a negative value if not found.
59      */
indexOf(int key)60     public int indexOf(int key) {
61         return mItems.indexOfKey(key);
62     }
63 
64     /**
65      * Notify that the content of a range of items changed. Note that this is
66      * not same as items being added or removed.
67      *
68      * @param positionStart The position of first item that has changed.
69      * @param itemCount The count of how many items have changed.
70      */
notifyArrayItemRangeChanged(int positionStart, int itemCount)71     public void notifyArrayItemRangeChanged(int positionStart, int itemCount) {
72         notifyItemRangeChanged(positionStart, itemCount);
73     }
74 
75     /**
76      * Sets the item for the given key.
77      *
78      * @param key The key associated with the item.
79      * @param item The item associated with the key.
80      */
set(int key, Object item)81     public void set(int key, Object item) {
82         int index = mItems.indexOfKey(key);
83         if (index >= 0) {
84             if (mItems.valueAt(index) != item) {
85                 mItems.setValueAt(index, item);
86                 notifyItemRangeChanged(index, 1);
87             }
88         } else {
89             mItems.append(key, item);
90             index = mItems.indexOfKey(key);
91             notifyItemRangeInserted(index, 1);
92         }
93     }
94 
95     /**
96      * Clears the given key and associated item from the adapter.
97      *
98      * @param key The key to be cleared.
99      */
clear(int key)100     public void clear(int key) {
101         int index = mItems.indexOfKey(key);
102         if (index >= 0) {
103             mItems.removeAt(index);
104             notifyItemRangeRemoved(index, 1);
105         }
106     }
107 
108     /**
109      * Removes all items from this adapter, leaving it empty.
110      */
clear()111     public void clear() {
112         final int itemCount = mItems.size();
113         if (itemCount == 0) {
114             return;
115         }
116         mItems.clear();
117         notifyItemRangeRemoved(0, itemCount);
118     }
119 
120     /**
121      * Returns the object for the given key, or null if no mapping for that key exists.
122      */
lookup(int key)123     public Object lookup(int key) {
124         return mItems.get(key);
125     }
126 }