]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/ObservableAtomicInteger.java
another major rework, transaction list is fully asynchronous
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / 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.model;
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     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         holder.set(newValue);
43         forceNotify();
44     }
45     private void forceNotify() {
46         setChanged();
47         notifyObservers();
48     }
49 //    public void lazySet(int newValue) {
50 //        holder.lazySet(newValue);
51 //        forceNotify();
52 //    }
53     public int getAndSet(int newValue) {
54         int result = holder.getAndSet(newValue);
55         forceNotify();
56         return result;
57     }
58     public boolean compareAndSet(int expect, int update) {
59         boolean result = holder.compareAndSet(expect, update);
60         if (result) forceNotify();
61         return result;
62     }
63 //    public boolean weakCompareAndSet(int expect, int update) {
64 //        boolean result = holder.weakCompareAndSet(expect, update);
65 //        if (result) forceNotify();
66 //        return result;
67 //    }
68     public int getAndIncrement() {
69         int result = holder.getAndIncrement();
70         forceNotify();
71         return result;
72     }
73     public int getAndDecrement() {
74         int result = holder.getAndDecrement();
75         forceNotify();
76         return result;
77     }
78     public int getAndAdd(int delta) {
79         int result = holder.getAndAdd(delta);
80         forceNotify();
81         return result;
82     }
83     public int incrementAndGet() {
84         int result = holder.incrementAndGet();
85         forceNotify();
86         return result;
87     }
88     public int decrementAndGet() {
89         int result = holder.decrementAndGet();
90         forceNotify();
91         return result;
92     }
93     public int addAndGet(int delta) {
94         int result = holder.addAndGet(delta);
95         forceNotify();
96         return result;
97     }
98     @RequiresApi(Build.VERSION_CODES.N)
99     public int getAndUpdate(IntUnaryOperator updateFunction) {
100         int result = holder.getAndUpdate(updateFunction);
101         forceNotify();
102         return result;
103     }
104     @RequiresApi(api = Build.VERSION_CODES.N)
105     public int updateAndGet(IntUnaryOperator updateFunction) {
106         int result = holder.updateAndGet(updateFunction);
107         forceNotify();
108         return result;
109     }
110     @RequiresApi(api = Build.VERSION_CODES.N)
111     public int getAndAccumulate(int x, IntBinaryOperator accumulatorFunction) {
112         int result = holder.getAndAccumulate(x, accumulatorFunction);
113         forceNotify();
114         return result;
115     }
116     @RequiresApi(api = Build.VERSION_CODES.N)
117     public int accumulateAndGet(int x, IntBinaryOperator accumulatorFunction) {
118         int result = holder.accumulateAndGet(x, accumulatorFunction);
119         forceNotify();
120         return result;
121     }
122     public int intValue() {
123         return holder.intValue();
124     }
125     public long longValue() {
126         return holder.longValue();
127     }
128     public float floatValue() {
129         return holder.floatValue();
130     }
131     public double doubleValue() {
132         return holder.doubleValue();
133     }
134     public byte byteValue() {
135         return holder.byteValue();
136     }
137     public short shortValue() {
138         return holder.shortValue();
139     }
140 }