Debian Codegraph @

extensions/library/src/main/java/android/databinding/ObservableArrayList.java

raw
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 */
16 package android.databinding;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20
21 /**
22 * An {@link ObservableList} implementation using ArrayList as an implementation.
23 */
24 public class ObservableArrayList<T> extends ArrayList<T> implements ObservableList<T> {
25 private transient ListChangeRegistry mListeners = new ListChangeRegistry();
26
27 @Override
28 public void addOnListChangedCallback(OnListChangedCallback listener) {
29 if (mListeners == null) {
30 mListeners = new ListChangeRegistry();
31 }
32 mListeners.add(listener);
33 }
34
35 @Override
36 public void removeOnListChangedCallback(OnListChangedCallback listener) {
37 if (mListeners != null) {
38 mListeners.remove(listener);
39 }
40 }
41
42 @Override
43 public boolean add(T object) {
44 super.add(object);
45 notifyAdd(size() - 1, 1);
46 return true;
47 }
48
49 @Override
50 public void add(int index, T object) {
51 super.add(index, object);
52 notifyAdd(index, 1);
53 }
54
55 @Override
56 public boolean addAll(Collection<? extends T> collection) {
57 int oldSize = size();
58 boolean added = super.addAll(collection);
59 if (added) {
60 notifyAdd(oldSize, size() - oldSize);
61 }
62 return added;
63 }
64
65 @Override
66 public boolean addAll(int index, Collection<? extends T> collection) {
67 boolean added = super.addAll(index, collection);
68 if (added) {
69 notifyAdd(index, collection.size());
70 }
71 return added;
72 }
73
74 @Override
75 public void clear() {
76 int oldSize = size();
77 super.clear();
78 if (oldSize != 0) {
79 notifyRemove(0, oldSize);
80 }
81 }
82
83 @Override
84 public T remove(int index) {
85 T val = super.remove(index);
86 notifyRemove(index, 1);
87 return val;
88 }
89
90 @Override
91 public boolean remove(Object object) {
92 int index = indexOf(object);
93 if (index >= 0) {
94 remove(index);
95 return true;
96 } else {
97 return false;
98 }
99 }
100
101 @Override
102 public T set(int index, T object) {
103 T val = super.set(index, object);
104 if (mListeners != null) {
105 mListeners.notifyChanged(this, index, 1);
106 }
107 return val;
108 }
109
110 @Override
111 protected void removeRange(int fromIndex, int toIndex) {
112 super.removeRange(fromIndex, toIndex);
113 notifyRemove(fromIndex, toIndex - fromIndex);
114 }
115
116 private void notifyAdd(int start, int count) {
117 if (mListeners != null) {
118 mListeners.notifyInserted(this, start, count);
119 }
120 }
121
122 private void notifyRemove(int start, int count) {
123 if (mListeners != null) {
124 mListeners.notifyRemoved(this, start, count);
125 }
126 }
127 }