2 * Copyright © 2020 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;
22 import androidx.annotation.NonNull;
23 import androidx.annotation.Nullable;
24 import androidx.annotation.RequiresApi;
26 import org.jetbrains.annotations.NotNull;
28 import java.util.Collection;
29 import java.util.Comparator;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.ListIterator;
33 import java.util.Observable;
34 import java.util.Spliterator;
35 import java.util.concurrent.locks.ReentrantReadWriteLock;
36 import java.util.function.Consumer;
37 import java.util.function.Predicate;
38 import java.util.function.UnaryOperator;
39 import java.util.stream.Stream;
41 import static net.ktnx.mobileledger.utils.Logger.debug;
43 public class ObservableList<T> extends Observable implements List<T> {
45 private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
46 private int notificationBlocks = 0;
47 private boolean notificationWasBlocked = false;
48 public ObservableList(List<T> list) {
51 private void forceNotify() {
52 if (notificationBlocked()) return;
56 private boolean notificationBlocked() {
57 return notificationWasBlocked = (notificationBlocks > 0);
59 private void forceNotify(int index) {
60 if (notificationBlocked()) return;
62 notifyObservers(index);
65 try (LockHolder lh = lockForReading()) {
69 public boolean isEmpty() {
70 try (LockHolder lh = lockForReading()) {
71 return list.isEmpty();
74 public boolean contains(@Nullable Object o) {
75 try (LockHolder lh = lockForReading()) {
76 return list.contains(o);
80 public Iterator<T> iterator() {
81 throw new RuntimeException("Iterators break encapsulation and ignore locking");
82 // return list.iterator();
85 public Object[] toArray() {
86 try (LockHolder lh = lockForReading()) {
87 return list.toArray();
92 public <T1> T1[] toArray(@Nullable T1[] a) {
93 try (LockHolder lh = lockForReading()) {
94 return list.toArray(a);
97 public boolean add(T t) {
98 try (LockHolder lh = lockForWriting()) {
99 boolean result = list.add(t);
106 public boolean remove(@Nullable Object o) {
107 try (LockHolder lh = lockForWriting()) {
108 boolean result = list.remove(o);
110 if (result) forceNotify();
114 public T removeQuietly(int index) {
115 return list.remove(index);
117 public boolean containsAll(@NonNull Collection<?> c) {
118 try (LockHolder lh = lockForReading()) {
119 return list.containsAll(c);
122 public boolean addAll(@NonNull Collection<? extends T> c) {
123 try (LockHolder lh = lockForWriting()) {
124 boolean result = list.addAll(c);
126 if (result) forceNotify();
130 public boolean addAll(int index, @NonNull Collection<? extends T> c) {
131 try (LockHolder lh = lockForWriting()) {
132 boolean result = list.addAll(index, c);
134 if (result) forceNotify();
138 public boolean addAllQuietly(int index, Collection<? extends T> c) {
139 return list.addAll(index, c);
141 public boolean removeAll(@NonNull Collection<?> c) {
142 try (LockHolder lh = lockForWriting()) {
143 boolean result = list.removeAll(c);
145 if (result) forceNotify();
149 public boolean retainAll(@NonNull Collection<?> c) {
150 try (LockHolder lh = lockForWriting()) {
151 boolean result = list.retainAll(c);
153 if (result) forceNotify();
157 @RequiresApi(api = Build.VERSION_CODES.N)
158 public void replaceAll(@NonNull UnaryOperator<T> operator) {
159 try (LockHolder lh = lockForWriting()) {
160 list.replaceAll(operator);
165 @RequiresApi(api = Build.VERSION_CODES.N)
166 public void sort(@Nullable Comparator<? super T> c) {
167 try (LockHolder lh = lockForWriting()) {
168 lock.writeLock().lock();
174 public void clear() {
175 try (LockHolder lh = lockForWriting()) {
176 boolean wasEmpty = list.isEmpty();
179 if (!wasEmpty) forceNotify();
182 public T get(int index) {
183 try (LockHolder lh = lockForReading()) {
184 return list.get(index);
187 public T set(int index, T element) {
188 try (LockHolder lh = lockForWriting()) {
189 T result = list.set(index, element);
195 public void add(int index, T element) {
196 try (LockHolder lh = lockForWriting()) {
197 list.add(index, element);
202 public T remove(int index) {
203 try (LockHolder lh = lockForWriting()) {
204 T result = list.remove(index);
210 public int indexOf(@Nullable Object o) {
211 try (LockHolder lh = lockForReading()) {
212 return list.indexOf(o);
215 public int lastIndexOf(@Nullable Object o) {
216 try (LockHolder lh = lockForReading()) {
217 return list.lastIndexOf(o);
221 public ListIterator<T> listIterator() {
222 if (!lock.isWriteLockedByCurrentThread()) throw new RuntimeException(
223 "Iterators break encapsulation and ignore locking. Write-lock first");
224 return list.listIterator();
227 public ListIterator<T> listIterator(int index) {
228 if (!lock.isWriteLockedByCurrentThread()) throw new RuntimeException(
229 "Iterators break encapsulation and ignore locking. Write-lock first");
230 return list.listIterator(index);
233 public List<T> subList(int fromIndex, int toIndex) {
234 try (LockHolder lh = lockForReading()) {
235 return list.subList(fromIndex, toIndex);
239 @RequiresApi(api = Build.VERSION_CODES.N)
240 public Spliterator<T> spliterator() {
241 if (!lock.isWriteLockedByCurrentThread())
242 throw new RuntimeException(
243 "Iterators break encapsulation and ignore locking. Write-lock first");
244 return list.spliterator();
246 @RequiresApi(api = Build.VERSION_CODES.N)
247 public boolean removeIf(@NotNull Predicate<? super T> filter) {
248 try (LockHolder lh = lockForWriting()) {
249 boolean result = list.removeIf(filter);
257 @RequiresApi(api = Build.VERSION_CODES.N)
258 public Stream<T> stream() {
259 if (!lock.isWriteLockedByCurrentThread()) throw new RuntimeException(
260 "Iterators break encapsulation and ignore locking. Write-lock first");
261 return list.stream();
264 @RequiresApi(api = Build.VERSION_CODES.N)
265 public Stream<T> parallelStream() {
266 if (!lock.isWriteLockedByCurrentThread())
267 throw new RuntimeException(
268 "Iterators break encapsulation and ignore locking. Write-lock first");
269 return list.parallelStream();
271 @RequiresApi(api = Build.VERSION_CODES.N)
272 public void forEach(@NotNull Consumer<? super T> action) {
273 try (LockHolder lh = lockForReading()) {
274 list.forEach(action);
277 public List<T> getList() {
278 if (!lock.isWriteLockedByCurrentThread())
279 throw new RuntimeException(
280 "Direct list access breaks encapsulation and ignore locking. Write-lock first");
283 public void setList(List<T> aList) {
284 try (LockHolder lh = lockForWriting()) {
290 public void triggerItemChangedNotification(T item) {
291 try (LockHolder lh = lockForReading()) {
292 int index = list.indexOf(item);
294 debug("ObList", "??? not sending notifications for item not found in the list");
297 debug("ObList", "Notifying item change observers");
298 triggerItemChangedNotification(index);
301 public void triggerItemChangedNotification(int index) {
304 public LockHolder lockForWriting() {
305 ReentrantReadWriteLock.WriteLock wLock = lock.writeLock();
308 ReentrantReadWriteLock.ReadLock rLock = lock.readLock();
311 return new LockHolder(rLock, wLock);
313 public LockHolder lockForReading() {
314 ReentrantReadWriteLock.ReadLock rLock = lock.readLock();
316 return new LockHolder(rLock);
318 public void blockNotifications() {
319 notificationBlocks++;
321 public void unblockNotifications() {
322 notificationBlocks--;
323 if ((notificationBlocks == 0) && notificationWasBlocked) notifyObservers();