1/*
2 * Copyright (C) 2015 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 */
16package android.databinding;
17
18import java.util.List;
19
20/**
21 * A {@link List} that notifies when changes are made. An ObservableList bound to the UI
22 * will keep the it up-to-date when changes occur.
23 * <p>
24 * The ObservableList must notify its callbacks whenever a change to the list occurs, using
25 * {@link OnListChangedCallback}.
26 * <p>
27 * ObservableArrayList implements ObservableList with an underlying ArrayList.
28 * ListChangeRegistry can help in maintaining the callbacks of other implementations.
29 *
30 * @see Observable
31 * @see ObservableMap
32 */
33public interface ObservableList<T> extends List<T> {
34
35    /**
36     * Adds a callback to be notified when changes to the list occur.
37     * @param callback The callback to be notified on list changes
38     */
39    void addOnListChangedCallback(OnListChangedCallback<? extends ObservableList<T>> callback);
40
41    /**
42     * Removes a callback previously added.
43     * @param callback The callback to remove.
44     */
45    void removeOnListChangedCallback(OnListChangedCallback<? extends ObservableList<T>> callback);
46
47    /**
48     * The callback that is called by ObservableList when the list has changed.
49     */
50    abstract class OnListChangedCallback<T extends ObservableList> {
51
52        /**
53         * Called whenever a change of unknown type has occurred, such as the entire list being
54         * set to new values.
55         *
56         * @param sender The changing list.
57         */
58        public abstract void onChanged(T sender);
59
60        /**
61         * Called whenever one or more items in the list have changed.
62         * @param sender The changing list.
63         * @param positionStart The starting index that has changed.
64         * @param itemCount The number of items that have changed.
65         */
66        public abstract void onItemRangeChanged(T sender, int positionStart, int itemCount);
67
68        /**
69         * Called whenever items have been inserted into the list.
70         * @param sender The changing list.
71         * @param positionStart The insertion index
72         * @param itemCount The number of items that have been inserted
73         */
74        public abstract void onItemRangeInserted(T sender, int positionStart, int itemCount);
75
76        /**
77         * Called whenever items in the list have been moved.
78         * @param sender The changing list.
79         * @param fromPosition The position from which the items were moved
80         * @param toPosition The destination position of the items
81         * @param itemCount The number of items moved
82         */
83        public abstract void onItemRangeMoved(T sender, int fromPosition, int toPosition,
84                int itemCount);
85
86        /**
87         * Called whenever items in the list have been deleted.
88         * @param sender The changing list.
89         * @param positionStart The starting index of the deleted items.
90         * @param itemCount The number of items removed.
91         */
92        public abstract void onItemRangeRemoved(T sender, int positionStart, int itemCount);
93    }
94}
95