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 androidx.annotation.NonNull;
22 import androidx.annotation.Nullable;
23 import androidx.annotation.RequiresApi;
24 import android.util.Log;
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;
38 public class ObservableList<T> extends Observable {
40 public ObservableList(List<T> list) {
43 private void forceNotify() {
47 private void forceNotify(int index) {
49 notifyObservers(index);
54 public boolean isEmpty() {
55 return list.isEmpty();
57 public boolean contains(@Nullable Object o) {
58 return list.contains(o);
60 public Iterator<T> iterator() {
61 return list.iterator();
63 public Object[] toArray() {
64 return list.toArray();
66 public <T1> T1[] toArray(@Nullable T1[] a) {
67 return list.toArray(a);
69 public boolean add(T t) {
70 boolean result = list.add(t);
71 if (result) forceNotify();
74 public boolean remove(@Nullable Object o) {
75 boolean result = list.remove(o);
76 if (result) forceNotify();
79 public boolean containsAll(@NonNull Collection<?> c) {
80 return list.containsAll(c);
82 public boolean addAll(@NonNull Collection<? extends T> c) {
83 boolean result = list.addAll(c);
84 if (result) forceNotify();
87 public boolean addAll(int index, @NonNull Collection<? extends T> c) {
88 boolean result = list.addAll(index, c);
89 if (result) forceNotify();
92 public boolean removeAll(@NonNull Collection<?> c) {
93 boolean result = list.removeAll(c);
94 if (result) forceNotify();
97 public boolean retainAll(@NonNull Collection<?> c) {
98 boolean result = list.retainAll(c);
99 if (result) forceNotify();
102 @RequiresApi(api = Build.VERSION_CODES.N)
103 public void replaceAll(@NonNull UnaryOperator<T> operator) {
104 list.replaceAll(operator);
107 @RequiresApi(api = Build.VERSION_CODES.N)
108 public void sort(@Nullable Comparator<? super T> c) {
112 public void clear() {
113 boolean wasEmpty = list.isEmpty();
115 if (!wasEmpty) forceNotify();
117 public T get(int index) {
118 return list.get(index);
120 public T set(int index, T element) {
121 T result = list.set(index, element);
125 public void add(int index, T element) {
126 list.add(index, element);
129 public T remove(int index) {
130 T result = list.remove(index);
134 public int indexOf(@Nullable Object o) {
135 return list.indexOf(o);
137 public int lastIndexOf(@Nullable Object o) {
138 return list.lastIndexOf(o);
140 public ListIterator<T> listIterator() {
141 return list.listIterator();
143 public ListIterator<T> listIterator(int index) {
144 return list.listIterator(index);
146 public List<T> subList(int fromIndex, int toIndex) {
147 return list.subList(fromIndex, toIndex);
149 @RequiresApi(api = Build.VERSION_CODES.N)
150 public Spliterator<T> spliterator() {
151 return list.spliterator();
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();
159 @RequiresApi(api = Build.VERSION_CODES.N)
160 public Stream<T> stream() {
161 return list.stream();
163 @RequiresApi(api = Build.VERSION_CODES.N)
164 public Stream<T> parallelStream() {
165 return list.parallelStream();
167 @RequiresApi(api = Build.VERSION_CODES.N)
168 public void forEach(Consumer<? super T> action) {
169 list.forEach(action);
171 public List<T> getList() {
174 public void setList(List<T> aList) {
178 public void triggerItemChangedNotification(T item) {
179 int index = list.indexOf(item);
181 Log.d("ObList", "??? not sending notifications for item not found in the list");
184 Log.d("ObList", "Notifying item change observers");
185 triggerItemChangedNotification(index);
187 public void triggerItemChangedNotification(int index) {