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.utils;
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 public ObservableAtomicInteger(int initialValue) {
36 holder.set(initialValue);
41 public void set(int newValue) {
42 // Log.d("atomic", "set");
46 private void forceNotify() {
48 // Log.d("atomic", String.format("notifying %d observers", countObservers()));
51 // public void lazySet(int newValue) {
52 // holder.lazySet(newValue);
55 public int getAndSet(int newValue) {
56 int result = holder.getAndSet(newValue);
60 public boolean compareAndSet(int expect, int update) {
61 boolean result = holder.compareAndSet(expect, update);
62 if (result) forceNotify();
65 // public boolean weakCompareAndSet(int expect, int update) {
66 // boolean result = holder.weakCompareAndSet(expect, update);
67 // if (result) forceNotify();
70 public int getAndIncrement() {
71 int result = holder.getAndIncrement();
75 public int getAndDecrement() {
76 int result = holder.getAndDecrement();
80 public int getAndAdd(int delta) {
81 int result = holder.getAndAdd(delta);
85 public int incrementAndGet() {
86 // Log.d("atomic", "incrementAndGet");
87 int result = holder.incrementAndGet();
91 public int decrementAndGet() {
92 // Log.d("atomic", "decrementAndGet");
93 int result = holder.decrementAndGet();
97 public int addAndGet(int delta) {
98 int result = holder.addAndGet(delta);
102 @RequiresApi(Build.VERSION_CODES.N)
103 public int getAndUpdate(IntUnaryOperator updateFunction) {
104 int result = holder.getAndUpdate(updateFunction);
108 @RequiresApi(api = Build.VERSION_CODES.N)
109 public int updateAndGet(IntUnaryOperator updateFunction) {
110 int result = holder.updateAndGet(updateFunction);
114 @RequiresApi(api = Build.VERSION_CODES.N)
115 public int getAndAccumulate(int x, IntBinaryOperator accumulatorFunction) {
116 int result = holder.getAndAccumulate(x, accumulatorFunction);
120 @RequiresApi(api = Build.VERSION_CODES.N)
121 public int accumulateAndGet(int x, IntBinaryOperator accumulatorFunction) {
122 int result = holder.accumulateAndGet(x, accumulatorFunction);
126 public int intValue() {
127 return holder.intValue();
129 public long longValue() {
130 return holder.longValue();
132 public float floatValue() {
133 return holder.floatValue();
135 public double doubleValue() {
136 return holder.doubleValue();
138 public byte byteValue() {
139 return holder.byteValue();
141 public short shortValue() {
142 return holder.shortValue();