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