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.
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.
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/>.
18 package net.ktnx.mobileledger.utils;
20 import android.os.Build;
21 import android.util.Log;
23 import java.util.Collection;
24 import java.util.Comparator;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.ListIterator;
28 import java.util.Observable;
29 import java.util.Spliterator;
30 import java.util.function.Consumer;
31 import java.util.function.Predicate;
32 import java.util.function.UnaryOperator;
33 import java.util.stream.Stream;
35 import androidx.annotation.NonNull;
36 import androidx.annotation.Nullable;
37 import androidx.annotation.RequiresApi;
39 public class ObservableList<T> extends Observable implements List<T> {
41 public ObservableList(List<T> list) {
44 private void forceNotify() {
48 private void forceNotify(int index) {
50 notifyObservers(index);
55 public boolean isEmpty() {
56 return list.isEmpty();
58 public boolean contains(@Nullable Object o) {
59 return list.contains(o);
62 public Iterator<T> iterator() {
63 return list.iterator();
65 public Object[] toArray() {
66 return list.toArray();
68 public <T1> T1[] toArray(@Nullable T1[] a) {
69 return list.toArray(a);
71 public boolean add(T t) {
72 boolean result = list.add(t);
73 if (result) forceNotify();
76 public boolean remove(@Nullable Object o) {
77 boolean result = list.remove(o);
78 if (result) forceNotify();
81 public T removeQuietly(int index) {
82 return list.remove(index);
84 public boolean containsAll(@NonNull Collection<?> c) {
85 return list.containsAll(c);
87 public boolean addAll(@NonNull Collection<? extends T> c) {
88 boolean result = list.addAll(c);
89 if (result) forceNotify();
92 public boolean addAll(int index, @NonNull Collection<? extends T> c) {
93 boolean result = list.addAll(index, c);
94 if (result) forceNotify();
97 public boolean addAllQuietly(int index, Collection<? extends T> c) {
98 return list.addAll(index, c);
100 public boolean removeAll(@NonNull Collection<?> c) {
101 boolean result = list.removeAll(c);
102 if (result) forceNotify();
105 public boolean retainAll(@NonNull Collection<?> c) {
106 boolean result = list.retainAll(c);
107 if (result) forceNotify();
110 @RequiresApi(api = Build.VERSION_CODES.N)
111 public void replaceAll(@NonNull UnaryOperator<T> operator) {
112 list.replaceAll(operator);
115 @RequiresApi(api = Build.VERSION_CODES.N)
116 public void sort(@Nullable Comparator<? super T> c) {
120 public void clear() {
121 boolean wasEmpty = list.isEmpty();
123 if (!wasEmpty) forceNotify();
125 public T get(int index) {
126 return list.get(index);
128 public T set(int index, T element) {
129 T result = list.set(index, element);
133 public void add(int index, T element) {
134 list.add(index, element);
137 public T remove(int index) {
138 T result = list.remove(index);
142 public int indexOf(@Nullable Object o) {
143 return list.indexOf(o);
145 public int lastIndexOf(@Nullable Object o) {
146 return list.lastIndexOf(o);
148 public ListIterator<T> listIterator() {
149 return list.listIterator();
151 public ListIterator<T> listIterator(int index) {
152 return list.listIterator(index);
154 public List<T> subList(int fromIndex, int toIndex) {
155 return list.subList(fromIndex, toIndex);
157 @RequiresApi(api = Build.VERSION_CODES.N)
158 public Spliterator<T> spliterator() {
159 return list.spliterator();
161 @RequiresApi(api = Build.VERSION_CODES.N)
162 public boolean removeIf(Predicate<? super T> filter) {
163 boolean result = list.removeIf(filter);
164 if (result) forceNotify();
167 @RequiresApi(api = Build.VERSION_CODES.N)
168 public Stream<T> stream() {
169 return list.stream();
171 @RequiresApi(api = Build.VERSION_CODES.N)
172 public Stream<T> parallelStream() {
173 return list.parallelStream();
175 @RequiresApi(api = Build.VERSION_CODES.N)
176 public void forEach(Consumer<? super T> action) {
177 list.forEach(action);
179 public List<T> getList() {
182 public void setList(List<T> aList) {
186 public void triggerItemChangedNotification(T item) {
187 int index = list.indexOf(item);
189 Log.d("ObList", "??? not sending notifications for item not found in the list");
192 Log.d("ObList", "Notifying item change observers");
193 triggerItemChangedNotification(index);
195 public void triggerItemChangedNotification(int index) {