]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/ObservableList.java
profile list can be reordered
[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 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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.utils;
19
20 import android.os.Build;
21 import android.support.annotation.NonNull;
22 import android.support.annotation.Nullable;
23 import android.support.annotation.RequiresApi;
24
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;
36
37 public class ObservableList<T> extends Observable {
38     private List<T> list;
39     public ObservableList(List<T> list) {
40         this.list = list;
41     }
42     private void forceNotify() {
43         setChanged();
44         notifyObservers();
45     }
46     private void forceNotify(Object arg) {
47         setChanged();
48         notifyObservers(arg);
49     }
50     public int size() {
51         return list.size();
52     }
53     public boolean isEmpty() {
54         return list.isEmpty();
55     }
56     public boolean contains(@Nullable Object o) {
57         return list.contains(o);
58     }
59     public Iterator<T> iterator() {
60         return list.iterator();
61     }
62     public Object[] toArray() {
63         return list.toArray();
64     }
65     public <T1> T1[] toArray(@Nullable T1[] a) {
66         return list.toArray(a);
67     }
68     public boolean add(T t) {
69         boolean result = list.add(t);
70         if (result) forceNotify();
71         return result;
72     }
73     public boolean remove(@Nullable Object o) {
74         boolean result = list.remove(o);
75         if (result) forceNotify();
76         return result;
77     }
78     public boolean containsAll(@NonNull Collection<?> c) {
79         return list.containsAll(c);
80     }
81     public boolean addAll(@NonNull Collection<? extends T> c) {
82         boolean result = list.addAll(c);
83         if (result) forceNotify();
84         return result;
85     }
86     public boolean addAll(int index, @NonNull Collection<? extends T> c) {
87         boolean result = list.addAll(index, c);
88         if (result) forceNotify();
89         return result;
90     }
91     public boolean removeAll(@NonNull Collection<?> c) {
92         boolean result = list.removeAll(c);
93         if (result) forceNotify();
94         return result;
95     }
96     public boolean retainAll(@NonNull Collection<?> c) {
97         boolean result = list.retainAll(c);
98         if (result) forceNotify();
99         return result;
100     }
101     @RequiresApi(api = Build.VERSION_CODES.N)
102     public void replaceAll(@NonNull UnaryOperator<T> operator) {
103         list.replaceAll(operator);
104         forceNotify();
105     }
106     @RequiresApi(api = Build.VERSION_CODES.N)
107     public void sort(@Nullable Comparator<? super T> c) {
108         list.sort(c);
109         forceNotify();
110     }
111     public void clear() {
112         boolean wasEmpty = list.isEmpty();
113         list.clear();
114         if (!wasEmpty) forceNotify();
115     }
116     public T get(int index) {
117         return list.get(index);
118     }
119     public T set(int index, T element) {
120         T result = list.set(index, element);
121         forceNotify();
122         return result;
123     }
124     public void add(int index, T element) {
125         list.add(index, element);
126         forceNotify();
127     }
128     public T remove(int index) {
129         T result = list.remove(index);
130         forceNotify();
131         return result;
132     }
133     public int indexOf(@Nullable Object o) {
134         return list.indexOf(o);
135     }
136     public int lastIndexOf(@Nullable Object o) {
137         return list.lastIndexOf(o);
138     }
139     public ListIterator<T> listIterator() {
140         return list.listIterator();
141     }
142     public ListIterator<T> listIterator(int index) {
143         return list.listIterator(index);
144     }
145     public List<T> subList(int fromIndex, int toIndex) {
146         return list.subList(fromIndex, toIndex);
147     }
148     @RequiresApi(api = Build.VERSION_CODES.N)
149     public Spliterator<T> spliterator() {
150         return list.spliterator();
151     }
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();
156         return result;
157     }
158     @RequiresApi(api = Build.VERSION_CODES.N)
159     public Stream<T> stream() {
160         return list.stream();
161     }
162     @RequiresApi(api = Build.VERSION_CODES.N)
163     public Stream<T> parallelStream() {
164         return list.parallelStream();
165     }
166     @RequiresApi(api = Build.VERSION_CODES.N)
167     public void forEach(Consumer<? super T> action) {
168         list.forEach(action);
169     }
170     public void setList(List<T> aList) {
171         list = aList;
172         forceNotify();
173     }
174     public List<T> getList() {
175         return list;
176     }
177 }