]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/ObservableAtomicInteger.java
move ObservableXXXX classes under utils/
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / utils / ObservableAtomicInteger.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.RequiresApi;
22
23 import java.util.Observable;
24 import java.util.concurrent.atomic.AtomicInteger;
25 import java.util.function.IntBinaryOperator;
26 import java.util.function.IntUnaryOperator;
27
28 public class ObservableAtomicInteger extends Observable {
29     private AtomicInteger holder;
30     ObservableAtomicInteger() {
31         super();
32         holder = new AtomicInteger();
33     }
34     public ObservableAtomicInteger(int initialValue) {
35         this();
36         holder.set(initialValue);
37     }
38     public int get() {
39         return holder.get();
40     }
41     public void set(int newValue) {
42 //        Log.d("atomic", "set");
43         holder.set(newValue);
44         forceNotify();
45     }
46     private void forceNotify() {
47         setChanged();
48 //        Log.d("atomic", String.format("notifying %d observers", countObservers()));
49         notifyObservers();
50     }
51 //    public void lazySet(int newValue) {
52 //        holder.lazySet(newValue);
53 //        forceNotify();
54 //    }
55     public int getAndSet(int newValue) {
56         int result = holder.getAndSet(newValue);
57         forceNotify();
58         return result;
59     }
60     public boolean compareAndSet(int expect, int update) {
61         boolean result = holder.compareAndSet(expect, update);
62         if (result) forceNotify();
63         return result;
64     }
65 //    public boolean weakCompareAndSet(int expect, int update) {
66 //        boolean result = holder.weakCompareAndSet(expect, update);
67 //        if (result) forceNotify();
68 //        return result;
69 //    }
70     public int getAndIncrement() {
71         int result = holder.getAndIncrement();
72         forceNotify();
73         return result;
74     }
75     public int getAndDecrement() {
76         int result = holder.getAndDecrement();
77         forceNotify();
78         return result;
79     }
80     public int getAndAdd(int delta) {
81         int result = holder.getAndAdd(delta);
82         forceNotify();
83         return result;
84     }
85     public int incrementAndGet() {
86 //        Log.d("atomic", "incrementAndGet");
87         int result = holder.incrementAndGet();
88         forceNotify();
89         return result;
90     }
91     public int decrementAndGet() {
92 //        Log.d("atomic", "decrementAndGet");
93         int result = holder.decrementAndGet();
94         forceNotify();
95         return result;
96     }
97     public int addAndGet(int delta) {
98         int result = holder.addAndGet(delta);
99         forceNotify();
100         return result;
101     }
102     @RequiresApi(Build.VERSION_CODES.N)
103     public int getAndUpdate(IntUnaryOperator updateFunction) {
104         int result = holder.getAndUpdate(updateFunction);
105         forceNotify();
106         return result;
107     }
108     @RequiresApi(api = Build.VERSION_CODES.N)
109     public int updateAndGet(IntUnaryOperator updateFunction) {
110         int result = holder.updateAndGet(updateFunction);
111         forceNotify();
112         return result;
113     }
114     @RequiresApi(api = Build.VERSION_CODES.N)
115     public int getAndAccumulate(int x, IntBinaryOperator accumulatorFunction) {
116         int result = holder.getAndAccumulate(x, accumulatorFunction);
117         forceNotify();
118         return result;
119     }
120     @RequiresApi(api = Build.VERSION_CODES.N)
121     public int accumulateAndGet(int x, IntBinaryOperator accumulatorFunction) {
122         int result = holder.accumulateAndGet(x, accumulatorFunction);
123         forceNotify();
124         return result;
125     }
126     public int intValue() {
127         return holder.intValue();
128     }
129     public long longValue() {
130         return holder.longValue();
131     }
132     public float floatValue() {
133         return holder.floatValue();
134     }
135     public double doubleValue() {
136         return holder.doubleValue();
137     }
138     public byte byteValue() {
139         return holder.byteValue();
140     }
141     public short shortValue() {
142         return holder.shortValue();
143     }
144 }