2 * Copyright © 2019 Damyan Ivanov.
3 * This file is part of Mobile-Ledger.
4 * Mobile-Ledger 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 * Mobile-Ledger 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 Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.utils;
20 import android.os.Build;
21 import android.support.annotation.NonNull;
22 import android.support.annotation.Nullable;
23 import android.support.annotation.RequiresApi;
25 import java.util.Collection;
26 import java.util.Comparator;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.ListIterator;
30 import java.util.Observable;
31 import java.util.Spliterator;
32 import java.util.function.Consumer;
33 import java.util.function.Predicate;
34 import java.util.function.UnaryOperator;
35 import java.util.stream.Stream;
37 public class ObservableList<T> extends Observable {
39 public ObservableList(List<T> list) {
42 private void forceNotify() {
46 private void forceNotify(Object arg) {
53 public boolean isEmpty() {
54 return list.isEmpty();
56 public boolean contains(@Nullable Object o) {
57 return list.contains(o);
59 public Iterator<T> iterator() {
60 return list.iterator();
62 public Object[] toArray() {
63 return list.toArray();
65 public <T1> T1[] toArray(@Nullable T1[] a) {
66 return list.toArray(a);
68 public boolean add(T t) {
69 boolean result = list.add(t);
70 if (result) forceNotify();
73 public boolean remove(@Nullable Object o) {
74 boolean result = list.remove(o);
75 if (result) forceNotify();
78 public boolean containsAll(@NonNull Collection<?> c) {
79 return list.containsAll(c);
81 public boolean addAll(@NonNull Collection<? extends T> c) {
82 boolean result = list.addAll(c);
83 if (result) forceNotify();
86 public boolean addAll(int index, @NonNull Collection<? extends T> c) {
87 boolean result = list.addAll(index, c);
88 if (result) forceNotify();
91 public boolean removeAll(@NonNull Collection<?> c) {
92 boolean result = list.removeAll(c);
93 if (result) forceNotify();
96 public boolean retainAll(@NonNull Collection<?> c) {
97 boolean result = list.retainAll(c);
98 if (result) forceNotify();
101 @RequiresApi(api = Build.VERSION_CODES.N)
102 public void replaceAll(@NonNull UnaryOperator<T> operator) {
103 list.replaceAll(operator);
106 @RequiresApi(api = Build.VERSION_CODES.N)
107 public void sort(@Nullable Comparator<? super T> c) {
111 public void clear() {
112 boolean wasEmpty = list.isEmpty();
114 if (!wasEmpty) forceNotify();
116 public T get(int index) {
117 return list.get(index);
119 public T set(int index, T element) {
120 T result = list.set(index, element);
124 public void add(int index, T element) {
125 list.add(index, element);
128 public T remove(int index) {
129 T result = list.remove(index);
133 public int indexOf(@Nullable Object o) {
134 return list.indexOf(o);
136 public int lastIndexOf(@Nullable Object o) {
137 return list.lastIndexOf(o);
139 public ListIterator<T> listIterator() {
140 return list.listIterator();
142 public ListIterator<T> listIterator(int index) {
143 return list.listIterator(index);
145 public List<T> subList(int fromIndex, int toIndex) {
146 return list.subList(fromIndex, toIndex);
148 @RequiresApi(api = Build.VERSION_CODES.N)
149 public Spliterator<T> spliterator() {
150 return list.spliterator();
152 @RequiresApi(api = Build.VERSION_CODES.N)
153 public boolean removeIf(Predicate<? super T> filter) {
154 boolean result = list.removeIf(filter);
155 if (result) forceNotify();
158 @RequiresApi(api = Build.VERSION_CODES.N)
159 public Stream<T> stream() {
160 return list.stream();
162 @RequiresApi(api = Build.VERSION_CODES.N)
163 public Stream<T> parallelStream() {
164 return list.parallelStream();
166 @RequiresApi(api = Build.VERSION_CODES.N)
167 public void forEach(Consumer<? super T> action) {
168 list.forEach(action);
170 public void setList(List<T> aList) {
174 public List<T> getList() {