]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/ObservableList.java
e74271aff99e49ae60bfeb507512bf61f3006afd
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / utils / ObservableList.java
1 /*
2  * Copyright © 2019 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.utils;
19
20 import android.os.Build;
21 import androidx.annotation.NonNull;
22 import androidx.annotation.Nullable;
23 import androidx.annotation.RequiresApi;
24 import android.util.Log;
25
26 import java.util.Collection;
27 import java.util.Comparator;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.ListIterator;
31 import java.util.Observable;
32 import java.util.Spliterator;
33 import java.util.function.Consumer;
34 import java.util.function.Predicate;
35 import java.util.function.UnaryOperator;
36 import java.util.stream.Stream;
37
38 public class ObservableList<T> extends Observable {
39     private List<T> list;
40     public ObservableList(List<T> list) {
41         this.list = list;
42     }
43     private void forceNotify() {
44         setChanged();
45         notifyObservers();
46     }
47     private void forceNotify(int index) {
48         setChanged();
49         notifyObservers(index);
50     }
51     public int size() {
52         return list.size();
53     }
54     public boolean isEmpty() {
55         return list.isEmpty();
56     }
57     public boolean contains(@Nullable Object o) {
58         return list.contains(o);
59     }
60     public Iterator<T> iterator() {
61         return list.iterator();
62     }
63     public Object[] toArray() {
64         return list.toArray();
65     }
66     public <T1> T1[] toArray(@Nullable T1[] a) {
67         return list.toArray(a);
68     }
69     public boolean add(T t) {
70         boolean result = list.add(t);
71         if (result) forceNotify();
72         return result;
73     }
74     public boolean remove(@Nullable Object o) {
75         boolean result = list.remove(o);
76         if (result) forceNotify();
77         return result;
78     }
79     public boolean containsAll(@NonNull Collection<?> c) {
80         return list.containsAll(c);
81     }
82     public boolean addAll(@NonNull Collection<? extends T> c) {
83         boolean result = list.addAll(c);
84         if (result) forceNotify();
85         return result;
86     }
87     public boolean addAll(int index, @NonNull Collection<? extends T> c) {
88         boolean result = list.addAll(index, c);
89         if (result) forceNotify();
90         return result;
91     }
92     public boolean removeAll(@NonNull Collection<?> c) {
93         boolean result = list.removeAll(c);
94         if (result) forceNotify();
95         return result;
96     }
97     public boolean retainAll(@NonNull Collection<?> c) {
98         boolean result = list.retainAll(c);
99         if (result) forceNotify();
100         return result;
101     }
102     @RequiresApi(api = Build.VERSION_CODES.N)
103     public void replaceAll(@NonNull UnaryOperator<T> operator) {
104         list.replaceAll(operator);
105         forceNotify();
106     }
107     @RequiresApi(api = Build.VERSION_CODES.N)
108     public void sort(@Nullable Comparator<? super T> c) {
109         list.sort(c);
110         forceNotify();
111     }
112     public void clear() {
113         boolean wasEmpty = list.isEmpty();
114         list.clear();
115         if (!wasEmpty) forceNotify();
116     }
117     public T get(int index) {
118         return list.get(index);
119     }
120     public T set(int index, T element) {
121         T result = list.set(index, element);
122         forceNotify();
123         return result;
124     }
125     public void add(int index, T element) {
126         list.add(index, element);
127         forceNotify();
128     }
129     public T remove(int index) {
130         T result = list.remove(index);
131         forceNotify();
132         return result;
133     }
134     public int indexOf(@Nullable Object o) {
135         return list.indexOf(o);
136     }
137     public int lastIndexOf(@Nullable Object o) {
138         return list.lastIndexOf(o);
139     }
140     public ListIterator<T> listIterator() {
141         return list.listIterator();
142     }
143     public ListIterator<T> listIterator(int index) {
144         return list.listIterator(index);
145     }
146     public List<T> subList(int fromIndex, int toIndex) {
147         return list.subList(fromIndex, toIndex);
148     }
149     @RequiresApi(api = Build.VERSION_CODES.N)
150     public Spliterator<T> spliterator() {
151         return list.spliterator();
152     }
153     @RequiresApi(api = Build.VERSION_CODES.N)
154     public boolean removeIf(Predicate<? super T> filter) {
155         boolean result = list.removeIf(filter);
156         if (result) forceNotify();
157         return result;
158     }
159     @RequiresApi(api = Build.VERSION_CODES.N)
160     public Stream<T> stream() {
161         return list.stream();
162     }
163     @RequiresApi(api = Build.VERSION_CODES.N)
164     public Stream<T> parallelStream() {
165         return list.parallelStream();
166     }
167     @RequiresApi(api = Build.VERSION_CODES.N)
168     public void forEach(Consumer<? super T> action) {
169         list.forEach(action);
170     }
171     public List<T> getList() {
172         return list;
173     }
174     public void setList(List<T> aList) {
175         list = aList;
176         forceNotify();
177     }
178     public void triggerItemChangedNotification(T item) {
179         int index = list.indexOf(item);
180         if (index == -1) {
181             Log.d("ObList", "??? not sending notifications for item not found in the list");
182             return;
183         }
184         Log.d("ObList", "Notifying item change observers");
185         triggerItemChangedNotification(index);
186     }
187     public void triggerItemChangedNotification(int index) {
188         forceNotify(index);
189     }
190 }