]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/ObservableValue.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / utils / ObservableValue.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.utils;
19
20 import java.util.Observable;
21 import java.util.Observer;
22
23 public class ObservableValue<T> {
24     private final ObservableValueImpl<T> impl = new ObservableValueImpl<>();
25     public ObservableValue() {}
26     public ObservableValue(T initialValue) {
27         impl.setValue(initialValue, false);
28     }
29     public void set(T newValue) {
30         impl.setValue(newValue);
31     }
32     public T get() {
33         return impl.getValue();
34     }
35     public void addObserver(Observer o) {
36         impl.addObserver(o);
37     }
38     public void deleteObserver(Observer o) {
39         impl.deleteObserver(o);
40     }
41     public void notifyObservers() {
42         impl.notifyObservers();
43     }
44     public void notifyObservers(T arg) {
45         impl.notifyObservers(arg);
46     }
47     public void deleteObservers() {
48         impl.deleteObservers();
49     }
50     public boolean hasChanged() {
51         return impl.hasChanged();
52     }
53     public int countObservers() {
54         return impl.countObservers();
55     }
56     public void forceNotifyObservers() {
57         impl.setChanged();
58         impl.notifyObservers();
59     }
60     private static class ObservableValueImpl<T> extends Observable {
61         protected T value;
62         public void setValue(T newValue) {
63             setValue(newValue, true);
64         }
65         protected void setChanged() {
66             super.setChanged();
67         }
68         private synchronized void setValue(T newValue, boolean notify) {
69             if ((newValue == null) && (value == null))
70                 return;
71
72             if ((newValue != null) && newValue.equals(value)) return;
73
74             T oldValue = value;
75             value = newValue;
76             setChanged();
77             if (notify) notifyObservers(oldValue);
78         }
79         public T getValue() {
80             return value;
81         }
82     }
83 }