From: Damyan Ivanov Date: Wed, 9 Jan 2019 05:16:40 +0000 (+0000) Subject: new class for observing a list X-Git-Tag: v0.3~97 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=0700200b6627f97ac37083fc2c7131bcaa39b635;p=mobile-ledger.git new class for observing a list notifies observers when items are added/removed/set can't intercept direct item modifications --- diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/ObservableList.java b/app/src/main/java/net/ktnx/mobileledger/utils/ObservableList.java new file mode 100644 index 00000000..9893c5d1 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/utils/ObservableList.java @@ -0,0 +1,177 @@ +/* + * Copyright © 2019 Damyan Ivanov. + * This file is part of Mobile-Ledger. + * Mobile-Ledger is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * Mobile-Ledger is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with Mobile-Ledger. If not, see . + */ + +package net.ktnx.mobileledger.utils; + +import android.os.Build; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.annotation.RequiresApi; + +import java.util.Collection; +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.util.Observable; +import java.util.Spliterator; +import java.util.function.Consumer; +import java.util.function.Predicate; +import java.util.function.UnaryOperator; +import java.util.stream.Stream; + +public class ObservableList extends Observable { + private List list; + public ObservableList(List list) { + this.list = list; + } + private void forceNotify() { + setChanged(); + notifyObservers(); + } + private void forceNotify(Object arg) { + setChanged(); + notifyObservers(arg); + } + public int size() { + return list.size(); + } + public boolean isEmpty() { + return list.isEmpty(); + } + public boolean contains(@Nullable Object o) { + return list.contains(o); + } + public Iterator iterator() { + return list.iterator(); + } + public Object[] toArray() { + return list.toArray(); + } + public T1[] toArray(@Nullable T1[] a) { + return list.toArray(a); + } + public boolean add(T t) { + boolean result = list.add(t); + if (result) forceNotify(); + return result; + } + public boolean remove(@Nullable Object o) { + boolean result = list.remove(o); + if (result) forceNotify(); + return result; + } + public boolean containsAll(@NonNull Collection c) { + return list.containsAll(c); + } + public boolean addAll(@NonNull Collection c) { + boolean result = list.addAll(c); + if (result) forceNotify(); + return result; + } + public boolean addAll(int index, @NonNull Collection c) { + boolean result = list.addAll(index, c); + if (result) forceNotify(); + return result; + } + public boolean removeAll(@NonNull Collection c) { + boolean result = list.removeAll(c); + if (result) forceNotify(); + return result; + } + public boolean retainAll(@NonNull Collection c) { + boolean result = list.retainAll(c); + if (result) forceNotify(); + return result; + } + @RequiresApi(api = Build.VERSION_CODES.N) + public void replaceAll(@NonNull UnaryOperator operator) { + list.replaceAll(operator); + forceNotify(); + } + @RequiresApi(api = Build.VERSION_CODES.N) + public void sort(@Nullable Comparator c) { + list.sort(c); + forceNotify(); + } + public void clear() { + boolean wasEmpty = list.isEmpty(); + list.clear(); + if (!wasEmpty) forceNotify(); + } + public T get(int index) { + return list.get(index); + } + public T set(int index, T element) { + T result = list.set(index, element); + forceNotify(); + return result; + } + public void add(int index, T element) { + list.add(index, element); + forceNotify(); + } + public T remove(int index) { + T result = list.remove(index); + forceNotify(); + return result; + } + public int indexOf(@Nullable Object o) { + return list.indexOf(o); + } + public int lastIndexOf(@Nullable Object o) { + return list.lastIndexOf(o); + } + public ListIterator listIterator() { + return list.listIterator(); + } + public ListIterator listIterator(int index) { + return list.listIterator(index); + } + public List subList(int fromIndex, int toIndex) { + return list.subList(fromIndex, toIndex); + } + @RequiresApi(api = Build.VERSION_CODES.N) + public Spliterator spliterator() { + return list.spliterator(); + } + @RequiresApi(api = Build.VERSION_CODES.N) + public boolean removeIf(Predicate filter) { + boolean result = list.removeIf(filter); + if (result) forceNotify(); + return result; + } + @RequiresApi(api = Build.VERSION_CODES.N) + public Stream stream() { + return list.stream(); + } + @RequiresApi(api = Build.VERSION_CODES.N) + public Stream parallelStream() { + return list.parallelStream(); + } + @RequiresApi(api = Build.VERSION_CODES.N) + public void forEach(Consumer action) { + list.forEach(action); + } + public void setList(List aList) { + list = aList; + forceNotify(); + } + public Iterable getList() { + return list; + } +} \ No newline at end of file