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.
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.
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/>.
18 package net.ktnx.mobileledger.model;
20 import android.os.Build;
21 import android.support.annotation.RequiresApi;
23 import java.util.Observable;
24 import java.util.concurrent.atomic.AtomicInteger;
25 import java.util.function.IntBinaryOperator;
26 import java.util.function.IntUnaryOperator;
28 public class ObservableAtomicInteger extends Observable {
29 private AtomicInteger holder;
30 ObservableAtomicInteger() {
32 holder = new AtomicInteger();
34 ObservableAtomicInteger(int initialValue) {
36 holder.set(initialValue);
41 public void set(int newValue) {
45 private void forceNotify() {
49 // public void lazySet(int newValue) {
50 // holder.lazySet(newValue);
53 public int getAndSet(int newValue) {
54 int result = holder.getAndSet(newValue);
58 public boolean compareAndSet(int expect, int update) {
59 boolean result = holder.compareAndSet(expect, update);
60 if (result) forceNotify();
63 // public boolean weakCompareAndSet(int expect, int update) {
64 // boolean result = holder.weakCompareAndSet(expect, update);
65 // if (result) forceNotify();
68 public int getAndIncrement() {
69 int result = holder.getAndIncrement();
73 public int getAndDecrement() {
74 int result = holder.getAndDecrement();
78 public int getAndAdd(int delta) {
79 int result = holder.getAndAdd(delta);
83 public int incrementAndGet() {
84 int result = holder.incrementAndGet();
88 public int decrementAndGet() {
89 int result = holder.decrementAndGet();
93 public int addAndGet(int delta) {
94 int result = holder.addAndGet(delta);
98 @RequiresApi(Build.VERSION_CODES.N)
99 public int getAndUpdate(IntUnaryOperator updateFunction) {
100 int result = holder.getAndUpdate(updateFunction);
104 @RequiresApi(api = Build.VERSION_CODES.N)
105 public int updateAndGet(IntUnaryOperator updateFunction) {
106 int result = holder.updateAndGet(updateFunction);
110 @RequiresApi(api = Build.VERSION_CODES.N)
111 public int getAndAccumulate(int x, IntBinaryOperator accumulatorFunction) {
112 int result = holder.getAndAccumulate(x, accumulatorFunction);
116 @RequiresApi(api = Build.VERSION_CODES.N)
117 public int accumulateAndGet(int x, IntBinaryOperator accumulatorFunction) {
118 int result = holder.accumulateAndGet(x, accumulatorFunction);
122 public int intValue() {
123 return holder.intValue();
125 public long longValue() {
126 return holder.longValue();
128 public float floatValue() {
129 return holder.floatValue();
131 public double doubleValue() {
132 return holder.doubleValue();
134 public byte byteValue() {
135 return holder.byteValue();
137 public short shortValue() {
138 return holder.shortValue();